【前端】纸掌柜h5端
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.

281 lines
7.6 KiB

import Vue from 'vue'
import Vuex from 'vuex'
import { isObject, isArray } from '@/utils/is'
let qnToken = null,
/**
* @value avatar 头像
* @value name 当前账号职员名称
* @value userId 用户id
* @value mobile 手机号
*/
userInfo = null,
/**
* @value id 企业id
* @value name 企业名称
* @value fddEnterpriseStatus 法大大认证状态 。1未认证,2认证进行中,3认证成功,4认证失败
*/
companyInfo = null,
uecToken = null,
searchHistory = null,
/**
* @value supplierId 当前被分析的供应商id
*/
supplierId = null
const companyInfoParams = ['id', 'name', 'fddEnterpriseStatus']
const userInfoParams = ['name', 'userId', 'mobile', 'avatar']
try {
uecToken = uni.getStorageSync('uecToken')
qnToken = uni.getStorageSync('qnToken')
supplierId = uni.getStorageSync('supplierId')
userInfo = uni.getStorageSync('userInfo')
searchHistory = uni.getStorageSync('searchHistory')
if (searchHistory) {
searchHistory = JSON.parse(searchHistory)
}
if (userInfo) {
userInfo = JSON.parse(userInfo)
}
companyInfo = uni.getStorageSync('companyInfo')
if (companyInfo) {
companyInfo = JSON.parse(companyInfo)
}
} catch (e) {
console.error('初始化错误:', e)
}
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
uecToken: uecToken || '',
qnToken: qnToken || '', // token
supplierId: supplierId || '', // 供应商id
userInfo: userInfo || {}, // 用户信息
companyInfo: companyInfo || {}, // 纸盘商信息
nextPage: {
name: '',
data: {}
},
searchHistory: searchHistory || [],
/**
* 监听cache的type即可,每次取值后都必须调用commit('resetCache')
*/
cache: {
type: 'none',
data: null
}
},
mutations: {
setCache(state, { type, data }) {
state.cache.type = type
state.cache.data = data
},
resetCache(state) {
state.cache.type = 'none'
state.cache.data = null
},
setUecToken(state, token) {
try {
uni.setStorageSync('uecToken', token)
state.uecToken = token
} catch (e) {
console.error('更改uecToken失败:', e)
}
},
removeUecToken(state) {
try {
uni.removeStorageSync('uecToken')
state.uecToken = ''
} catch (e) {
console.error('删除uecToken失败:', e)
}
},
setToken(state, token) {
try {
uni.setStorageSync('qnToken', token)
state.qnToken = token
} catch (e) {
console.error('更改token失败:', e)
}
},
removeToken(state) {
try {
uni.removeStorageSync('qnToken')
state.qnToken = ''
} catch (e) {
console.error('删除token失败:', e)
}
},
setUserInfo(state, userInfo) {
if (!isObject(userInfo)) {
console.error('userInfo必须是对象')
return
}
for (let i = 0; i < userInfoParams.length; i++) {
if (userInfo[userInfoParams[i]] === undefined) {
console.error('userInfo必须包含' + userInfoParams[i] + '属性')
return
}
}
try {
uni.setStorageSync('userInfo', JSON.stringify(userInfo))
state.userInfo = userInfo
} catch (e) {
console.error('设置userInfo失败:', e)
}
},
/**
* 更改当前用户信息
* @param {*} state 状态
* @param {arr} map 以key-value形式存储的数组
* @value key 需要更改的key
* @value value 更改后的值
*/
changeUserInfo(state, map) {
if (!isArray(map)) {
console.error('map必须是数组')
return
}
let userInfo = state.userInfo
map.forEach((item) => {
if (userInfoParams.includes(item.key)) {
userInfo[item.key] = item.value
}
})
try {
uni.setStorageSync('userInfo', JSON.stringify(userInfo))
state.userInfo = userInfo
} catch (e) {
console.error('更改userInfo失败:', e)
}
},
removeUserInfo(state) {
try {
uni.removeStorageSync('userInfo')
state.userInfo = {}
} catch (e) {
console.error('删除userInfo失败:', e)
}
},
setCompanyInfo(state, companyInfo) {
if (!isObject(companyInfo)) {
console.error('companyInfo必须是对象')
return
}
for (let i = 0; i < companyInfoParams.length; i++) {
if (companyInfo[companyInfoParams[i]] === undefined) {
console.error(`companyInfo必须包含${companyInfoParams[i]}`)
return
}
}
try {
uni.setStorageSync('companyInfo', JSON.stringify(companyInfo))
state.companyInfo = companyInfo
} catch (e) {
console.error('设置companyInfo失败:', e)
}
},
/**
* 更改当前供应商信息
* @param {*} state 状态
* @param {arr} map 以key-value形式存储的数组
* @value key 需要更改的key
* @value value 更改后的值
*/
changeCompanyInfo(state, map) {
if (!isArray(map)) {
console.error('map必须是数组')
return
}
let companyInfo = state.companyInfo
map.forEach((item) => {
if (companyInfoParams.includes(item.key)) {
companyInfo[item.key] = item.value
}
})
try {
uni.setStorageSync('companyInfo', JSON.stringify(companyInfo))
state.companyInfo = companyInfo
} catch (e) {
console.error('更改companyInfo失败:', e)
}
},
removeCompanyInfo(state) {
try {
uni.removeStorageSync('companyInfo')
state.companyInfo = {}
} catch (e) {
console.error('删除companyInfo失败:', e)
}
},
setNextPage(state, nextPage) {
if (!isObject(nextPage)) {
console.error('nextPage必须是对象')
return
}
state.nextPage.name = nextPage.name || ''
state.nextPage.data = nextPage.data || {}
},
removeNextPage(state) {
state.nextPage.name = ''
state.nextPage.data = {}
},
setSearchHistory(state, searchHistory) {
if (!isArray(searchHistory)) {
console.error('searchHistory必须是数组')
return
}
try {
uni.setStorageSync('searchHistory', JSON.stringify(searchHistory))
state.searchHistory = searchHistory
} catch (e) {
console.error('更改searchHistory失败:', e)
}
},
clearSearchHistory(state) {
try {
uni.removeStorageSync('searchHistory')
state.searchHistory = []
} catch (e) {
console.error('删除searchHistory失败:', e)
}
},
setSupplierId(state, id) {
try {
uni.setStorageSync('supplierId', id)
state.supplierId = id
} catch (e) {
console.error('更改supplierId失败:', e)
}
},
removeSupplierId(state) {
try {
uni.removeStorageSync('supplierId')
state.supplierId = ''
} catch (e) {
console.error('删除supplierId失败:', e)
}
}
},
actions: {
logout({ commit }) {
commit('removeUecToken')
commit('removeToken')
commit('removeUserInfo')
commit('removeCompanyInfo')
},
addSearchHistory({ commit, state }, searchHistory) {
const arr = [...state.searchHistory] // 单层数组直接解构
let index = arr.findIndex((item) => item === searchHistory)
if (index > -1) {
arr.splice(index, 1)
}
arr.unshift(searchHistory)
if (arr.length > 10) {
arr = arr.slice(0, 10)
}
commit('setSearchHistory', arr)
}
}
})
export default store