更新:登录功能

This commit is contained in:
2025-11-04 12:37:04 +08:00
commit a21fb92916
897 changed files with 51500 additions and 0 deletions

19
utils/auth.ts Normal file
View 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
View 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
View 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'
}