Files
shoot-miniprograms/src/store.js

84 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-05-25 23:51:10 +08:00
import { defineStore } from "pinia";
2025-06-13 16:36:18 +08:00
const defaultUser = {
id: "",
nickName: "游客",
2025-06-17 13:47:33 +08:00
avatar: "../static/avatar.png",
2025-06-13 16:36:18 +08:00
trio: 0, // 大于1表示完成了新手引导
};
2025-05-25 23:51:10 +08:00
// 定义游戏相关的 store
export default defineStore("store", {
// 状态
state: () => ({
2025-06-13 16:36:18 +08:00
user: defaultUser,
2025-05-29 23:45:44 +08:00
device: {
2025-06-08 13:55:09 +08:00
deviceId: "",
2025-05-29 23:45:44 +08:00
deviceName: "",
2025-05-25 23:51:10 +08:00
},
2025-06-02 14:42:07 +08:00
config: {},
2025-05-25 23:51:10 +08:00
}),
// 计算属性
getters: {
getUsername: (state) => {
return state.user.username;
},
},
// 方法
actions: {
2025-05-26 16:28:13 +08:00
updateUser(user) {
2025-06-18 02:26:42 +08:00
this.user = { ...defaultUser, ...user };
const rankInfos = this.config.randInfos || [];
let lvlName = "";
if (this.user.scores) {
rankInfos.some((r, index) => {
lvlName = rankInfos[index].name;
if (r.upgrade_scores > this.user.scores) {
if (rankInfos[index - 1]) {
lvlName = rankInfos[index - 1].name;
}
return true;
}
return false;
});
}
this.user.lvlName = lvlName;
2025-05-26 16:28:13 +08:00
},
2025-05-29 23:45:44 +08:00
updateDevice(deviceId, deviceName) {
2025-06-08 13:55:09 +08:00
this.device.deviceId = deviceId;
2025-05-29 23:45:44 +08:00
this.device.deviceName = deviceName;
},
2025-06-02 14:42:07 +08:00
updateConfig(config) {
this.config = config;
2025-06-18 02:26:42 +08:00
const rankInfos = config.randInfos || [];
let lvlName = "";
if (this.user.scores) {
rankInfos.some((r, index) => {
lvlName = rankInfos[index].name;
if (r.upgrade_scores > this.user.scores) {
if (rankInfos[index - 1]) {
lvlName = rankInfos[index - 1].name;
}
return true;
}
return false;
});
}
this.user.lvlName = lvlName;
2025-06-02 14:42:07 +08:00
},
2025-05-25 23:51:10 +08:00
},
// 开启数据持久化
persist: {
enabled: true,
strategies: [
{
storage: uni.getStorageSync,
2025-06-02 14:42:07 +08:00
paths: ["user", "device", "config"], // 只持久化用户信息
2025-05-25 23:51:10 +08:00
},
],
},
});