Spaces:
Running
Running
import { create } from "zustand"; | |
import { persist } from "zustand/middleware"; | |
import { StoreKey } from "../constant"; | |
import { showToast } from "../components/ui-lib"; | |
import { useAccessStore } from "./access"; | |
import { getHeaders } from "../requests"; | |
export interface shuixianRes { | |
code: number; | |
msg: string; | |
token: string; | |
data: { | |
name: string; | |
head: string; | |
signatrue: string; | |
wallet: number; | |
vip_state: string; | |
vip_time_stmp: string; | |
ban: string; | |
sig_state: string; | |
title: string; | |
mail: string; | |
}; | |
} | |
function getLogin(){ | |
setTimeout(() => { | |
window.location.href = "/#/login"; | |
}, 1000); | |
} | |
export interface eladminRes { | |
data:{ | |
user:object; | |
token:string; | |
nickName:string; | |
wallet:number; | |
vipTime:string; | |
email:string; | |
sigState:string; | |
head:string; | |
}; | |
flag:boolean; | |
msg:string; | |
message:string; | |
} | |
export interface codeRes { | |
uuid:string; | |
img:string; | |
} | |
export interface UserStore { | |
user: string; | |
password: string; | |
name: string; | |
wallet: number; | |
vip_time: string; | |
mail: string; | |
sig_state: string; | |
head: string; | |
uuid: string; | |
img: string; | |
update: (updater: (user: UserInfo) => void) => void; | |
login: (userName: string, password: string,code:string) => void; | |
register: ( | |
user: string, | |
password: string, | |
name: string, | |
mail: string, | |
code: string, | |
) => void; | |
getMailCode: (mail: string) => void; | |
userSig: () => void; | |
setUuidAndImg:(uuid:string,img:string) => void; | |
getCode:() => any; | |
reset: () => void; | |
updateUser: (user: string) => void; | |
updatePassword: (password: string) => void; | |
updateInfo: ( | |
name: string, | |
wallet: number, | |
vip_time: string, | |
mail: string, | |
sig_state: string, | |
head: string, | |
) => void; | |
updateWallet: (wallet: number) => void; | |
updateName: (name: string) => void; | |
getUserInfo: () => void; | |
findPwd: (mail: string,code:string) => void; | |
useKami: (code: string) => void; | |
logOut:()=>any; | |
getRestPwdCode:(mail:string)=>void; | |
updatePass:(oldPass:string,newPass:string)=>void; | |
} | |
export const DEFAULT_USER = { | |
user: "", | |
password: "", | |
name: "", | |
wallet: 0, | |
vip_time: "", | |
mail: "", | |
sig_state: "", | |
head: "", | |
uuid:"", | |
img:"" | |
}; | |
export type UserInfo = typeof DEFAULT_USER; | |
export const useUserStore = create<UserStore>()( | |
persist( | |
(set, get) => ({ | |
...DEFAULT_USER, | |
update(updater) { | |
const config = { ...get() }; | |
updater(config); | |
set(() => config); | |
}, | |
updateInfo( | |
name: string, | |
wallet: number, | |
vip_time: string, | |
mail: string, | |
sig_state: string, | |
head: string, | |
) { | |
set(() => ({ | |
name: name, | |
wallet: wallet, | |
vip_time: vip_time, | |
mail: mail, | |
sig_state: sig_state, | |
head: head, | |
})); | |
}, | |
reset() { | |
set(() => ({ ...DEFAULT_USER })); | |
}, | |
updateUser(user: string) { | |
set(() => ({ user: user })); | |
}, | |
setUuidAndImg(uuid: string,img:string) { | |
set(() => ({ uuid:uuid, img:img})); | |
}, | |
async getCode() { | |
let res = await fetch( | |
"/api/user/code", | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
let response = (await res.json()) as codeRes; | |
console.log(response); | |
this.setUuidAndImg(response.uuid,response.img) | |
return response.img | |
}, | |
async updateName(name: string) { | |
let res = await fetch( | |
"/api/user/set?name="+name, | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
showToast(response.msg); | |
if (response.flag) { | |
await this.getUserInfo(); | |
}else{ | |
if(response.msg=="未登录!"){ | |
getLogin() | |
} | |
} | |
}, | |
updatePassword(password: string) { | |
set(() => ({ password: password })); | |
}, | |
updateWallet(wallet: number) { | |
set(() => ({ wallet: get().wallet - wallet })); | |
}, | |
async login(user, password,code) { | |
let body={ | |
"username": user, | |
"password": password, | |
"code": code, | |
"uuid": get().uuid | |
} | |
let res = await fetch( | |
"/api/user/login", | |
{ | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body:JSON.stringify(body) | |
}, | |
); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
if (response.flag) { | |
useUserStore.getState().updateUser(user); | |
useAccessStore.getState().updateAuth(response.data.token); | |
showToast("登录成功!"); | |
setTimeout(() => { | |
window.location.href = "/#/chat"; | |
}, 1000); | |
await this.getUserInfo(); | |
} else { | |
showToast(response.message); | |
} | |
}, | |
async register(user, password, name, mail, code) { | |
let res = await fetch( | |
"/api/user/register?user=" + | |
user + | |
"&password=" + | |
password + | |
"&name=" + | |
name+"&mail="+mail+"&code="+code, | |
{ | |
method: "POST", | |
}, | |
); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
if (response.flag) { | |
showToast("注册成功"); | |
setTimeout(() => { | |
window.location.href = "/#/login"; | |
}, 1000); | |
} else { | |
showToast(response.msg); | |
} | |
}, | |
async getMailCode(mail: string) { | |
let res = await fetch("/api/user/mail?mail=" + mail, { | |
method: "POST", | |
}); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
showToast(response.msg); | |
}, | |
async userSig() { | |
let res = await fetch( | |
"/api/user/sig", | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
showToast(response.msg); | |
if (response.flag) { | |
await this.getUserInfo(); | |
}else{ | |
if(response.msg=="未登录!"){ | |
getLogin() | |
} | |
} | |
}, | |
async getUserInfo() { | |
let resdata = await fetch( | |
"/api/user/info", | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
let responsedata = (await resdata.json()) as eladminRes; | |
if (responsedata.flag) { | |
let data = responsedata.data; | |
this.updateInfo( | |
data.nickName, | |
data.wallet, | |
data.vipTime, | |
data.email, | |
data.sigState, | |
data.head, | |
); | |
} else { | |
showToast(responsedata.msg); | |
if(responsedata.msg=="未登录!"){ | |
getLogin() | |
} | |
} | |
}, | |
async findPwd(mail,code) { | |
let res = await fetch("/api/user/findpwd?mail=" + mail+"&code="+code, { | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
if (response.flag) { | |
showToast("密码已发送至您的邮箱,请注意查收!"); | |
} else { | |
showToast(response.message); | |
} | |
}, | |
async useKami(code) { | |
let res = await fetch( | |
"/api/user/kami?user=" + | |
get().user + | |
"&password=" + | |
get().password + | |
"&code=" + | |
code, | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
let response = (await res.json()) as eladminRes; | |
console.log(response); | |
if (response.flag) { | |
showToast(response.msg); | |
await this.getUserInfo(); | |
} else { | |
showToast(response.msg); | |
if(response.msg=="未登录!"){ | |
getLogin() | |
} | |
} | |
}, | |
async logOut(){ | |
await fetch( | |
"/api/user/logout", | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
this.reset() | |
}, | |
async getRestPwdCode(mail){ | |
await fetch( | |
"/api/user/restmail?mail="+mail, | |
{ | |
method: "POST", | |
headers: { | |
...getHeaders(), | |
}, | |
}, | |
); | |
showToast("发送成功!可能在垃圾邮箱中!") | |
}, | |
async updatePass(oldPass,newPass){ | |
let body={ | |
oldPass:oldPass, | |
newPass:newPass | |
} | |
let res=await fetch( | |
"/api/user/updatePass", | |
{ | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
...getHeaders(), | |
}, | |
body:JSON.stringify(body) | |
}, | |
); | |
let ress=(await res.json()) as eladminRes | |
if(ress.message==null){ | |
showToast("修改成功!") | |
}else{ | |
showToast(ress.message) | |
} | |
} | |
}), | |
{ | |
name: StoreKey.User, | |
version: 1, | |
}, | |
), | |
); | |