alipay-emulator/store/index.js

167 lines
3.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
},
// 示例数据:待办事项
todos: storage.get('todos') || [],
// 示例数据:商品列表
products: storage.get('products') || [],
});
// 定义操作方法
export const useStore = () => {
// 用户相关操作
const setUserInfo = (userInfo) => {
store.userInfo = userInfo;
};
const clearUserInfo = () => {
store.userInfo = null;
};
// 设置相关操作
const updateSettings = (settings) => {
store.settings = { ...store.settings, ...settings };
};
// 待办事项相关操作
const addTodo = (todo) => {
const newTodo = {
id: Date.now(),
...todo,
completed: false,
createdAt: new Date().toISOString()
};
store.todos.push(newTodo);
return newTodo;
};
const updateTodo = (id, updates) => {
const index = store.todos.findIndex(todo => todo.id === id);
if (index !== -1) {
store.todos[index] = { ...store.todos[index], ...updates };
return store.todos[index];
}
return null;
};
const deleteTodo = (id) => {
const index = store.todos.findIndex(todo => todo.id === id);
if (index !== -1) {
store.todos.splice(index, 1);
return true;
}
return false;
};
// 商品相关操作
const addProduct = (product) => {
const newProduct = {
id: Date.now(),
...product,
createdAt: new Date().toISOString()
};
store.products.push(newProduct);
return newProduct;
};
const updateProduct = (id, updates) => {
const index = store.products.findIndex(product => product.id === id);
if (index !== -1) {
store.products[index] = { ...store.products[index], ...updates };
return store.products[index];
}
return null;
};
const deleteProduct = (id) => {
const index = store.products.findIndex(product => product.id === id);
if (index !== -1) {
store.products.splice(index, 1);
return true;
}
return false;
};
// 数据持久化
// 监听store变化自动保存到本地存储
watch(
() => store.userInfo,
(newValue) => storage.set('userInfo', newValue),
{ deep: true }
);
watch(
() => store.settings,
(newValue) => storage.set('settings', newValue),
{ deep: true }
);
watch(
() => store.todos,
(newValue) => storage.set('todos', newValue),
{ deep: true }
);
watch(
() => store.products,
(newValue) => storage.set('products', 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,
addTodo,
updateTodo,
deleteTodo,
addProduct,
updateProduct,
deleteProduct,
getSystemInfo
};
};