mine.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {BASE_URL} from '../../utils/request';
  2. import { uploadImage } from '../../utils/upload.js';
  3. import { MAKE_PHONE_CALL } from '../../utils/util.js';
  4. import { userModify } from '../../api/user';
  5. import { isLoggedIn, doLogin } from '../../utils/auth';
  6. const app = getApp();
  7. Page({
  8. data: {
  9. menuList: [
  10. // { icon: '/static/image/me-1.png', title: '活动规则' },
  11. { icon: '/static/image/me-2.png', title: '活动须知' },
  12. { icon: '/static/image/me-3.png', title: '报名记录' },
  13. { icon: '/static/image/me-4.png', title: '优惠券' },
  14. { icon: '/static/image/me-5.png', title: '设置' },
  15. { icon: '/static/image/me-3.png', title: '参赛管理' },
  16. { icon: '/static/image/me-6.png', title: '联系客服' }
  17. ],
  18. userInfo: {},
  19. programConfig:{},
  20. baseUrl:BASE_URL,
  21. loggedIn: isLoggedIn()
  22. },
  23. onLoad(options) {
  24. this.setData({
  25. userInfo: app.globalData.userInfo,
  26. programConfig: app.globalData.programConfig
  27. });
  28. },
  29. onShow() {
  30. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  31. this.getTabBar().setData({ selected: 2 })
  32. }
  33. this.setData({
  34. userInfo: app.globalData.userInfo,
  35. loggedIn: isLoggedIn()
  36. });
  37. },
  38. goPage(e){
  39. const index = e.currentTarget.dataset.index;
  40. const pageMap = {
  41. 0: `/pages/rules/rules?type=1`,
  42. // 1: `/pages/rules/rules?type=${index}`,
  43. 1: `/pages/registrationRecords/registrationRecords`,
  44. 2: `/pages/coupon/coupon`,
  45. 3: `/pages/setting/setting`,
  46. 4:`/pages/competitionRecords/competitionRecords`,
  47. 5:`/pages/webview/webview`,
  48. };
  49. if (pageMap[index]) {
  50. wx.navigateTo({ url: pageMap[index] });
  51. }
  52. // else if (index === 5) {
  53. // const tabbar = this.getTabBar && this.getTabBar();
  54. // if (tabbar && tabbar.openShowAgreementModal) {
  55. // tabbar.openShowAgreementModal()
  56. // }
  57. // }
  58. },
  59. // 头像选择
  60. async onChooseAvatar(e) {
  61. const { avatarUrl } = e.detail;
  62. const res = await uploadImage(avatarUrl);
  63. this.setData({ "userInfo.avatar": avatarUrl });
  64. this.onSave(res.path);
  65. },
  66. async onSave(pathUrl) {
  67. const res = await userModify({ avatar: pathUrl });
  68. if(res.code == 200){
  69. wx.setStorageSync('userInfo', res.data)
  70. app.globalData.userInfo = res.data;
  71. } else {
  72. wx.showToast({
  73. title: res.message || '更新失败',
  74. icon: 'none',
  75. duration: 2000
  76. });
  77. }
  78. },
  79. onPhone() {
  80. MAKE_PHONE_CALL(this.data.programConfig.customer_phone);
  81. },
  82. handleActionWithLogin(e) {
  83. const index = e.currentTarget.dataset.index;
  84. if (index >= 1 && index <= 4 && !isLoggedIn()) {
  85. return;
  86. }
  87. this.doAction(index);
  88. },
  89. async onGetPhoneNumber(e) {
  90. const index = e.currentTarget.dataset.index;
  91. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  92. wx.showToast({ title: '授权失败', icon: 'none' });
  93. return;
  94. }
  95. const { encryptedData, iv } = e.detail;
  96. try {
  97. wx.login({
  98. success: async loginRes => {
  99. await doLogin({
  100. code: loginRes.code,
  101. phone: { encryptedData, iv }
  102. });
  103. this.setData({ loggedIn: true });
  104. this.doAction(index);
  105. }
  106. });
  107. } catch (err) {
  108. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  109. console.error(err);
  110. }
  111. },
  112. doAction(index) {
  113. this.goPage({ currentTarget: { dataset: { index } } });
  114. },
  115. })