index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Dialg from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
  2. import { tabberFunc } from "../../utils/util";
  3. const app = getApp();
  4. Page({
  5. data: {
  6. waveimg: "../../assets/login/wave.svg",
  7. // 用户信息
  8. login: {},
  9. // 列表
  10. items: [
  11. { imgurl: 'loading', title: "更新资料", require: true, success () { this.updateFunc() } },
  12. { imgurl: 'punch', title: "草稿箱", require: true, success () { wx.navigateTo({ url: '/pages/template/index?data=drafts', }) } },
  13. { imgurl: 'form', title: "已填报", require: true, success () { wx.navigateTo({ url: '/pages/template/index?data=provided', }) } }
  14. ],
  15. update_lock: false,//更新资料按钮锁
  16. subscriber_lock: false,//订阅消息按钮锁
  17. },
  18. // tabber
  19. NavChange (e) {
  20. let { active, path } = e.currentTarget.dataset;
  21. tabberFunc(active, path)
  22. },
  23. // 更新资料
  24. async updateFunc () {
  25. function getUserProfile () {
  26. return new Promise((resolve, reject) => {
  27. wx.getUserProfile({
  28. desc: '用于完善会员资料',
  29. success: (res) => {
  30. let { encryptedData, iv } = res, { appId: appid } = app.globalData.mustArg, { openid } = app.globalData.user;
  31. resolve({ encryptedData, iv, appid, openid })
  32. },
  33. fail: err => reject(err)
  34. })
  35. })
  36. }
  37. try {
  38. let { encryptedData, iv, appid, openid } = await getUserProfile();
  39. let res = await wx.$request({
  40. method: "post", url: "/account/setUserInfo/", data: {
  41. appid, openid, iv, encryptedData
  42. },
  43. });
  44. let { face, name } = res.data;
  45. this.setData({ login: { state: name, imgurl: face } });
  46. app.globalData.mustArg = { ...app.globalData.mustArg, encryptedData, iv };
  47. app.globalData.user = { ...app.globalData.user, face, name }
  48. } catch (err) { wx.$err(err) }
  49. },
  50. // 功能事件
  51. onClickFunc (e) {
  52. let { index } = e.currentTarget.dataset, { items } = this.data;
  53. wx.getStorageSync('token') ?
  54. items[index]['success'].bind(this)() :
  55. Dialg.confirm({ message: "您尚未登录,不能操作该功能!", confirmButtonText: '微信快捷登录', confirmButtonOpenType: "getPhoneNumber" }).then(res => {
  56. }).catch(() => { })
  57. ;
  58. },
  59. // 登录
  60. async getphonenumber (e) {
  61. try {
  62. if (!e.detail.hasOwnProperty('iv')) return false;
  63. let { encryptedData, iv } = e.detail, { appId: appid } = app.globalData.mustArg, { openid } = app.globalData.user;
  64. let res = await wx.$request({ url: "/account/wxbind/", data: { appid, openid, encryptedData, iv }, method: "post" });
  65. let { face, name, tel, token, type, user_id } = res.data;
  66. wx.setStorageSync("token", token ? 'JWT ' + token : '');
  67. let login = {
  68. imgurl: token ? face : app.globalData.mustArg.userInfo.avatarUrl,
  69. name: token ? name : "微信快捷登录",
  70. state: token ? true : false
  71. };
  72. this.setData({ login });
  73. app.globalData.user = { ...app.globalData.user, face, name, tel, token, type, user_id };
  74. } catch (err) { wx.$err(err) }
  75. },
  76. onSelect (event) {
  77. },
  78. onLoad: function (options) {
  79. },
  80. onReady: function () {
  81. },
  82. onShow: function () {
  83. let login = {
  84. imgurl: (wx.getStorageSync('token') && app.globalData.user.face) ? app.globalData.user.face : app.globalData.mustArg.userInfo.avatarUrl,
  85. name: wx.getStorageSync('token') ? app.globalData.user.name : "微信快捷登录",
  86. state: wx.getStorageSync('token') ? true : false
  87. };
  88. this.setData({ login });
  89. },
  90. onHide: function () {
  91. },
  92. onUnload: function () {
  93. },
  94. onPullDownRefresh: function () {
  95. },
  96. onReachBottom: function () {
  97. },
  98. onShareAppMessage: function () {
  99. }
  100. })