Răsfoiți Sursa

feat(login): 微信登录自动获取手机号

登录按钮增加open-type=getPhoneNumber,捕获phoneCode传递给后端wechatLogin,实现首次登录自动绑定微信手机号

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Sisyphus 3 luni în urmă
părinte
comite
a769a48c87
2 a modificat fișierele cu 19 adăugiri și 4 ștergeri
  1. 13 2
      cfc-frontend/pages/login/login.vue
  2. 6 2
      cfc-frontend/utils/api.js

+ 13 - 2
cfc-frontend/pages/login/login.vue

@@ -31,7 +31,8 @@
         <button
           class="wechat-btn"
           :loading="loading"
-          @click="handleWechatLogin"
+          open-type="getPhoneNumber"
+          @getphonenumber="onGetPhoneNumber"
         >
           <text class="wechat-icon">微信</text>
           <text>一键登录</text>
@@ -63,6 +64,7 @@ export default {
       agreed: false,
       nickname: '',
       avatar: '',
+      phoneCode: '',
       redirectUrl: ''
     }
   },
@@ -94,6 +96,13 @@ export default {
         url: '/pages/policy/privacy'
       })
     },
+    onGetPhoneNumber(e) {
+      if (e.detail && e.detail.code) {
+        this.phoneCode = e.detail.code
+      }
+      // 无论是否授权手机号,都执行微信登录
+      this.handleWechatLogin()
+    },
     handleWechatLogin() {
       if (!this.agreed) {
         uni.showToast({ title: '请先同意用户协议', icon: 'none' })
@@ -117,10 +126,12 @@ export default {
           fail: function() { reject(new Error('微信登录失败')) }
         })
       })]).then(function(loginRes) {
+        var phoneCodeToSend = self.phoneCode
+        self.phoneCode = '' // 使用后清空
         wechatLogin(loginRes.code, {
           nickname: self.nickname || '用户',
           avatar: self.avatar || ''
-        }).then(function(res) {
+        }, phoneCodeToSend).then(function(res) {
           // 保存 token 和用户信息(只写一次)
           uni.setStorageSync('token', res.data.token)
           uni.setStorageSync('userId', res.data.userId)

+ 6 - 2
cfc-frontend/utils/api.js

@@ -140,8 +140,12 @@ function _fallbackToDify(path, method, data, options, resolve, reject) {
 }
 
 // 登录模块
-export const wechatLogin = (code, userInfo) => {
-  return request('/api/auth/wechat-login', 'POST', { code, ...userInfo })
+export const wechatLogin = (code, userInfo, phoneCode) => {
+  var data = { code: code, nickname: userInfo.nickname, avatar: userInfo.avatar }
+  if (phoneCode) {
+    data.phoneCode = phoneCode
+  }
+  return request('/api/auth/wechat-login', 'POST', data)
 }
 
 // 发送验证码