only-user.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 style="width: 100%;" class="cu-btn margin-tb-sm lg bg-blue" v-if="canIUseGetUserProfile"
  22. @click="getUserProfile">微信授权登录</button>
  23. <button style="width: 100%;" class="cu-btn margin-tb-sm lg bg-blue" v-else
  24. open-type="getUserInfo" @getuserinfo="agreeGetUser" lang="zh_CN">微信授权登录</button>
  25. </view>
  26. <view class="flex flex-direction p-l-50 p-r-50">
  27. <button class="cu-btn margin-tb-sm lg " @click="backToPage">暂不登录</button>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. const app = getApp();
  36. import api from 'utils/api'
  37. import util from 'utils/util'
  38. import validate from 'utils/validate'
  39. import __config from '@/config/env'; // 配置文件
  40. export default {
  41. name: 'OnlyUser',
  42. data() {
  43. return {
  44. loginAppName: __config.loginAppName,
  45. theme: app.globalData.theme, //全局颜色变量
  46. CustomBar: this.CustomBar,
  47. bgImg: util.spliceBgImgUrl('/login-head-bg.png'),
  48. logoImg: util.spliceAssetsUrl('/login-logo.png'),
  49. wxSession: {},
  50. canIUseGetUserProfile: false,
  51. modalShow: false,
  52. reUrl: '/pages/home/index',
  53. };
  54. },
  55. components: {},
  56. onLoad(options) {
  57. console.info('onle-user: options => ', JSON.stringify(options))
  58. if (options.reUrl) {
  59. this.reUrl = decodeURIComponent(options.reUrl)
  60. }
  61. console.info('onle-user: this.reUrl => ', this.reUrl)
  62. if (uni.getUserProfile) {
  63. this.canIUseGetUserProfile = true
  64. }
  65. this.wxSession = uni.getStorageSync(__config.loginWxSessionKey)
  66. },
  67. onShow() {},
  68. methods: {
  69. /**
  70. * 返回来源页面
  71. */
  72. backToPage() {
  73. uni.reLaunch({
  74. url: this.reUrl
  75. })
  76. },
  77. agreeGetUser(e) {
  78. if (e.detail.errMsg == 'getUserInfo:ok') {
  79. let params = Object.assign(e.detail, this.wxSession)
  80. api.doPost('/openapi/ma/only_user/decryptUserInfo', params).then(res => {
  81. let loginData = res.data;
  82. if (loginData.isLogin) {
  83. uni.setStorageSync(__config.tokenNameKey, loginData.tokenName);
  84. uni.setStorageSync(__config.tokenValueKey, loginData.tokenValue);
  85. /**
  86. * 缓存用户信息
  87. */
  88. let wxMemberInfo = util.parseUser(loginData.userInfo)
  89. uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
  90. uni.reLaunch({
  91. url: this.reUrl,
  92. success: () => {
  93. uni.removeStorageSync(__config.loginWxSessionKey)
  94. }
  95. })
  96. }
  97. });
  98. }
  99. },
  100. getUserProfile(e) {
  101. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  102. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  103. wx.getUserProfile({
  104. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  105. success: (detail) => {
  106. let params = Object.assign(detail, this.wxSession)
  107. console.log('getUserProfile', detail)
  108. api.doPost('/openapi/ma/only_user/decryptUserInfo', params).then(res => {
  109. let loginData = res.data;
  110. if (loginData.isLogin) {
  111. uni.setStorageSync(__config.tokenNameKey, loginData.tokenName);
  112. uni.setStorageSync(__config.tokenValueKey, loginData.tokenValue);
  113. /**
  114. * 缓存用户信息
  115. */
  116. let wxMemberInfo = util.parseUser(loginData.userInfo)
  117. uni.setStorageSync(__config.loginWxUserInfoKey, wxMemberInfo)
  118. uni.reLaunch({
  119. url: this.reUrl,
  120. success: () => {
  121. uni.removeStorageSync(__config.loginWxSessionKey)
  122. }
  123. })
  124. }
  125. });
  126. }
  127. })
  128. },
  129. }
  130. };
  131. </script>
  132. <style lang="scss" scoped>
  133. page {
  134. background-color: #FFFFFF;
  135. }
  136. .wx-login-page {
  137. background-repeat: no-repeat;
  138. background-size: 100%;
  139. }
  140. </style>