更新:登录功能
This commit is contained in:
19
utils/auth.ts
Normal file
19
utils/auth.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// utils/auth.ts
|
||||
import { useUserStore } from '@/stores/user'
|
||||
|
||||
export function getAuthToken(): string {
|
||||
try {
|
||||
const store = useUserStore()
|
||||
return store?.token || uni.getStorageSync('token') || ''
|
||||
} catch {
|
||||
return uni.getStorageSync('token') || ''
|
||||
}
|
||||
}
|
||||
|
||||
export function setAuthToken(token: string) {
|
||||
uni.setStorageSync('token', token)
|
||||
}
|
||||
|
||||
export function clearAuthToken() {
|
||||
uni.removeStorageSync('token')
|
||||
}
|
||||
6
utils/i18n.ts
Normal file
6
utils/i18n.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
// utils/i18n.ts
|
||||
import { i18n } from '@/main' // 你的 i18n 实例
|
||||
|
||||
export function t(key: string, values?: Record<string, any>) {
|
||||
return i18n.global.t(key, values)
|
||||
}
|
||||
40
utils/validator.ts
Normal file
40
utils/validator.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 验证邮箱格式
|
||||
* @param email 邮箱地址
|
||||
* @returns 是否有效
|
||||
*/
|
||||
export function validateEmail(email: string): boolean {
|
||||
return /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(email)
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证密码格式
|
||||
* @param password 密码 6-20位,必须包含字母和数字
|
||||
* @returns 是否有效
|
||||
*/
|
||||
export function validatePassword(password: string): boolean {
|
||||
return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(password)
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查密码强度
|
||||
* @param password 密码
|
||||
* @returns 'strong' | 'medium' | 'weak' | 'invalid'
|
||||
*/
|
||||
export function checkPasswordStrength(password: string): 'strong' | 'medium' | 'weak' | 'invalid' {
|
||||
// 强密码:8位以上,包含大小写字母、数字和特殊字符
|
||||
const strongRegex = /^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$/
|
||||
// 中等密码:8位以上,包含大小写字母、数字、特殊字符中的两项
|
||||
const mediumRegex = /^(?=.{8,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[a-z])(?=.*\W))|((?=.*[0-9])(?=.*\W))|((?=.*[A-Z])(?=.*\W))).*$/
|
||||
// 足够长度:8位以上
|
||||
const enoughRegex = /(?=.{8,}).*/
|
||||
|
||||
if (strongRegex.test(password)) {
|
||||
return 'strong'
|
||||
} else if (mediumRegex.test(password)) {
|
||||
return 'medium'
|
||||
} else if (enoughRegex.test(password)) {
|
||||
return 'weak'
|
||||
}
|
||||
return 'invalid'
|
||||
}
|
||||
Reference in New Issue
Block a user