Spaces:
Running
Running
File size: 9,761 Bytes
3444669 51dc757 3444669 608cb4c 3444669 47eb5bf 3283b52 47eb5bf 3283b52 47eb5bf 3444669 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 28f2243 608cb4c 28f2243 3444669 608cb4c f4ed138 608cb4c 47eb5bf 608cb4c 3444669 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 013ade0 47eb5bf 608cb4c 47eb5bf 608cb4c 6fa5e4b 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 3283b52 47eb5bf 608cb4c 47eb5bf 608cb4c 6fa5e4b 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 28f2243 608cb4c 77cd7c5 47eb5bf 77cd7c5 47eb5bf 77cd7c5 47eb5bf 77cd7c5 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 6fa5e4b 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 6fa5e4b 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 013ade0 47eb5bf 608cb4c 28f2243 608cb4c 28f2243 608cb4c 28f2243 608cb4c 28f2243 608cb4c 6fa5e4b 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 47eb5bf 608cb4c 28f2243 3283b52 28f2243 3283b52 28f2243 3283b52 28f2243 3444669 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
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: "2000-01-01",
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,
},
),
);
|