index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {miniProgramConfig} from '../../api/other';
  2. import {BASE_URL} from '../../utils/request';
  3. import {isLoggedIn,doLogin} from '../../utils/auth';
  4. const app = getApp();
  5. Page({
  6. data: {
  7. baseUrl:BASE_URL,
  8. banners: [],
  9. introduction:'',
  10. start_time:'',
  11. end_time:'',
  12. ad_img:'',
  13. pendingAction:null,//按钮类型
  14. loggedIn:false
  15. },
  16. async onLoad(){
  17. const res = await miniProgramConfig();
  18. app.globalData.programConfig = res.data;
  19. this.setData({
  20. banners:res.data.carousels,
  21. introduction:res.data.marathon_event.introduction,
  22. start_time:res.data.marathon_event.start_time,
  23. end_time:res.data.marathon_event.end_time,
  24. ad_img:res.data.ad_img
  25. })
  26. },
  27. onShow() {
  28. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  29. this.getTabBar().setData({
  30. selected: 0
  31. })
  32. this.setData({
  33. loggedIn: isLoggedIn()
  34. })
  35. }
  36. },
  37. goPage(e){
  38. const index = e.currentTarget.dataset.index;
  39. if(index == 0){
  40. wx.navigateTo({
  41. url: `/pages/rules/rules?type=${index}`
  42. })
  43. }else if(index == 1){
  44. wx.navigateTo({
  45. url: `/pages/rules/rules?type=${index}`
  46. })
  47. }
  48. },
  49. onShareAppMessage() {
  50. return {
  51. path: '/pages/index/index',
  52. imageUrl: app.globalData.programConfig.share_img
  53. }
  54. },
  55. goRegister(e) {
  56. const action = e.currentTarget.dataset.action;
  57. if (isLoggedIn()) {
  58. this.doAction(action)
  59. }
  60. },
  61. async onGetPhoneNumber(e) {
  62. const action = e.currentTarget.dataset.action;
  63. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  64. wx.showToast({ title: '授权失败', icon: 'none' })
  65. return
  66. }
  67. const { encryptedData, iv } = e.detail;
  68. try {
  69. wx.login({
  70. success: async loginRes => {
  71. await doLogin({
  72. code: loginRes.code,
  73. // phone: { encryptedData, iv }
  74. });
  75. this.setData({ loggedIn: isLoggedIn() })
  76. this.doAction(action);
  77. }
  78. })
  79. } catch (err) {
  80. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  81. console.error(err);
  82. }
  83. },
  84. doAction(action) {
  85. if (action === 'register') wx.navigateTo({ url: '/pages/register/register' });
  86. if (action === 'invite'){
  87. const tabbar = this.getTabBar && this.getTabBar()
  88. if (tabbar && tabbar.openShare) {
  89. tabbar.openShare()
  90. } else {
  91. console.log("没找到 tabbar 组件")
  92. }
  93. };
  94. }
  95. })