瀏覽代碼

微信一键登录

lifanagju_citu 6 月之前
父節點
當前提交
13800907a4
共有 3 個文件被更改,包括 30 次插入12 次删除
  1. 12 0
      api/common.js
  2. 14 8
      layout/components/authModal/login/index.vue
  3. 4 4
      store/user.js

+ 12 - 0
api/common.js

@@ -41,6 +41,18 @@ export const smsLogin = (data) => {
   })
 }
 
+// 微信小程序的一键登录
+export const weChatLogin = (data) => {
+  return request({
+    url: '/app-api/menduner/system/auth/weixin-mini-app-login',
+    method: 'POST',
+    data,
+    custom: {
+      showLoading: false
+    }
+  })
+}
+
 // 短信登录
 export const userRegister = (data) => {
   return request({

+ 14 - 8
layout/components/authModal/login/index.vue

@@ -59,7 +59,10 @@
           </uni-forms-item>
         </uni-forms>
         <view class="quickLogon">
-          <button v-if="showWxLogon" class="wxLogon" type="text" :plain="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信登录</button>
+          <view v-if="showWxLogon">
+            <button v-if="!protocol" class="wxLogon" type="text" :plain="true" @click="showProtocolToast">微信一键登录</button>
+            <button v-else class="wxLogon" type="text" :plain="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</button>
+          </view>
           <view v-else></view>
           <view class="register" @tap="handleChangeRegister">还没有登录账户?去注册</view>
         </view>
@@ -232,10 +235,13 @@ function handleChangeLogin () {
   changeType.value = 'login'
 }
 
-const showWxLogon = false
+const showProtocolToast = () => {
+  uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
+}
+
+const showWxLogon = true
 // 微信登录
 const getPhoneNumber = async (e) => {
-  if (!protocol.value) return await uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
   if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
     uni.showToast({ title: '微信登录失败', icon: 'none' })
     return
@@ -245,11 +251,11 @@ const getPhoneNumber = async (e) => {
     success: async (result) => {
       const wxLoginCode = result?.code || ''
       const query = {
-        wxLoginCode,
-        iv: e.detail.iv,
-        encryptedData: e.detail.encryptedData,
+        loginCode: wxLoginCode,
+        phoneCode: e.detail.code,
+        state: e.detail.encryptedData,
       }
-      await useUserStore.handleSmsLogin(query, current.value === 0)
+      await useUserStore.handleSmsLogin(query, 2)
     },
     fail:(res)=> { console.log("获取登录凭证code失败!", res) }
   })
@@ -276,7 +282,7 @@ const handleLogin = async () => {
     account: query.phone
   })
   
-  await useUserStore.handleSmsLogin(query, current.value === 0)
+  await useUserStore.handleSmsLogin(query, current.value)
 }
 </script>
 

+ 4 - 4
store/user.js

@@ -1,6 +1,6 @@
 import { defineStore } from 'pinia';
 import { getBaseInfo, getUserInfo } from '@/api/user';
-import { smsLogin, passwordLogin, logout, userRegister, shareUserRegister } from '@/api/common'
+import { smsLogin, passwordLogin, weChatLogin, logout, userRegister, shareUserRegister } from '@/api/common'
 import { closeAuthModal } from '@/hooks/useModal'
 import { timesTampChange } from '@/utils/date'
 import { getBaseInfoDictOfName } from '@/utils/getText'
@@ -49,9 +49,9 @@ export const userStore = defineStore('user', {
       this.isLogin = val
     },
     // 登录
-    async handleSmsLogin (query, type) {
-      const api = type ? smsLogin : passwordLogin
-      const { data, code } = await api(query)
+    async handleSmsLogin (query, index = 0) {
+      const apiList = [smsLogin, passwordLogin, weChatLogin] 
+      const { data, code } = await apiList[index](query)
       if (code === 0) {
         uni.showToast({
           title: '登录成功'