mp-weixin.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. const pages = getCurrentPages()
  124. if (pages.length > 1) {
  125. uni.navigateBack({
  126. delta: Number(delta || 1)
  127. })
  128. } else {
  129. this.$navTo('pages/index/index')
  130. }
  131. }
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .container {
  137. padding: 0 60rpx;
  138. font-size: 32rpx;
  139. background: #fff;
  140. min-height: 100vh;
  141. }
  142. .wechatapp {
  143. padding: 80rpx 0 48rpx;
  144. border-bottom: 1rpx solid #e3e3e3;
  145. margin-bottom: 72rpx;
  146. text-align: center;
  147. .header {
  148. width: 190rpx;
  149. height: 190rpx;
  150. border: 4rpx solid #fff;
  151. margin: 0 auto 0;
  152. border-radius: 50%;
  153. overflow: hidden;
  154. box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
  155. }
  156. }
  157. .auth-title {
  158. color: #585858;
  159. font-size: 34rpx;
  160. margin-bottom: 40rpx;
  161. }
  162. .auth-subtitle {
  163. color: #888;
  164. margin-bottom: 88rpx;
  165. font-size: 28rpx;
  166. }
  167. .login-btn {
  168. padding: 0 20rpx;
  169. .button {
  170. height: 88rpx;
  171. line-height: 88rpx;
  172. background: #04be01;
  173. color: #fff;
  174. font-size: 30rpx;
  175. border-radius: 999rpx;
  176. text-align: center;
  177. }
  178. }
  179. .no-login-btn {
  180. margin-top: 20rpx;
  181. padding: 0 20rpx;
  182. .button {
  183. height: 88rpx;
  184. line-height: 88rpx;
  185. background: #dfdfdf;
  186. color: #fff;
  187. font-size: 30rpx;
  188. border-radius: 999rpx;
  189. text-align: center;
  190. }
  191. }
  192. </style>