Преглед изворни кода

微信授权登录,不需要获取手机号

Xiao_123 пре 1 месец
родитељ
комит
c59f5fd46d
1 измењених фајлова са 25 додато и 7 уклоњено
  1. 25 7
      layout/components/authModal/login/index.vue

+ 25 - 7
layout/components/authModal/login/index.vue

@@ -6,14 +6,13 @@
       <view class="ss-m-t-100">
         <view>
           <button
-            v-if="!protocol"
             class="send-button MiSans-Medium"
-            @click="showProtocolToast"
+            @click="handleLogin"
             >
             微信授权注册/登录
           </button>
-          <button 
-          v-else
+          <!-- <button 
+            v-else
             class="send-button MiSans-Medium"
             :loading="phoneNumberLoading"
             :disabled="phoneNumberLoading"
@@ -21,7 +20,7 @@
             @getphonenumber="getPhoneNumber"
             >
             微信授权注册/登录
-          </button>
+          </button> -->
         </view>
 
         <view class="ss-flex ss-row-center ss-m-y-50">
@@ -60,8 +59,27 @@ const handleToDetail = (type) => {
   })
 }
 
-const showProtocolToast = () => {
-  uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
+// 授权
+const handleLogin = () => {
+  if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
+  phoneNumberLoading.value = true
+  wx.login({
+    success: async (res) => {
+      try {
+        const { result } = await weChatRegister({ wechat_code: res?.code })
+        console.log(result, '用户注册返回结果')
+        uni.setStorageSync('wechat_user', result)
+        uni.$emit && uni.$emit('auth:login', result)
+        closeAuthModal()
+      } finally {
+        phoneNumberLoading.value = false
+      }
+    },
+    fail:(res)=> { 
+      phoneNumberLoading.value = false
+      console.log("获取登录凭证code失败!", res)
+    }
+  })
 }
 
 // 微信登录/注册