绑定微信号,实现自动登录

This commit is contained in:
Rhett霍 2026-03-23 13:07:22 +08:00
parent a7611313ff
commit 44020afbe0
3 changed files with 188 additions and 30 deletions

View File

@ -57,3 +57,21 @@ export function getCodeImg() {
timeout: 20000 timeout: 20000
}) })
} }
// 微信自动登录
export function autoLogin(data) {
return request({
'url': '/weixin/auth/autoLogin',
'data':data,
'method': 'post'
})
}
// 微信-登录绑定微信号
export function autobind(data) {
return request({
'url': '/weixin/auth/bind',
'data':data,
'method': 'post'
})
}

View File

@ -39,8 +39,8 @@
<script setup> <script setup>
import { ref, getCurrentInstance } from "vue" import { ref, getCurrentInstance } from "vue"
import { onLoad } from "@dcloudio/uni-app" import { onLoad } from "@dcloudio/uni-app"
import { getToken } from '@/utils/auth' import { getToken, setToken } from '@/utils/auth'
import { getCodeImg } from '@/api/login' import { getCodeImg, autoLogin, autobind } from '@/api/login'
import { useConfigStore, useUserStore } from '@/store' import { useConfigStore, useUserStore } from '@/store'
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
@ -52,24 +52,20 @@
const register = ref(false) const register = ref(false)
// //
const passwordVisible = ref(false) const passwordVisible = ref(false)
// URLweixinId
const weixinId = ref('')
//
let autoLoginExecuted = false
// URL
let urlCleared = false
const loginForm = ref({ const loginForm = ref({
username: "admin", username: "admin",
password: "admin123", password: "jm@9527",
code: "", code: "",
uuid: "" uuid: ""
}) })
import { onShow } from "@dcloudio/uni-app"
//
onShow(() => {
// #ifdef H5
const currentUrl = window.location.href
alert('当前URL' + currentUrl)
// #endif
})
// //
function togglePasswordVisible() { function togglePasswordVisible() {
passwordVisible.value = !passwordVisible.value passwordVisible.value = !passwordVisible.value
@ -105,35 +101,179 @@ onShow(() => {
} }
} }
// // IDautobindID
async function pwdLogin() { async function pwdLogin() {
useUserStore().login(loginForm.value).then(() => { // ID
proxy.$modal.closeLoading() if (weixinId.value) {
loginSuccess() const params = {
}).catch(() => { username: loginForm.value.username,
if (captchaEnabled.value) { password: loginForm.value.password,
getCode() code: loginForm.value.code,
uuid: loginForm.value.uuid,
weixinId: weixinId.value
} }
})
autobind(params).then(res => {
proxy.$modal.closeLoading()
if (res && res.code === 200) {
// token
if (res.data && res.data.token) {
setToken(res.data.token)
}
proxy.$modal.msgSuccess("绑定成功,登录中...")
// URL
removeWeixinIdFromUrl()
setTimeout(() => {
loginSuccess()
}, 1500)
} else {
proxy.$modal.msgError(res.msg || "绑定失败,请重试")
if (captchaEnabled.value) {
getCode()
}
}
}).catch(() => {
proxy.$modal.closeLoading()
if (captchaEnabled.value) {
getCode()
}
})
} else {
//
useUserStore().login(loginForm.value).then(() => {
proxy.$modal.closeLoading()
loginSuccess()
}).catch(() => {
proxy.$modal.closeLoading()
if (captchaEnabled.value) {
getCode()
}
})
}
} }
// //
function loginSuccess(result) { function loginSuccess() {
// //
useUserStore().getInfo().then(res => { useUserStore().getInfo().then(() => {
proxy.$tab.reLaunch('/pages/work/index')
}).catch(() => {
// 使
proxy.$tab.reLaunch('/pages/work/index') proxy.$tab.reLaunch('/pages/work/index')
}) })
} }
// URLweixinId?weixinId=#
function getWeixinIdFromUrl() {
// #ifdef H5
const currentUrl = window.location.href
// 使 weixinId #
const match = currentUrl.match(/[?&]weixinId=(.*)/)
if (match && match[1]) {
return decodeURIComponent(match[1])
}
return null
// #endif
// #ifndef H5
return null
// #endif
}
// URLweixinId
function removeWeixinIdFromUrl() {
if (urlCleared) return
// #ifdef H5
try {
const currentUrl = window.location.href
if (currentUrl.includes('weixinId=')) {
const url = new URL(currentUrl)
url.searchParams.delete('weixinId')
window.history.replaceState({}, '', url.toString())
urlCleared = true
}
} catch (e) {
console.error('清除URL参数失败', e)
}
// #endif
}
onLoad(() => { onLoad(() => {
//#ifdef H5 //#ifdef H5
if (getToken()) { //
proxy.$tab.reLaunch('/pages/index') const token = getToken()
if (token) {
proxy.$tab.reLaunch('/pages/work/index')
return
} }
//
if (autoLoginExecuted) {
return
}
// URLweixinId
const id = getWeixinIdFromUrl()
if (id) {
// weixinId
weixinId.value = id
autoLoginExecuted = true
// URL
removeWeixinIdFromUrl()
//
proxy.$modal.loading("微信自动登录中...")
const bodydata = {
weixinId: id
}
autoLogin(bodydata).then(res => {
proxy.$modal.closeLoading()
//
if (res && res.code === 200) {
// token
if (res.data && res.data.token) {
setToken(res.data.token)
}
proxy.$modal.msgSuccess("登录成功")
//
useUserStore().getInfo().then(() => {
proxy.$tab.reLaunch('/pages/work/index')
}).catch(() => {
// 使token
if (getToken()) {
proxy.$tab.reLaunch('/pages/work/index')
} else {
proxy.$modal.msgError("登录异常,请使用账号密码登录")
getCode()
}
})
} else {
proxy.$modal.msgWarning(res?.msg || "请绑定账号后登录")
//
getCode()
}
}).catch(error => {
proxy.$modal.closeLoading()
console.error('自动登录错误:', error)
proxy.$modal.msgError(error.message || "自动登录失败,请使用账号密码登录")
//
getCode()
})
} else {
// ID
getCode()
}
//#endif
//#ifndef H5
// H5
getCode()
//#endif //#endif
}) })
getCode()
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@ -184,7 +324,7 @@ onShow(() => {
line-height: 20px; line-height: 20px;
text-align: left; text-align: left;
padding-left: 15px; padding-left: 15px;
padding-right: 45px; /* 为眼睛图标留出更多空间 */ padding-right: 45px;
} }
.password-eye { .password-eye {
@ -199,7 +339,6 @@ onShow(() => {
width: 30px; width: 30px;
height: 30px; height: 30px;
/* 增加点击区域 */
&::after { &::after {
content: ''; content: '';
position: absolute; position: absolute;

View File

@ -117,6 +117,7 @@ export const useUserStore = defineStore('user', () => {
roles, roles,
permissions, permissions,
userInfo, userInfo,
SET_TOKEN,
SET_AVATAR, SET_AVATAR,
login: loginAction, login: loginAction,
getInfo: getInfoAction, getInfo: getInfoAction,