账户密码登录

This commit is contained in:
liu
2026-08-21 11:01:37 +08:00
parent 3bd26acbb9
commit f0d8f6bac4
13 changed files with 328 additions and 599 deletions
+9 -18
View File
@@ -1,7 +1,7 @@
import { create } from 'zustand'
import Taro from '@tarojs/taro'
import { registerApi, wxLoginApi } from '@/services/auth'
import type { RegisterParams, WxLoginParams } from '@/services/auth'
import { loginApi } from '@/services/auth'
import type { LoginParams } from '@/services/auth'
import type { User } from '@/types/user'
/** 存储 key */
@@ -26,7 +26,7 @@ function loadFromStorage(): { user: User | null; token: string | null } {
return { user: null, token: null }
}
/** 登录 / 注册成功后持久化 token 与用户信息 */
/** 登录成功后持久化 token 与门店信息 */
function persistAuth(token: string, user: User): void {
try {
Taro.setStorageSync(STORAGE_KEYS.TOKEN, token)
@@ -40,11 +40,10 @@ interface AuthState {
user: User | null
token: string | null
loading: boolean
login: (params: WxLoginParams) => Promise<void>
/** 微信注册(手机号授权 + 门店编码绑定门店) */
register: (params: RegisterParams) => Promise<void>
/** 账号密码登录(门店账号由商家后台分配) */
login: (params: LoginParams) => Promise<void>
logout: () => void
/** 更新用户信息(用于编辑资料后同步 store) */
/** 更新门店信息(用于编辑资料后同步 store) */
setUser: (user: User) => void
}
@@ -57,17 +56,9 @@ const useAuthStore = create<AuthState>((set) => {
token: initial.token,
loading: !!(initial.token && initial.user), // 已恢复则立即 ready
/** 登录(仅已注册用户可登录,未注册由页面引导去注册) */
login: async (params: WxLoginParams) => {
const res = await wxLoginApi(params)
const { token, user } = res.data
set({ user, token })
persistAuth(token, user)
},
/** 注册:POST /mini/auth/register */
register: async (params: RegisterParams) => {
const res = await registerApi(params)
/** 账号密码登录:POST /mini/auth/login */
login: async (params: LoginParams) => {
const res = await loginApi(params)
const { token, user } = res.data
set({ user, token })
persistAuth(token, user)