import { defineStore } from "pinia"; import { getUserInfo } from "@/api/user.js"; import { LOGIN_STATUS, UID, PLATFORM, EXPIRES_TIME } from "@/config/cache.js"; import Cache from "@/utils/cache.js"; import { USER_INFO } from "@/config/cache.js"; import { getMiniProgramData } from "@/api/api"; export const useAppStore = defineStore("app", { state: () => { return { token: Cache.get(LOGIN_STATUS) || "", backgroundColor: "#fff", userInfo: Cache.get(USER_INFO) ? JSON.parse(Cache.get(USER_INFO)) : null, uid: Cache.get(UID) || null, homeActive: false, chatUrl: Cache.get("chatUrl") || "", systemPlatform: Cache.get(PLATFORM) ? Cache.get(PLATFORM) : "", productType: Cache.get("productType") || "", navbarHeight: 0, userPanelInfo: {}, refreshArticles: false, indexRefreshArticles: false, wxConfig: { auditModeEnabled: false, carouselImages: [ { id: 0, imageUrl: "", jumpUrl: "", sort: 0, }, ], homePopupImage: "", inviteImage: "", isOpen: true, logoImage: "", mailingAddress: "", signCoinValue: 0, signGrowthValue: 0, }, // 店铺信息 shopInfo:{ name:"水贝001号店铺", shopId:"001" } }; }, getters: { uidComputed: (state) => state.uid, tokenComputed: (state) => state.token, isLogin: (state) => !!state.token, homeActiveComputed: (state) => state.homeActive, productTypeComputed: (state) => state.productType, chatUrlComputed: (state) => state.chatUrl, $userInfo: (state) => state.userInfo, navbarHeightGetter: (state) => state.navbarHeight, userPanelInfoGetter: (state) => state.userPanelInfo, $wxConfig: (state) => state.wxConfig, shopInfoGetter: (state) => state.shopInfo, }, actions: { SET_REFRESH(val) { this.refreshArticles = val; }, setIndexRefersh(val) { this.indexRefreshArticles = val; }, SET_NAVBAR_HEIGHT(val) { this.navbarHeight = val; }, LOGIN(opt) { this.token = opt.token; Cache.set(LOGIN_STATUS, opt.token); }, SETUID(val) { this.uid = val; Cache.set(UID, val); }, UPDATE_LOGIN(token) { this.token = token; }, LOGOUT() { this.token = undefined; this.uid = undefined; Cache.clear(LOGIN_STATUS); Cache.clear(UID); Cache.clear(USER_INFO); Cache.clear(EXPIRES_TIME); }, BACKGROUND_COLOR(color) { this.color = color; document.body.style.backgroundColor = color; }, UPDATE_USERINFO(userInfo) { this.userInfo = userInfo; Cache.set(USER_INFO, userInfo); }, OPEN_HOME() { this.homeActive = true; }, CLOSE_HOME() { this.homeActive = false; }, SET_CHATURL(chatUrl) { this.chatUrl = chatUrl; }, SYSTEM_PLATFORM(systemPlatform) { this.systemPlatform = systemPlatform; Cache.set(PLATFORM, systemPlatform); }, changInfo(payload) { this.userInfo[payload.amount1] = payload.amount2; Cache.set(USER_INFO, this.userInfo); }, PRODUCT_TYPE(productType) { this.productType = productType; Cache.set("productType", productType); }, async USERINFO(force) { try { const res = await getUserInfo(); this.UPDATE_USERINFO(res.data); return res.data; } catch (e) {} }, // 获取小程序基本配置 async GET_WX_CONFIG() { try { const res = await getMiniProgramData(); this.wxConfig = { ...res.data }; } catch (e) { console.log("GET_WX_CONFIG-stores-res", e); throw e; } }, UPDATE_userPanelInfo(userPanelInfo) { this.userPanelInfo = userPanelInfo; }, }, });