mp-weixin.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // 显示错误信息
  93. if (isEmpty(resultData)) {
  94. app.$toast(err.result.message)
  95. }
  96. // 跳转回原页面
  97. if (resultData.isBack) {
  98. setTimeout(() => app.onNavigateBack(1), 2000)
  99. }
  100. // 判断还需绑定手机号
  101. if (resultData.isBindMobile) {
  102. app.onEmitSuccess(userInfo)
  103. }
  104. })
  105. },
  106. // 将oauth提交给父级
  107. // 这里要重新获取code, 因为上一次获取的code不能复用(会报错)
  108. async onEmitSuccess(userInfo) {
  109. const app = this
  110. app.$emit('success', {
  111. oauth: 'MP-WEIXIN', // 第三方登录类型: MP-WEIXIN
  112. code: await app.getCode(), // 微信登录的code, 用于换取openid
  113. userInfo // 微信用户信息
  114. })
  115. },
  116. /**
  117. * 暂不登录
  118. */
  119. handleCancel() {
  120. // 跳转回原页面
  121. this.onNavigateBack()
  122. },
  123. /**
  124. * 登录成功-跳转回原页面
  125. */
  126. onNavigateBack(delta = 1) {
  127. const pages = getCurrentPages()
  128. if (pages.length > 1) {
  129. uni.navigateBack({
  130. delta: Number(delta || 1)
  131. })
  132. } else {
  133. this.$navTo('pages/index/index')
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .container {
  141. padding: 0 60rpx;
  142. font-size: 32rpx;
  143. background: #fff;
  144. min-height: 100vh;
  145. }
  146. .wechatapp {
  147. padding: 80rpx 0 48rpx;
  148. border-bottom: 1rpx solid #e3e3e3;
  149. margin-bottom: 72rpx;
  150. text-align: center;
  151. .header {
  152. width: 190rpx;
  153. height: 190rpx;
  154. border: 4rpx solid #fff;
  155. margin: 0 auto 0;
  156. border-radius: 50%;
  157. overflow: hidden;
  158. box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
  159. }
  160. }
  161. .auth-title {
  162. color: #585858;
  163. font-size: 34rpx;
  164. margin-bottom: 40rpx;
  165. }
  166. .auth-subtitle {
  167. color: #888;
  168. margin-bottom: 88rpx;
  169. font-size: 28rpx;
  170. }
  171. .login-btn {
  172. padding: 0 20rpx;
  173. .button {
  174. height: 88rpx;
  175. line-height: 88rpx;
  176. background: #04be01;
  177. color: #fff;
  178. font-size: 30rpx;
  179. border-radius: 999rpx;
  180. text-align: center;
  181. }
  182. }
  183. .no-login-btn {
  184. margin-top: 20rpx;
  185. padding: 0 20rpx;
  186. .button {
  187. height: 88rpx;
  188. line-height: 88rpx;
  189. background: #dfdfdf;
  190. color: #fff;
  191. font-size: 30rpx;
  192. border-radius: 999rpx;
  193. text-align: center;
  194. }
  195. }
  196. </style>