Files
shoot-miniprograms/src/store.js
2025-06-02 14:42:07 +08:00

52 lines
969 B
JavaScript

import { defineStore } from "pinia";
// 定义游戏相关的 store
export default defineStore("store", {
// 状态
state: () => ({
user: {
id: "",
nickName: "游客",
avatarUrl: "../static/avatar.png",
trio: 0, // 大于1表示完成了新手引导
},
device: {
id: "",
deviceName: "",
},
config: {},
}),
// 计算属性
getters: {
getUsername: (state) => {
return state.user.username;
},
},
// 方法
actions: {
updateUser(user) {
this.user = user;
},
updateDevice(deviceId, deviceName) {
this.device.id = deviceId;
this.device.deviceName = deviceName;
},
updateConfig(config) {
this.config = config;
},
},
// 开启数据持久化
persist: {
enabled: true,
strategies: [
{
storage: uni.getStorageSync,
paths: ["user", "device", "config"], // 只持久化用户信息
},
],
},
});