index.js 3.6 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. })
  34. })
  35. }
  36. try {
  37. let { encryptedData, iv, appid, openid } = await getUserProfile();
  38. let res = await wx.$request({
  39. method: "post", url: "/account/setUserInfo/", data: {
  40. appid, openid, iv, encryptedData
  41. },
  42. });
  43. let { face, name } = res.data;
  44. this.setData({ login: { state: name, imgurl: face } });
  45. app.globalData.mustArg = { ...app.globalData.mustArg, encryptedData, iv };
  46. app.globalData.user = { ...app.globalData.user, face, name }
  47. } catch (err) { wx.$err(err) }
  48. },
  49. // 功能事件
  50. onClickFunc (e) {
  51. let { index } = e.currentTarget.dataset, { items } = this.data;
  52. wx.getStorageSync('token') ?
  53. items[index]['success'].bind(this)() :
  54. Dialg.confirm({ message: "您尚未登录,不能操作该功能!", confirmButtonText: '微信快捷登录', confirmButtonOpenType: "getPhoneNumber" }).then(res => {
  55. }).catch(() => { })
  56. ;
  57. },
  58. // 登录
  59. async getphonenumber (e) {
  60. try {
  61. if (!e.detail.hasOwnProperty('iv')) return false;
  62. let { encryptedData, iv } = e.detail, { appId: appid } = app.globalData.mustArg, { openid } = app.globalData.user;
  63. let res = await wx.$request({ url: "/account/wxbind/", data: { appid, openid, encryptedData, iv }, method: "post" });
  64. let { face, name, tel, token, type, user_id } = res.data;
  65. wx.setStorageSync("token", token ? 'JWT ' + token : '');
  66. let login = {
  67. imgurl: token ? face : app.globalData.mustArg.userInfo.avatarUrl,
  68. name: token ? name : "微信快捷登录",
  69. state: token ? true : false
  70. };
  71. this.setData({ login });
  72. app.globalData.user = { ...app.globalData.user, face, name, tel, token, type, user_id };
  73. } catch (err) { wx.$err(err) }
  74. },
  75. onSelect (event) {
  76. },
  77. onLoad: function (options) {
  78. },
  79. onReady: function () {
  80. },
  81. onShow: function () {
  82. let login = {
  83. imgurl: (wx.getStorageSync('token') && app.globalData.user.face) ? app.globalData.user.face : app.globalData.mustArg.userInfo.avatarUrl,
  84. name: wx.getStorageSync('token') ? app.globalData.user.name : "微信快捷登录",
  85. state: wx.getStorageSync('token') ? true : false
  86. };
  87. this.setData({ login });
  88. },
  89. onHide: function () {
  90. },
  91. onUnload: function () {
  92. },
  93. onPullDownRefresh: function () {
  94. },
  95. onReachBottom: function () {
  96. },
  97. onShareAppMessage: function () {
  98. }
  99. })