mp-weixin-mobile.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <!-- 微信授权手机号一键登录 -->
  3. <view class="wechat-auth">
  4. <button class="btn-normal" open-type="getPhoneNumber" @getphonenumber="handelMpWeixinMobileLogin($event)" @click="clickPhoneNumber">
  5. <view class="wechat-auth-container">
  6. <image class="icon" src="../../../static/channel/wechat.png"></image>
  7. <text class="title">手机号快捷登录</text>
  8. </view>
  9. </button>
  10. </view>
  11. </template>
  12. <script>
  13. import store from '@/store'
  14. import { isEmpty, inArray } from '@/utils/util'
  15. export default {
  16. props: {
  17. // 是否存在第三方用户信息
  18. isParty: {
  19. type: Boolean,
  20. default: () => false
  21. },
  22. // 第三方用户信息数据
  23. partyData: {
  24. type: Object
  25. }
  26. },
  27. data() {
  28. return {
  29. // 微信小程序登录凭证 (code)
  30. // 提交到后端,用于换取openid
  31. code: ''
  32. }
  33. },
  34. methods: {
  35. // 按钮点击事件: 获取微信手机号按钮
  36. // 实现目的: 在getphonenumber事件触发之前获取微信登录code
  37. // 因为如果在getphonenumber事件中获取code的话,提交到后端的encryptedData会存在解密不了的情况
  38. async clickPhoneNumber() {
  39. this.code = await this.getCode()
  40. },
  41. // 微信授权获取手机号一键登录
  42. // getphonenumber事件的回调方法
  43. async handelMpWeixinMobileLogin({ detail }) {
  44. const app = this
  45. if (detail.errMsg != 'getPhoneNumber:ok') {
  46. console.log('微信授权获取手机号失败', detail)
  47. return
  48. }
  49. if (detail.errMsg == 'getPhoneNumber:ok') {
  50. app.isLoading = true
  51. store.dispatch('LoginMpWxMobile', {
  52. code: app.code,
  53. encryptedData: detail.encryptedData,
  54. iv: detail.iv,
  55. isParty: app.isParty,
  56. partyData: app.partyData
  57. })
  58. .then(result => {
  59. // 显示登录成功
  60. app.$toast(result.message)
  61. // 相应全局事件订阅: 刷新上级页面数据
  62. uni.$emit('syncRefresh', true)
  63. // 跳转回原页面
  64. setTimeout(() => app.onNavigateBack(1), 2000)
  65. })
  66. .catch(err => {
  67. const resultData = err.result.data
  68. })
  69. .finally(() => app.isLoading = false)
  70. }
  71. },
  72. // 获取微信登录的code
  73. // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
  74. getCode() {
  75. return new Promise((resolve, reject) => {
  76. uni.login({
  77. provider: 'weixin',
  78. success: res => {
  79. console.log('code', res.code)
  80. resolve(res.code)
  81. },
  82. fail: reject
  83. })
  84. })
  85. },
  86. /**
  87. * 登录成功-跳转回原页面
  88. */
  89. onNavigateBack(delta = 1) {
  90. const pages = getCurrentPages()
  91. if (pages.length > 1) {
  92. uni.navigateBack({
  93. delta: Number(delta || 1)
  94. })
  95. } else {
  96. this.$navTo('pages/index/index')
  97. }
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. // 微信授权登录
  104. .wechat-auth {
  105. width: 320rpx;
  106. margin: 50rpx auto 0 auto;
  107. .wechat-auth-container {
  108. display: flex;
  109. justify-content: center;
  110. }
  111. .icon {
  112. width: 38rpx;
  113. height: 38rpx;
  114. margin-right: 15rpx;
  115. }
  116. .title {
  117. font-size: 28rpx;
  118. color: #666666;
  119. }
  120. }
  121. </style>