init
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { create } from 'zustand'
|
||||
import Taro from '@tarojs/taro'
|
||||
import { wxLoginApi } from '@/services/auth'
|
||||
import { bindPhoneApi, wxLoginApi } from '@/services/auth'
|
||||
import type { WxLoginParams } from '@/services/auth'
|
||||
import type { User } from '@/types/user'
|
||||
|
||||
@@ -31,6 +31,8 @@ interface AuthState {
|
||||
token: string | null
|
||||
loading: boolean
|
||||
login: (params: WxLoginParams) => Promise<void>
|
||||
/** 微信手机号授权码绑定手机号(自动匹配门店/供应商) */
|
||||
bindPhone: (phoneCode: string) => Promise<void>
|
||||
logout: () => void
|
||||
/** 更新用户信息(用于编辑资料后同步 store) */
|
||||
setUser: (user: User) => void
|
||||
@@ -58,6 +60,19 @@ const useAuthStore = create<AuthState>((set) => {
|
||||
}
|
||||
},
|
||||
|
||||
/** 绑定手机号:POST /mini/auth/phone(登录后 type=0 待绑定时调用) */
|
||||
bindPhone: async (phoneCode: string) => {
|
||||
const res = await bindPhoneApi({ phoneCode })
|
||||
const { token, user } = res.data
|
||||
set({ user, token })
|
||||
try {
|
||||
Taro.setStorageSync(STORAGE_KEYS.TOKEN, token)
|
||||
Taro.setStorageSync(STORAGE_KEYS.USER, JSON.stringify(user))
|
||||
} catch {
|
||||
// storage 写入失败不阻塞绑定流程
|
||||
}
|
||||
},
|
||||
|
||||
/** 退出登录 */
|
||||
logout: () => {
|
||||
set({ user: null, token: null })
|
||||
|
||||
Reference in New Issue
Block a user