完善页面6
This commit is contained in:
92
stores/useMockData.js
Normal file
92
stores/useMockData.js
Normal file
@@ -0,0 +1,92 @@
|
||||
// ⚠ Mock 数据,后续请替换成真实接口。
|
||||
import { reactive, watch } from "vue";
|
||||
|
||||
export const useMockData = () => {
|
||||
// 从本地存储读取初始数据,如果没有则使用默认值
|
||||
const initialCategories = uni.getStorageSync("mockCategories") || [
|
||||
{ id: 101, name: "分类 A", status: 1, sort: 1 },
|
||||
{ id: 102, name: "分类 B", status: 1, sort: 2 },
|
||||
];
|
||||
|
||||
const initialArticles = uni.getStorageSync("mockArticles") || [
|
||||
{
|
||||
id: 1,
|
||||
title: "文章 1",
|
||||
summary: "摘要 1",
|
||||
cover: "",
|
||||
categoryId: 101,
|
||||
content: "<p>富文本内容 1</p>",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "文章 2",
|
||||
summary: "摘要 2",
|
||||
cover: "",
|
||||
categoryId: 102,
|
||||
content: "<p>富文本内容 2</p>",
|
||||
},
|
||||
];
|
||||
|
||||
const mockCategories = reactive(initialCategories);
|
||||
const mockArticles = reactive(initialArticles);
|
||||
|
||||
// 监听数据变化并保存到本地存储
|
||||
watch(
|
||||
mockCategories,
|
||||
(newVal) => {
|
||||
uni.setStorageSync("mockCategories", JSON.parse(JSON.stringify(newVal)));
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
mockArticles,
|
||||
(newVal) => {
|
||||
uni.setStorageSync("mockArticles", JSON.parse(JSON.stringify(newVal)));
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
const addCategory = (category) => {
|
||||
const newId =
|
||||
mockCategories.length > 0
|
||||
? Math.max(...mockCategories.map((c) => c.id)) + 1
|
||||
: 101;
|
||||
mockCategories.push({ ...category, id: newId });
|
||||
};
|
||||
|
||||
const updateCategory = (id, category) => {
|
||||
const index = mockCategories.findIndex((c) => c.id === id);
|
||||
if (index !== -1) {
|
||||
Object.assign(mockCategories[index], category);
|
||||
}
|
||||
};
|
||||
|
||||
const addArticle = (article) => {
|
||||
const newId =
|
||||
mockArticles.length > 0
|
||||
? Math.max(...mockArticles.map((a) => a.id)) + 1
|
||||
: 1;
|
||||
mockArticles.push({ ...article, id: newId });
|
||||
};
|
||||
|
||||
const updateArticle = (id, article) => {
|
||||
const index = mockArticles.findIndex((a) => a.id === id);
|
||||
if (index !== -1) {
|
||||
Object.assign(mockArticles[index], article);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
mockCategories,
|
||||
mockArticles,
|
||||
addCategory,
|
||||
updateCategory,
|
||||
addArticle,
|
||||
updateArticle,
|
||||
};
|
||||
};
|
||||
|
||||
// 单例模式,确保全局共享状态
|
||||
const mockStore = useMockData();
|
||||
export default mockStore;
|
||||
Reference in New Issue
Block a user