peixue-dev/peidu/uniapp/store/modules/tenant.js

113 lines
2.0 KiB
JavaScript

/**
* 租户状态管理
*/
const state = {
// 当前租户ID
currentTenantId: null,
// 当前租户信息
currentTenant: null,
// 租户列表
tenantList: [],
// 是否是总部
isHeadquarters: false
}
const mutations = {
/**
* 设置当前租户ID
*/
SET_TENANT_ID(state, tenantId) {
state.currentTenantId = tenantId
state.isHeadquarters = tenantId === 1 || tenantId === null
// 保存到本地存储
uni.setStorageSync('tenantId', tenantId)
},
/**
* 设置当前租户信息
*/
SET_TENANT(state, tenant) {
state.currentTenant = tenant
if (tenant) {
state.currentTenantId = tenant.id
state.isHeadquarters = tenant.id === 1
}
},
/**
* 设置租户列表
*/
SET_TENANT_LIST(state, list) {
state.tenantList = list
},
/**
* 清除租户信息
*/
CLEAR_TENANT(state) {
state.currentTenantId = null
state.currentTenant = null
state.isHeadquarters = false
uni.removeStorageSync('tenantId')
}
}
const actions = {
/**
* 设置租户ID
*/
setTenantId({ commit }, tenantId) {
commit('SET_TENANT_ID', tenantId)
},
/**
* 设置租户信息
*/
setTenant({ commit }, tenant) {
commit('SET_TENANT', tenant)
},
/**
* 切换租户
*/
switchTenant({ commit }, tenant) {
commit('SET_TENANT', tenant)
// 刷新页面数据
uni.reLaunch({
url: '/pages/index/index'
})
},
/**
* 从本地存储恢复租户ID
*/
restoreTenantId({ commit }) {
const tenantId = uni.getStorageSync('tenantId')
if (tenantId) {
commit('SET_TENANT_ID', tenantId)
}
},
/**
* 清除租户信息
*/
clearTenant({ commit }) {
commit('CLEAR_TENANT')
}
}
const getters = {
currentTenantId: state => state.currentTenantId,
currentTenant: state => state.currentTenant,
tenantList: state => state.tenantList,
isHeadquarters: state => state.isHeadquarters
}
export default {
namespaced: true,
state,
mutations,
actions,
getters
}