mp-weixin.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="container">
  3. <view class="wechatapp">
  4. <view class="header">
  5. <!-- <open-data class="avatar" type="userAvatarUrl"></open-data> -->
  6. <image class="image" :src="storeInfo && storeInfo.image_url ? storeInfo.image_url.replace('http://','https://') : '/static/default-avatar.png'"></image>
  7. </view>
  8. </view>
  9. <view class="auth-title">申请获取以下权限</view>
  10. <view class="auth-subtitle">获得你的公开信息(昵称、头像等)</view>
  11. <view class="login-btn">
  12. <!-- 获取微信用户信息 -->
  13. <button class="button btn-normal" @click.stop="getUserProfile">授权登录</button>
  14. </view>
  15. <view class="no-login-btn">
  16. <button class="button btn-normal" @click="handleCancel">暂不登录</button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import store from '@/store'
  22. import { isEmpty } from '@/utils/util'
  23. import SettingModel from '@/common/model/Setting'
  24. export default {
  25. data() {
  26. return {
  27. // 商城基本信息
  28. storeInfo: undefined,
  29. // 微信小程序登录凭证 (code)
  30. // 提交到后端,用于换取openid
  31. code: ''
  32. }
  33. },
  34. created() {
  35. // 获取商城基本信息
  36. this.getStoreInfo()
  37. },
  38. methods: {
  39. // 获取商城基本信息
  40. getStoreInfo() {
  41. SettingModel.item('store').then(store => this.storeInfo = store)
  42. // SettingModel.h5Url(true)
  43. },
  44. // 获取code
  45. // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
  46. getCode() {
  47. return new Promise((resolve, reject) => {
  48. uni.login({
  49. provider: 'weixin',
  50. success: res => {
  51. console.log('code', res.code)
  52. resolve(res.code)
  53. },
  54. fail: reject
  55. })
  56. })
  57. },
  58. // 获取微信用户信息(新版)
  59. getUserProfile() {
  60. const app = this
  61. wx.canIUse('getUserProfile') && wx.getUserProfile({
  62. lang: 'zh_CN',
  63. desc: '获取用户相关信息',
  64. success({ userInfo }) {
  65. console.log('用户同意了授权')
  66. console.log('userInfo:', userInfo)
  67. // 授权成功事件
  68. app.onAuthSuccess(userInfo)
  69. },
  70. fail() {
  71. console.log('用户拒绝了授权')
  72. }
  73. })
  74. },
  75. // 授权成功事件
  76. // 这里分为两个逻辑:
  77. // 1.将code和userInfo提交到后端,如果存在该用户 则实现自动登录,无需再填写手机号
  78. // 2.如果不存在该用户, 则显示注册页面, 需填写手机号
  79. // 3.如果后端报错了, 则显示错误信息
  80. async onAuthSuccess(userInfo) {
  81. const app = this
  82. // 提交到后端
  83. store.dispatch('LoginMpWx', {
  84. partyData: {
  85. code: await app.getCode(),
  86. oauth: 'MP-WEIXIN',
  87. userInfo
  88. },
  89. refereeId: store.getters.refereeId
  90. })
  91. .then(result => {
  92. // 一键登录成功
  93. app.$toast(result.message)
  94. // 相应全局事件订阅: 刷新上级页面数据
  95. uni.$emit('syncRefresh', true)
  96. // 跳转回原页面
  97. setTimeout(() => app.onNavigateBack(), 2000)
  98. })
  99. .catch(err => {
  100. const resultData = err.result.data
  101. // 显示错误信息
  102. if (isEmpty(resultData)) {
  103. app.$toast(err.result.message)
  104. }
  105. // 跳转回原页面
  106. if (resultData.isBack) {
  107. setTimeout(() => app.onNavigateBack(1), 2000)
  108. }
  109. // 判断还需绑定手机号
  110. if (resultData.isBindMobile) {
  111. app.onEmitSuccess(userInfo)
  112. }
  113. })
  114. },
  115. // 将oauth提交给父级
  116. // 这里要重新获取code, 因为上一次获取的code不能复用(会报错)
  117. async onEmitSuccess(userInfo) {
  118. const app = this
  119. app.$emit('success', {
  120. oauth: 'MP-WEIXIN', // 第三方登录类型: MP-WEIXIN
  121. code: await app.getCode(), // 微信登录的code, 用于换取openid
  122. userInfo // 微信用户信息
  123. })
  124. },
  125. /**
  126. * 暂不登录
  127. */
  128. handleCancel() {
  129. // 跳转回原页面
  130. this.onNavigateBack()
  131. },
  132. /**
  133. * 登录成功-跳转回原页面
  134. */
  135. onNavigateBack(delta = 1) {
  136. const pages = getCurrentPages()
  137. if (pages.length > 1) {
  138. uni.navigateBack({
  139. delta: Number(delta || 1)
  140. })
  141. } else {
  142. this.$navTo('pages/index/index')
  143. }
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .container {
  150. padding: 0 60rpx;
  151. font-size: 32rpx;
  152. background: #fff;
  153. min-height: 100vh;
  154. }
  155. .wechatapp {
  156. padding: 80rpx 0 48rpx;
  157. border-bottom: 1rpx solid #e3e3e3;
  158. margin-bottom: 72rpx;
  159. text-align: center;
  160. .header {
  161. width: 190rpx;
  162. height: 190rpx;
  163. border: 4rpx solid #fff;
  164. margin: 0 auto 0;
  165. border-radius: 50%;
  166. overflow: hidden;
  167. box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
  168. .image {
  169. display: block;
  170. width: 100%;
  171. height: 100%;
  172. }
  173. }
  174. }
  175. .auth-title {
  176. color: #585858;
  177. font-size: 34rpx;
  178. margin-bottom: 40rpx;
  179. }
  180. .auth-subtitle {
  181. color: #888;
  182. margin-bottom: 88rpx;
  183. font-size: 28rpx;
  184. }
  185. .login-btn {
  186. padding: 0 20rpx;
  187. .button {
  188. height: 88rpx;
  189. background: #04be01;
  190. color: #fff;
  191. font-size: 30rpx;
  192. border-radius: 999rpx;
  193. display: flex;
  194. justify-content: center;
  195. align-items: center;
  196. }
  197. }
  198. .no-login-btn {
  199. margin-top: 20rpx;
  200. padding: 0 20rpx;
  201. .button {
  202. height: 88rpx;
  203. background: #dfdfdf;
  204. color: #fff;
  205. font-size: 30rpx;
  206. border-radius: 999rpx;
  207. display: flex;
  208. justify-content: center;
  209. align-items: center;
  210. }
  211. }
  212. </style>