Initial commit: One Pipe System
完整的管理系统,包含账户管理、卡片管理、套餐管理、财务管理等功能模块。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
6
src/utils/auth/index.ts
Normal file
6
src/utils/auth/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 认证相关工具函数统一导出
|
||||
*/
|
||||
|
||||
export * from './rememberPassword'
|
||||
export * from './loginValidation'
|
||||
172
src/utils/auth/loginValidation.ts
Normal file
172
src/utils/auth/loginValidation.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* 登录表单验证规则
|
||||
*/
|
||||
|
||||
import type { FormItemRule } from 'element-plus'
|
||||
|
||||
/**
|
||||
* 用户名验证规则
|
||||
*/
|
||||
export const usernameRules = (t: (key: string) => string): FormItemRule[] => [
|
||||
{
|
||||
required: true,
|
||||
message: t('login.placeholder[0]'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
min: 3,
|
||||
max: 20,
|
||||
message: t('login.validation.usernameLength'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
pattern: /^[a-zA-Z0-9_]+$/,
|
||||
message: t('login.validation.usernamePattern'),
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
|
||||
/**
|
||||
* 密码验证规则
|
||||
*/
|
||||
export const passwordRules = (t: (key: string) => string): FormItemRule[] => [
|
||||
{
|
||||
required: true,
|
||||
message: t('login.placeholder[1]'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
min: 6,
|
||||
max: 20,
|
||||
message: t('login.validation.passwordLength'),
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
|
||||
/**
|
||||
* 强密码验证规则(用于注册/修改密码)
|
||||
*/
|
||||
export const strongPasswordRules = (t: (key: string) => string): FormItemRule[] => [
|
||||
{
|
||||
required: true,
|
||||
message: t('login.placeholder[1]'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
min: 8,
|
||||
max: 20,
|
||||
message: t('login.validation.strongPasswordLength'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
validator: (rule: any, value: any, callback: any) => {
|
||||
if (!value) {
|
||||
callback()
|
||||
return
|
||||
}
|
||||
|
||||
// 必须包含大小写字母、数字
|
||||
const hasUpperCase = /[A-Z]/.test(value)
|
||||
const hasLowerCase = /[a-z]/.test(value)
|
||||
const hasNumber = /\d/.test(value)
|
||||
|
||||
if (!hasUpperCase || !hasLowerCase || !hasNumber) {
|
||||
callback(new Error(t('login.validation.strongPasswordPattern')))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
|
||||
/**
|
||||
* 确认密码验证规则
|
||||
*/
|
||||
export const confirmPasswordRules = (
|
||||
t: (key: string) => string,
|
||||
getPassword: () => string
|
||||
): FormItemRule[] => [
|
||||
{
|
||||
required: true,
|
||||
message: t('login.validation.confirmPasswordRequired'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
validator: (rule: any, value: any, callback: any) => {
|
||||
if (!value) {
|
||||
callback()
|
||||
return
|
||||
}
|
||||
|
||||
if (value !== getPassword()) {
|
||||
callback(new Error(t('login.validation.confirmPasswordNotMatch')))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
|
||||
/**
|
||||
* 手机号验证规则
|
||||
*/
|
||||
export const phoneRules = (t: (key: string) => string, required = false): FormItemRule[] => {
|
||||
const rules: FormItemRule[] = []
|
||||
|
||||
if (required) {
|
||||
rules.push({
|
||||
required: true,
|
||||
message: t('login.validation.phoneRequired'),
|
||||
trigger: 'blur'
|
||||
})
|
||||
}
|
||||
|
||||
rules.push({
|
||||
pattern: /^1[3-9]\d{9}$/,
|
||||
message: t('login.validation.phonePattern'),
|
||||
trigger: 'blur'
|
||||
})
|
||||
|
||||
return rules
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮箱验证规则
|
||||
*/
|
||||
export const emailRules = (t: (key: string) => string, required = false): FormItemRule[] => {
|
||||
const rules: FormItemRule[] = []
|
||||
|
||||
if (required) {
|
||||
rules.push({
|
||||
required: true,
|
||||
message: t('login.validation.emailRequired'),
|
||||
trigger: 'blur'
|
||||
})
|
||||
}
|
||||
|
||||
rules.push({
|
||||
type: 'email',
|
||||
message: t('login.validation.emailPattern'),
|
||||
trigger: 'blur'
|
||||
})
|
||||
|
||||
return rules
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证码验证规则
|
||||
*/
|
||||
export const captchaRules = (t: (key: string) => string): FormItemRule[] => [
|
||||
{
|
||||
required: true,
|
||||
message: t('login.validation.captchaRequired'),
|
||||
trigger: 'blur'
|
||||
},
|
||||
{
|
||||
len: 4,
|
||||
message: t('login.validation.captchaLength'),
|
||||
trigger: 'blur'
|
||||
}
|
||||
]
|
||||
80
src/utils/auth/rememberPassword.ts
Normal file
80
src/utils/auth/rememberPassword.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 记住密码功能
|
||||
* 使用 localStorage 存储加密后的用户名和密码
|
||||
*/
|
||||
|
||||
// 存储key
|
||||
const REMEMBER_KEY = 'remembered_credentials'
|
||||
|
||||
// 简单加密(实际项目中应使用更安全的加密方式)
|
||||
const encode = (str: string): string => {
|
||||
return btoa(encodeURIComponent(str))
|
||||
}
|
||||
|
||||
// 简单解密
|
||||
const decode = (str: string): string => {
|
||||
try {
|
||||
return decodeURIComponent(atob(str))
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存的凭证接口
|
||||
*/
|
||||
export interface RememberedCredentials {
|
||||
username: string
|
||||
password: string
|
||||
rememberPassword: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存登录凭证
|
||||
*/
|
||||
export const saveCredentials = (username: string, password: string, remember: boolean) => {
|
||||
if (remember) {
|
||||
const credentials = {
|
||||
u: encode(username),
|
||||
p: encode(password),
|
||||
r: true
|
||||
}
|
||||
localStorage.setItem(REMEMBER_KEY, JSON.stringify(credentials))
|
||||
} else {
|
||||
// 如果不记住密码,清除已保存的凭证
|
||||
localStorage.removeItem(REMEMBER_KEY)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取保存的登录凭证
|
||||
*/
|
||||
export const getRememberedCredentials = (): RememberedCredentials | null => {
|
||||
try {
|
||||
const saved = localStorage.getItem(REMEMBER_KEY)
|
||||
if (!saved) return null
|
||||
|
||||
const credentials = JSON.parse(saved)
|
||||
return {
|
||||
username: decode(credentials.u),
|
||||
password: decode(credentials.p),
|
||||
rememberPassword: credentials.r
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除保存的登录凭证
|
||||
*/
|
||||
export const clearRememberedCredentials = () => {
|
||||
localStorage.removeItem(REMEMBER_KEY)
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有保存的凭证
|
||||
*/
|
||||
export const hasRememberedCredentials = (): boolean => {
|
||||
return localStorage.getItem(REMEMBER_KEY) !== null
|
||||
}
|
||||
Reference in New Issue
Block a user