mine.js 3.3 KB

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