mine.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-5.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=${index}`,
  42. 1: `/pages/rules/rules?type=${index}`,
  43. 2: `/pages/registrationRecords/registrationRecords`,
  44. 3: `/pages/coupon/coupon`,
  45. 4: `/pages/setting/setting`,
  46. // 5:`/pages/registrationRecordList/registrationRecordList`,
  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. app.globalData.userInfo = res.data;
  68. } else {
  69. wx.showToast({
  70. title: res.message || '更新失败',
  71. icon: 'none',
  72. duration: 2000
  73. });
  74. }
  75. },
  76. onPhone() {
  77. MAKE_PHONE_CALL(this.data.programConfig.customer_phone);
  78. },
  79. handleActionWithLogin(e) {
  80. const index = e.currentTarget.dataset.index;
  81. if (index >= 2 && index <= 4 && !isLoggedIn()) {
  82. return;
  83. }
  84. this.doAction(index);
  85. },
  86. async onGetPhoneNumber(e) {
  87. const index = e.currentTarget.dataset.index;
  88. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  89. wx.showToast({ title: '授权失败', icon: 'none' });
  90. return;
  91. }
  92. const { encryptedData, iv } = e.detail;
  93. try {
  94. wx.login({
  95. success: async loginRes => {
  96. await doLogin({
  97. code: loginRes.code,
  98. phone: { encryptedData, iv }
  99. });
  100. this.setData({ loggedIn: true });
  101. this.doAction(index);
  102. }
  103. });
  104. } catch (err) {
  105. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  106. console.error(err);
  107. }
  108. },
  109. doAction(index) {
  110. this.goPage({ currentTarget: { dataset: { index } } });
  111. },
  112. })