You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.6 KiB
82 lines
1.6 KiB
// store/groupStore.js
|
|
const groupModule = {
|
|
namespaced: true,
|
|
|
|
state: () => ({
|
|
groups: []
|
|
}),
|
|
|
|
mutations: {
|
|
SET_GROUPS(state, groups) {
|
|
state.groups = groups
|
|
},
|
|
ADD_GROUP(state, group) {
|
|
// 使用 unshift 插入到列表头部(与 Pinia 保持一致)
|
|
state.groups.unshift(group)
|
|
},
|
|
UPDATE_GROUP(state, group) {
|
|
const g = state.groups.find(item => item.id === group.id)
|
|
if (g) {
|
|
Object.assign(g, group)
|
|
}
|
|
},
|
|
REMOVE_GROUP(state, id) {
|
|
state.groups.filter(g => g.id === id).forEach(g => g.quit = true)
|
|
},
|
|
CLEAR_GROUPS(state) {
|
|
state.groups = []
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
// 设置群组列表
|
|
setGroups({ commit }, groups) {
|
|
commit('SET_GROUPS', groups)
|
|
},
|
|
|
|
// 添加或更新群组
|
|
addGroup({ commit, state }, group) {
|
|
const exists = state.groups.some(g => g.id === group.id)
|
|
if (exists) {
|
|
commit('UPDATE_GROUP', group)
|
|
} else {
|
|
commit('ADD_GROUP', group)
|
|
}
|
|
},
|
|
|
|
// 删除群组(逻辑删除)
|
|
removeGroup({ commit }, id) {
|
|
commit('REMOVE_GROUP', id)
|
|
},
|
|
|
|
// 更新群组信息
|
|
updateGroup({ commit }, group) {
|
|
commit('UPDATE_GROUP', group)
|
|
},
|
|
|
|
// 清空群组列表
|
|
clear({ commit }) {
|
|
commit('CLEAR_GROUPS')
|
|
},
|
|
|
|
// 加载群组列表(异步)
|
|
async loadGroup({ dispatch }) {
|
|
try {
|
|
const groups = await uni.$http.get('/api/group/list')
|
|
dispatch('setGroups', groups.data.data)
|
|
} catch (error) {
|
|
// 将错误抛出,让调用方处理
|
|
throw error
|
|
}
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
findGroup: (state) => (id) => {
|
|
return state.groups.find(g => g.id === id)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default groupModule
|