mine.js 3.4 KB

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