phone-user.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <view class="wx-login-page" :style="{backgroundImage: bgImg}">
  3. <cu-custom :isBack="false">
  4. <block slot="content">登录</block>
  5. </cu-custom>
  6. <view class="flex justify-center m-t-40">
  7. <u--image class="logo-image" :width="300+'rpx'" :height="235+'rpx'" :showLoading="true" :src="logoImg"
  8. :fade="true" duration="100" mode="aspectFit"></u--image>
  9. </view>
  10. <view class="cu-card case" style="margin-top: 20rpx;">
  11. <view class="cu-item shadow" style="min-height: 800rpx;">
  12. <view class="cu-list m-t-100">
  13. <view class="cu-title font-45 m-l-30">
  14. Hello!
  15. </view>
  16. <view class="cu-title font-40 m-t-20 m-l-30">
  17. {{loginAppName}}
  18. </view>
  19. <view class="cuIcon-title text-gray m-t-30 p-l-30 p-r-30">申请获取您微信绑定的手机号</view>
  20. <view class="flex flex-direction m-t-70 p-l-50 p-r-50">
  21. <button class="cu-btn margin-tb-sm lg bg-blue" open-type="getPhoneNumber"
  22. @getphonenumber="getPhoneNumber">手机号授权登录
  23. </button>
  24. </view>
  25. <view class="flex flex-direction p-l-50 p-r-50">
  26. <button class="cu-btn margin-tb-sm lg " @click="backToPage">暂不登录</button>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="cu-modal" :class="modalShow ? 'show' : ''">
  32. <view class="cu-dialog">
  33. <view class="cu-bar bg-white justify-end">
  34. <view class="content">微信授权</view>
  35. <view class="action" @tap="hideModal">
  36. <text class="cuIcon-close text-gray"></text>
  37. </view>
  38. </view>
  39. <view class="bg-white flex justify-start text-left p-t-20 p-l-35 p-r-35 p-b-40">
  40. <view class="m-b-15">本系统需要获得以下权限</view>
  41. <u-line></u-line>
  42. <view class="cuIcon-title text-gray m-t-15">申请获取您微信的用户信息,以便于更好的为您服务</view>
  43. </view>
  44. <u-line></u-line>
  45. <view class="cu-bar bg-white justify-end p-t-26 p-b-26">
  46. <view class="action">
  47. <button class="cu-btn line-gray text-black" @tap="hideModal">取消</button>
  48. <button class="cu-btn bg-blue margin-left" v-if="canIUseGetUserProfile"
  49. @click="getUserProfile">确认授权</button>
  50. <button class="cu-btn bg-blue margin-left" v-else open-type="getUserInfo"
  51. @getuserinfo="agreeGetUser" lang="zh_CN">确认授权</button>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. const app = getApp();
  60. import api from 'utils/api'
  61. import util from 'utils/util'
  62. import validate from 'utils/validate'
  63. import __config from '@/config/env'; // 配置文件
  64. const wxPhoneInfoKey = 'wx_phone_info';
  65. export default {
  66. name: 'UserPhone',
  67. data() {
  68. return {
  69. loginAppName: __config.loginAppName,
  70. theme: app.globalData.theme, //全局颜色变量
  71. CustomBar: this.CustomBar,
  72. bgImg: util.spliceBgImgUrl('/login-head-bg.png'),
  73. logoImg: util.spliceAssetsUrl('/login-logo.png'),
  74. wxSession: {},
  75. canIUseGetUserProfile: false,
  76. modalShow: false,
  77. reUrl: '/pages/home/index',
  78. };
  79. },
  80. components: {},
  81. onLoad(options) {
  82. if (options.reUrl) {
  83. this.reUrl = decodeURIComponent(options.reUrl)
  84. }
  85. if (uni.getUserProfile) {
  86. this.canIUseGetUserProfile = true
  87. }
  88. this.wxSession = uni.getStorageSync(__config.loginWxSessionKey)
  89. },
  90. onShow() {},
  91. methods: {
  92. /**
  93. * 返回来源页面
  94. */
  95. backToPage() {
  96. uni.reLaunch({
  97. url: this.reUrl
  98. })
  99. },
  100. //获取微信手机号
  101. getPhoneNumber(e) {
  102. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  103. let params = Object.assign(e.detail, this.wxSession)
  104. console.log('getPhoneNumber', e.detail)
  105. api.doPost('/openapi/ma/phone_user/decryptPhoneNumber', params).then(res => {
  106. if (res.code == 200) {
  107. let phoneInfoData = res.data;
  108. uni.setStorageSync(wxPhoneInfoKey, phoneInfoData);
  109. this.modalShow = true
  110. } else {
  111. uni.showToast({
  112. title: res.msg,
  113. })
  114. }
  115. });
  116. }
  117. },
  118. agreeGetUser(e) {
  119. if (e.detail.errMsg == 'getUserInfo:ok') {
  120. let phoneInfoData = uni.getStorageSync(wxPhoneInfoKey)
  121. let params = Object.assign(e.detail, this.wxSession, {
  122. wxMaPhoneNumberInfo: phoneInfoData
  123. })
  124. console.log('agreeGetUser', e.detail)
  125. api.doPost('/openapi/ma/phone_user/decryptUserInfo', params).then(res => {
  126. let loginData = res.data;
  127. if (loginData.isLogin) {
  128. uni.setStorageSync(__config.tokenNameKey, loginData.tokenName);
  129. uni.setStorageSync(__config.tokenValueKey, loginData.tokenValue);
  130. /**
  131. * 缓存用户信息
  132. */
  133. let wxMemberInfo = util.parseUser(loginData.userInfo)
  134. uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
  135. uni.reLaunch({
  136. url: this.reUrl,
  137. success: () => {
  138. uni.removeStorageSync(wxPhoneInfoKey)
  139. uni.removeStorageSync(__config.loginWxSessionKey)
  140. }
  141. })
  142. }
  143. });
  144. }
  145. },
  146. getUserProfile(e) {
  147. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  148. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  149. wx.getUserProfile({
  150. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  151. success: (detail) => {
  152. let phoneInfoData = uni.getStorageSync(wxPhoneInfoKey)
  153. let params = Object.assign(detail, this.wxSession, {
  154. wxMaPhoneNumberInfo: phoneInfoData
  155. })
  156. console.log('getUserProfile', detail)
  157. api.doPost('/openapi/ma/phone_user/decryptUserInfo', params).then(res => {
  158. let loginData = res.data;
  159. if (loginData.isLogin) {
  160. uni.setStorageSync(__config.tokenNameKey, loginData.tokenName);
  161. uni.setStorageSync(__config.tokenValueKey, loginData.tokenValue);
  162. /**
  163. * 缓存用户信息
  164. */
  165. let wxMemberInfo = util.parseUser(loginData.userInfo)
  166. uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
  167. uni.reLaunch({
  168. url: this.reUrl,
  169. success: () => {
  170. uni.removeStorageSync(wxPhoneInfoKey)
  171. uni.removeStorageSync(__config.loginWxSessionKey)
  172. }
  173. })
  174. }
  175. });
  176. }
  177. })
  178. },
  179. /**
  180. * 隐藏弹出层
  181. */
  182. hideModal() {
  183. this.modalShow = false
  184. },
  185. }
  186. };
  187. </script>
  188. <style lang="scss" scoped>
  189. page {
  190. background-color: #FFFFFF;
  191. }
  192. .wx-login-page {
  193. background-repeat: no-repeat;
  194. background-size: 100%;
  195. }
  196. </style>