提交
This commit is contained in:
139
pxdj-merch-uni/src/stores/user/useUserStore.ts
Normal file
139
pxdj-merch-uni/src/stores/user/useUserStore.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { defineStore } from "pinia";
|
||||
import pinia from "../index";
|
||||
import { wxLogin, testLogin, getUserInfo, logOut,updateProdSwitch } from "@/api/user/user";
|
||||
import { setUserInfo, activation } from "@/api/user/user-info";
|
||||
import util from "@/utils/util.js";
|
||||
|
||||
const STORE_ID = "USER";
|
||||
|
||||
const useUserStore = defineStore(STORE_ID, {
|
||||
state: (): any => {
|
||||
return {
|
||||
userInfo: null,
|
||||
userOverview: null,
|
||||
token: null,
|
||||
isNewReg: null,
|
||||
sessionId: "",
|
||||
activeTab: 0, // 默认选中的索引
|
||||
prodSwitch: 1,
|
||||
};
|
||||
},
|
||||
getters: {
|
||||
isLogin(): boolean {
|
||||
return !!(this.userInfo && this.token);
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
//设置active的值
|
||||
setActive(active) {
|
||||
this.activeTab = active;
|
||||
},
|
||||
// 微信登录
|
||||
async doWxLogin(loginData) {
|
||||
const { data } = await wxLogin(loginData);
|
||||
if (data && data.accessToken) return data;
|
||||
return false;
|
||||
},
|
||||
async doPhoneLogin(loginData) {
|
||||
const { data } = await testLogin(loginData);
|
||||
if (data && data.accessToken) return data;
|
||||
return false;
|
||||
},
|
||||
|
||||
async uploadAvatar(avatarUrl) {
|
||||
const res = await util.wxUploadAvatar(avatarUrl, this.token);
|
||||
console.log(res);
|
||||
this.getUserInfo();
|
||||
},
|
||||
async updateProdSwitch(){
|
||||
const {data} = await updateProdSwitch();
|
||||
this.prodSwitch=data;
|
||||
},
|
||||
// 退出登录
|
||||
getProdSwitch() {
|
||||
return this.prodSwitch;
|
||||
},
|
||||
async doLogout() {
|
||||
await logOut();
|
||||
this.$reset();
|
||||
uni.setStorage({ key: STORE_ID, data: JSON.stringify(this.$state) });
|
||||
},
|
||||
|
||||
async getUserInfo() {
|
||||
const { data } = await getUserInfo();
|
||||
if (!data) return false;
|
||||
this.userInfo = data;
|
||||
uni.setStorage({ key: STORE_ID, data: JSON.stringify(this.$state) });
|
||||
return this.userInfo;
|
||||
},
|
||||
async setUserInfo(params) {
|
||||
const { success } = await setUserInfo(params);
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
await this.getUserInfo();
|
||||
uni.showToast({
|
||||
title: "激活成功",
|
||||
icon: "none",
|
||||
});
|
||||
return true;
|
||||
},
|
||||
|
||||
async onActivation(params) {
|
||||
uni.showLoading({
|
||||
title: "提交中...",
|
||||
});
|
||||
const { success } = await activation(params);
|
||||
if (!success) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.getUserInfo();
|
||||
let text = "激活成功";
|
||||
if (params.flagStatus !== 0) {
|
||||
text = "提交成功,审核中";
|
||||
}
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: text,
|
||||
icon: "none",
|
||||
});
|
||||
return true;
|
||||
},
|
||||
|
||||
getSessionId() {
|
||||
if (this.sessionId) {
|
||||
return this.sessionId;
|
||||
} else {
|
||||
const sessionId = util.randomString(16);
|
||||
this.sessionId = sessionId;
|
||||
return sessionId;
|
||||
}
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
/* 开启数据持久化 */
|
||||
enabled: true,
|
||||
H5Storage: window?.localStorage,
|
||||
strategies: [
|
||||
{
|
||||
key: STORE_ID,
|
||||
paths: [
|
||||
"userInfo",
|
||||
"userOverview",
|
||||
"token",
|
||||
"isNewReg",
|
||||
"sessionId",
|
||||
"activeTab",
|
||||
"prodSwitch"
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default useUserStore;
|
||||
|
||||
export function useUserStoreWithout() {
|
||||
return useUserStore(pinia);
|
||||
}
|
||||
Reference in New Issue
Block a user