alipay-emulator/store/index.js

120 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { reactive, ref, watch } from 'vue';
import { storage } from '../utils/storage';
// 定义应用的全局状态
export const store = reactive({
// 用户信息
userInfo: storage.get('userInfo') || null,
// 应用设置
settings: storage.get('settings') || {
theme: 'light',
language: 'zh-CN',
notifications: true
},
// 系统信息
systemInfo: {
statusBarHeight: 0,
windowHeight: 0,
windowWidth: 0
},
// 账单列表
billList: storage.get('bill_list') || [],
});
// 定义操作方法
export const useStore = () => {
// 用户相关操作
const setUserInfo = (userInfo) => {
store.userInfo = userInfo;
};
const clearUserInfo = () => {
store.userInfo = null;
};
// 设置相关操作
const updateSettings = (settings) => {
store.settings = { ...store.settings, ...settings };
};
// 账单相关操作
const addBill = (bill) => {
store.billList.unshift(bill);
return bill;
};
const updateBill = (id, updates) => {
const index = store.billList.findIndex(bill => bill.id === id);
if (index !== -1) {
store.billList[index] = { ...store.billList[index], ...updates };
return store.billList[index];
}
return null;
};
const deleteBill = (id) => {
const index = store.billList.findIndex(bill => bill.id === id);
if (index !== -1) {
store.billList.splice(index, 1);
return true;
}
return false;
};
const getBillList = () => {
return store.billList
}
// 数据持久化
// 监听store变化自动保存到本地存储
watch(
() => store.userInfo,
(newValue) => storage.set('userInfo', newValue),
{ deep: true }
);
watch(
() => store.settings,
(newValue) => storage.set('settings', newValue),
{ deep: true }
);
watch(
() => store.billList,
(newValue) => storage.set('bill_list', newValue),
{ deep: true }
);
// 获取系统信息
const getSystemInfo = () => {
uni.getSystemInfo({
success: (res) => {
store.systemInfo.statusBarHeight = res.statusBarHeight || 0;
store.systemInfo.windowHeight = res.windowHeight || 0;
store.systemInfo.windowWidth = res.windowWidth || 0;
},
fail: (err) => {
console.error('获取系统信息失败:', err);
}
});
};
return {
store,
setUserInfo,
clearUserInfo,
updateSettings,
addBill,
updateBill,
deleteBill,
getBillList,
getSystemInfo
};
};