mine.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-6.png', title: '联系客服' }
  16. ],
  17. userInfo: {},
  18. programConfig:{},
  19. baseUrl:BASE_URL,
  20. loggedIn: isLoggedIn()
  21. },
  22. onLoad(options) {
  23. this.setData({
  24. userInfo: app.globalData.userInfo,
  25. programConfig: app.globalData.programConfig
  26. });
  27. },
  28. onShow() {
  29. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  30. this.getTabBar().setData({ selected: 2 })
  31. }
  32. this.setData({
  33. userInfo: app.globalData.userInfo,
  34. loggedIn: isLoggedIn()
  35. });
  36. },
  37. goPage(e){
  38. const index = e.currentTarget.dataset.index;
  39. const pageMap = {
  40. 0: `/pages/rules/rules?type=${index}`,
  41. 1: `/pages/rules/rules?type=${index}`,
  42. 2: `/pages/registrationRecords/registrationRecords`,
  43. 3: `/pages/coupon/coupon`,
  44. 4: `/pages/setting/setting`
  45. };
  46. if (pageMap[index]) {
  47. wx.navigateTo({ url: pageMap[index] });
  48. } else if (index === 5) {
  49. const tabbar = this.getTabBar && this.getTabBar();
  50. if (tabbar && tabbar.openShowAgreementModal) {
  51. tabbar.openShowAgreementModal()
  52. }
  53. }
  54. },
  55. // 头像选择
  56. async onChooseAvatar(e) {
  57. const { avatarUrl } = e.detail;
  58. const res = await uploadImage(avatarUrl);
  59. this.setData({ "userInfo.avatar": avatarUrl });
  60. this.onSave(res.path);
  61. },
  62. async onSave(pathUrl) {
  63. const res = await userModify({ avatar: pathUrl });
  64. if(res.code == 200){
  65. app.globalData.userInfo = res.data;
  66. } else {
  67. wx.showToast({
  68. title: res.message || '更新失败',
  69. icon: 'none',
  70. duration: 2000
  71. });
  72. }
  73. },
  74. onPhone() {
  75. MAKE_PHONE_CALL(this.data.programConfig.customer_phone);
  76. },
  77. handleActionWithLogin(e) {
  78. const index = e.currentTarget.dataset.index;
  79. if (index >= 2 && index <= 4 && !isLoggedIn()) {
  80. return;
  81. }
  82. this.doAction(index);
  83. },
  84. async onGetPhoneNumber(e) {
  85. const index = e.currentTarget.dataset.index;
  86. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  87. wx.showToast({ title: '授权失败', icon: 'none' });
  88. return;
  89. }
  90. const { encryptedData, iv } = e.detail;
  91. try {
  92. wx.login({
  93. success: async loginRes => {
  94. await doLogin({
  95. code: loginRes.code,
  96. // phone: { encryptedData, iv } 可按接口需要传
  97. });
  98. this.setData({ loggedIn: true });
  99. this.doAction(index);
  100. }
  101. });
  102. } catch (err) {
  103. wx.showToast({ title: '登录失败,请重试', icon: 'none' });
  104. console.error(err);
  105. }
  106. },
  107. doAction(index) {
  108. this.goPage({ currentTarget: { dataset: { index } } });
  109. },
  110. })