mine.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. };
  48. if (pageMap[index]) {
  49. wx.navigateTo({ url: pageMap[index] });
  50. }
  51. // else if (index === 5) {
  52. // const tabbar = this.getTabBar && this.getTabBar();
  53. // if (tabbar && tabbar.openShowAgreementModal) {
  54. // tabbar.openShowAgreementModal()
  55. // }
  56. // }
  57. },
  58. // 头像选择
  59. async onChooseAvatar(e) {
  60. const { avatarUrl } = e.detail;
  61. const res = await uploadImage(avatarUrl);
  62. this.setData({ "userInfo.avatar": avatarUrl });
  63. this.onSave(res.path);
  64. },
  65. async onSave(pathUrl) {
  66. const res = await userModify({ avatar: pathUrl });
  67. if(res.code == 200){
  68. wx.setStorageSync('userInfo', res.data)
  69. app.globalData.userInfo = res.data;
  70. } else {
  71. wx.showToast({
  72. title: res.message || '更新失败',
  73. icon: 'none',
  74. duration: 2000
  75. });
  76. }
  77. },
  78. onPhone() {
  79. MAKE_PHONE_CALL(this.data.programConfig.customer_phone);
  80. },
  81. handleActionWithLogin(e) {
  82. const index = e.currentTarget.dataset.index;
  83. if (index >= 1 && index <= 4 && !isLoggedIn()) {
  84. return;
  85. }
  86. this.doAction(index);
  87. },
  88. async onGetPhoneNumber(e) {
  89. const index = e.currentTarget.dataset.index;
  90. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  91. wx.showToast({ title: '授权失败', icon: 'none' });
  92. return;
  93. }
  94. const { encryptedData, iv } = e.detail;
  95. try {
  96. wx.login({
  97. success: async loginRes => {
  98. await doLogin({
  99. code: loginRes.code,
  100. phone: { encryptedData, iv }
  101. });
  102. this.setData({ loggedIn: true });
  103. this.doAction(index);
  104. }
  105. });
  106. } catch (err) {
  107. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  108. console.error(err);
  109. }
  110. },
  111. doAction(index) {
  112. this.goPage({ currentTarget: { dataset: { index } } });
  113. },
  114. })