index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <!-- wx9bf5de3fd1a15538 -->
  3. <!-- wx99bd3af055be9f6d -->
  4. <view class="login">
  5. <!-- 头部功能组件 -->
  6. <cu-loadding :show="showloading" />
  7. <!-- header -->
  8. <view @click="userfunc" class="header">
  9. <cu-custom bgColor="header-blue"><view v-if="show" slot="content">设置</view></cu-custom>
  10. <cu-login-userinfo :userImage="userinfo.face" :userName="userinfo.name" />
  11. </view>
  12. <!-- section -->
  13. <view class="bg-white">
  14. <view class="cu-list menu card-menu shadow-lg radius">
  15. <view @click="navfunc('/pages/information/index', true)" class="cu-item arrow">
  16. <view class="content flex align-center">
  17. <van-icon name="info-o" />
  18. <text class="margin-left-sm">设备信息</text>
  19. </view>
  20. </view>
  21. <view @click="navfunc('/pages/status/index', true)" class="cu-item arrow">
  22. <view class="content flex align-center">
  23. <van-icon name="hotel-o" />
  24. <text class="margin-left-sm">设备状态</text>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="bg-white margin-top">
  30. <view class="cu-list menu card-menu shadow-lg radius">
  31. <view @click="navfunc('/pages/privacy/index')" class="cu-item arrow ">
  32. <view class="content flex align-center">
  33. <van-icon name="shield-o" />
  34. <text class="margin-left-sm">隐私声明</text>
  35. </view>
  36. </view>
  37. <view @click="show = true" class="cu-item arrow ">
  38. <view class="content flex align-center">
  39. <van-icon name="setting-o" />
  40. <text class="margin-left-sm">设置</text>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- footer -->
  46. <!-- 功能栏 -->
  47. <van-action-sheet :show="show" :actions="actions" @close="show = false" @select="onSelect" @getphonenumber="onGetUserInfo" />
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. showloading: true, //加载
  55. show: false, //设置
  56. actions: [],
  57. userinfo: {
  58. face: '',
  59. name: ''
  60. }
  61. };
  62. },
  63. onLoad() {
  64. this.initRequest();
  65. },
  66. methods: {
  67. // 报错
  68. showErr(content) {
  69. content = JSON.stringify(content) === '{}' ? '未知错误' : content;
  70. uni.showModal({
  71. content,
  72. showCancel: false,
  73. success: res => console.log(res)
  74. });
  75. },
  76. // 进入页面请求
  77. async initRequest() {
  78. try {
  79. var { appid, code } = await this.codeAppid();
  80. // codeSession
  81. var res = await this.$global.request({
  82. url: '/customer/code2Session/',
  83. method: 'post',
  84. data: {
  85. code,
  86. appid
  87. }
  88. });
  89. if (res.data.token) uni.setStorageSync('token', 'JWT ' + res.data.token);
  90. this.$store.commit('userInfo', { appid, code, ...res.data });
  91. // 设置
  92. this.changeUser();
  93. } catch (err) {
  94. console.log(err);
  95. this.showErr(err);
  96. } finally {
  97. this.showloading = false;
  98. }
  99. },
  100. // 获取 code appid
  101. codeAppid() {
  102. return new Promise((resolve, reject) => {
  103. const appid = uni.getAccountInfoSync().miniProgram.appId;
  104. uni.login({ success, fail: err => reject(err) });
  105. function success(res) {
  106. var { code } = res;
  107. resolve({ code, appid });
  108. }
  109. });
  110. },
  111. // 头像点击事件
  112. userfunc() {
  113. if (!uni.getStorageSync('token')) this.show = true;
  114. },
  115. // 更新用户显示资料
  116. changeUser() {
  117. this.userinfo = {
  118. face: this.$store.state.userInfo.face || 'https://img.yzcdn.cn/vant/cat.jpeg',
  119. name: this.$store.state.userInfo.name || '昵称'
  120. };
  121. this.actions = [
  122. { name: '微信快捷登录', color: uni.getStorageSync('token') ? '#666666' : '#39b54a', disabled: uni.getStorageSync('token'), openType: 'getPhoneNumber' },
  123. { name: '更新资料', disabled: !uni.getStorageSync('token') }
  124. ];
  125. },
  126. // 微信快捷登录
  127. async onGetUserInfo(e) {
  128. try {
  129. var { encryptedData, iv } = e.detail;
  130. var { openid, appid } = this.$store.state.userInfo;
  131. var res = await this.$global.request({ url: '/customer/wxbind/', method: 'post', data: { openid, appid, encryptedData, iv } });
  132. if (res.data.token) uni.setStorageSync('token', 'JWT ' + res.data.token);
  133. this.$store.commit('userInfo', res.data);
  134. this.changeUser();
  135. uni.showToast({
  136. icon: 'none',
  137. title: '登录成功'
  138. });
  139. } catch (err) {
  140. this.showErr(err);
  141. } finally {
  142. this.show = false;
  143. }
  144. },
  145. // 获取用户信息权限
  146. getUserInfoRoot() {
  147. return new Promise((resolve, reject) => {
  148. uni.getUserProfile({
  149. desc: '用于完善会员资料',
  150. success: res => resolve(res),
  151. fail: err => reject(err)
  152. });
  153. });
  154. },
  155. // 跳转
  156. navfunc(url, status = false) {
  157. if (status && !uni.getStorageSync('token')) {
  158. this.show = true;
  159. return false;
  160. }
  161. uni.navigateTo({ url });
  162. },
  163. // 更新资料
  164. async updateUserInfo() {
  165. // customer/setUserInfo/
  166. try {
  167. var res = await this.getUserInfoRoot();
  168. var { iv, encryptedData } = res;
  169. res = await this.$global.request({
  170. url: '/customer/setinfo/',
  171. data: {
  172. iv,
  173. encryptedData,
  174. appid: this.$store.state.userInfo.appid,
  175. openid: this.$store.state.userInfo.openid
  176. },
  177. method: 'post',
  178. header: {
  179. 'Content-Type': 'application/x-www-form-urlencoded',
  180. Authorization: uni.getStorageSync('token')
  181. }
  182. });
  183. this.$store.commit('userInfo', res.data);
  184. this.changeUser();
  185. uni.showToast({
  186. icon: 'none',
  187. title: '更新成功'
  188. });
  189. } catch (err) {
  190. this.showErr(err);
  191. }
  192. },
  193. // 选中
  194. onSelect(event) {
  195. switch (event.detail.name) {
  196. case '更新资料':
  197. this.updateUserInfo();
  198. break;
  199. }
  200. }
  201. }
  202. };
  203. </script>
  204. <style lang="scss" scoped>
  205. .login {
  206. }
  207. </style>