Просмотр исходного кода

feat(登录): 小程序端微信登录强制获取手机号;H5 保留静默登录

Xiaogang Liao 1 месяц назад
Родитель
Сommit
30253945bb
1 измененных файлов с 44 добавлено и 1 удалено
  1. 44 1
      train-frontend/pages/index/index.vue

+ 44 - 1
train-frontend/pages/index/index.vue

@@ -126,9 +126,16 @@
             <text class="sheet-title">欢迎回来</text>
             <text class="sheet-sub">登录以解锁更多成长功能</text>
           </view>
+          <!-- #ifdef MP-WEIXIN -->
+          <button class="sheet-login-btn" open-type="getPhoneNumber" @getphonenumber="handleLoginPhone" :loading="loginLoading" :disabled="loginLoading">
+            微信一键登录
+          </button>
+          <!-- #endif -->
+          <!-- #ifndef MP-WEIXIN -->
           <button class="sheet-login-btn" @click="handleLogin" :loading="loginLoading" :disabled="loginLoading">
             微信一键登录
           </button>
+          <!-- #endif -->
           <view class="sheet-footer">
             <text class="sheet-footer-text">登录即代表同意服务协议</text>
           </view>
@@ -139,7 +146,7 @@
 </template>
 
 <script>
-import { wechatLogin, getCourseList, getMyEnrollments, getMyBadges, getCourseProgress, getMyCert, getSalonList } from '@/utils/api.js'
+import { wechatLogin, wechatPhoneLogin, getCourseList, getMyEnrollments, getMyBadges, getCourseProgress, getMyCert, getSalonList } from '@/utils/api.js'
 import { formatDateTime } from '@/utils/format.js'
 import { loadInviteCode, getInviteCode, buildSharePath } from '@/utils/share.js'
 import { captureInviteCode } from '@/utils/invite.js'
@@ -386,6 +393,42 @@ export default {
               self.loadInfo()
             }).catch(function(err) {
               console.error('登录失败', err)
+              uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' })
+            }).finally(function() {
+              self.loginLoading = false
+            })
+          } else {
+            uni.showToast({ title: '微信登录失败', icon: 'none' })
+            self.loginLoading = false
+          }
+        },
+        fail: function() {
+          uni.showToast({ title: '微信登录失败', icon: 'none' })
+          self.loginLoading = false
+        }
+      })
+    },
+    handleLoginPhone(e) {
+      if (this.loginLoading) return
+      var phoneCode = (e && e.detail && e.detail.code) ? e.detail.code : ''
+      if (!phoneCode) {
+        uni.showToast({ title: '请授权手机号后登录', icon: 'none' })
+        return
+      }
+      this.loginLoading = true
+      var self = this
+      wx.login({
+        success: function(res) {
+          if (res.code) {
+            wechatPhoneLogin({ code: res.code, phoneCode: phoneCode }).then(function(resp) {
+              var data = resp.data
+              self.$store.dispatch('login', data)
+              uni.showToast({ title: '登录成功', icon: 'success' })
+              self.hideLoginSheet()
+              self.loadInfo()
+            }).catch(function(err) {
+              console.error('登录失败', err)
+              uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' })
             }).finally(function() {
               self.loginLoading = false
             })