fix: 设备分配代理,虚比例,sim顺序
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m15s
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m15s
This commit is contained in:
@@ -5,26 +5,21 @@
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { ElMessage, ElNotification } from 'element-plus'
|
||||
import { ElNotification } from 'element-plus'
|
||||
import type { FormInstance, FormRules } from 'element-plus'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { HOME_PAGE } from '@/router/routesAlias'
|
||||
import AppConfig from '@/config'
|
||||
import { AuthService } from '@/api/modules'
|
||||
import { ApiStatus } from '@/utils/http/status'
|
||||
import { MOCK_ACCOUNTS, mockLogin, mockGetUserInfo, type MockAccount } from '@/mock/auth'
|
||||
import { saveCredentials, getRememberedCredentials } from '@/utils/auth/rememberPassword'
|
||||
import { usernameRules, passwordRules } from '@/utils/auth/loginValidation'
|
||||
import { getRedirectPath } from '@/router/guards/permission'
|
||||
|
||||
// 开发模式:是否使用 Mock 数据
|
||||
const USE_MOCK = false
|
||||
|
||||
/**
|
||||
* 登录表单数据
|
||||
*/
|
||||
interface LoginForm {
|
||||
account: string
|
||||
username: string
|
||||
password: string
|
||||
rememberPassword: boolean
|
||||
@@ -44,7 +39,6 @@ export function useLogin() {
|
||||
|
||||
// 表单数据
|
||||
const formData = reactive<LoginForm>({
|
||||
account: '',
|
||||
username: '',
|
||||
password: '',
|
||||
rememberPassword: true
|
||||
@@ -56,47 +50,19 @@ export function useLogin() {
|
||||
password: passwordRules(t)
|
||||
}))
|
||||
|
||||
// Mock 账号列表
|
||||
const mockAccounts = computed<MockAccount[]>(() => MOCK_ACCOUNTS)
|
||||
|
||||
/**
|
||||
* 初始化表单
|
||||
*/
|
||||
// 初始化表单
|
||||
const initForm = () => {
|
||||
// 尝试加载记住的密码
|
||||
const remembered = getRememberedCredentials()
|
||||
if (remembered) {
|
||||
formData.username = remembered.username
|
||||
formData.password = remembered.password
|
||||
formData.rememberPassword = remembered.rememberPassword
|
||||
|
||||
// 如果使用 Mock,尝试匹配 Mock 账号
|
||||
if (USE_MOCK) {
|
||||
const matchedAccount = MOCK_ACCOUNTS.find((acc) => acc.username === remembered.username)
|
||||
if (matchedAccount) {
|
||||
formData.account = matchedAccount.key
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 开发环境:使用真实 API 默认账号
|
||||
if (import.meta.env.MODE === 'development') {
|
||||
formData.username = 'admin'
|
||||
formData.password = 'Admin@123456'
|
||||
formData.rememberPassword = false
|
||||
}
|
||||
// 生产环境:不填充默认值
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 Mock 账号
|
||||
*/
|
||||
const setupAccount = (key: string) => {
|
||||
const selectedAccount = MOCK_ACCOUNTS.find((account) => account.key === key)
|
||||
if (selectedAccount) {
|
||||
formData.account = key
|
||||
formData.username = selectedAccount.username
|
||||
formData.password = selectedAccount.password
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,13 +78,7 @@ export function useLogin() {
|
||||
if (!valid) return
|
||||
|
||||
loading.value = true
|
||||
|
||||
// 判断使用 Mock 还是真实 API
|
||||
if (USE_MOCK) {
|
||||
await handleMockLogin()
|
||||
} else {
|
||||
await handleRealLogin()
|
||||
}
|
||||
await handleRealLogin()
|
||||
} catch (error: any) {
|
||||
console.error('登录失败:', error)
|
||||
} finally {
|
||||
@@ -127,44 +87,7 @@ export function useLogin() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock 登录(开发测试用)
|
||||
*/
|
||||
const handleMockLogin = async () => {
|
||||
const loginResult = mockLogin(formData.username, formData.password)
|
||||
|
||||
if (!loginResult) {
|
||||
console.log(t('login.error.invalidCredentials'))
|
||||
return
|
||||
}
|
||||
|
||||
// 保存 Token
|
||||
userStore.setToken(loginResult.access_token, loginResult.refresh_token)
|
||||
|
||||
// 获取用户信息
|
||||
const userInfo = mockGetUserInfo(loginResult.access_token)
|
||||
|
||||
if (!userInfo) {
|
||||
console.log(t('login.error.getUserInfoFailed'))
|
||||
return
|
||||
}
|
||||
|
||||
// 保存用户信息
|
||||
userStore.setUserInfo(userInfo)
|
||||
userStore.setLoginStatus(true)
|
||||
|
||||
// 保存记住密码
|
||||
saveCredentials(formData.username, formData.password, formData.rememberPassword)
|
||||
|
||||
// 显示登录成功提示
|
||||
showLoginSuccessNotice()
|
||||
|
||||
// 跳转到重定向页面或首页
|
||||
const redirectPath = getRedirectPath(route)
|
||||
await router.push(redirectPath || HOME_PAGE)
|
||||
}
|
||||
|
||||
/**
|
||||
* 真实 API 登录
|
||||
* 登录
|
||||
*/
|
||||
const handleRealLogin = async () => {
|
||||
// 调用登录接口
|
||||
@@ -253,8 +176,6 @@ export function useLogin() {
|
||||
formData,
|
||||
rules,
|
||||
loading,
|
||||
mockAccounts,
|
||||
setupAccount,
|
||||
handleLogin
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user