mp-weixin.vue 5.9 KB

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