mp-weixin.vue 5.0 KB

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