import Vue from 'vue'
|
import Vuex from 'vuex'
|
import api from "@/api/api"
|
import MinCache from '@/common/util/MinCache.js'
|
import {
|
ACCESS_TOKEN,
|
USER_NAME,
|
USER_INFO,
|
X_TENANT_ID
|
} from "@/common/util/constants"
|
|
Vue.use(Vuex)
|
|
export default new Vuex.Store({
|
state: {
|
auth: [],
|
token: '',
|
userid: '',
|
username: '',
|
realname: '',
|
welcome: '',
|
avatar: '',
|
user: {}, // 补充用户信息对象
|
tenantId: '', // 补充租户ID
|
// 产线管理相关状态
|
lineList: [], // 产线列表
|
currentLineId: uni.getStorageSync('currentLineId') || null, // 当前选中的产线ID
|
currentLineName: uni.getStorageSync('currentLineName') || '', // 当前选中的产线名称
|
currentLineInfo: null, // 当前选中的产线详细信息
|
currentLineType: uni.getStorageSync('currentLineType') || '' // 新增:当前选中的产线类型
|
},
|
mutations: {
|
SET_AUTH(state, auth) {
|
state.auth = auth
|
},
|
SET_TOKEN: (state, token) => {
|
state.token = token
|
},
|
SET_NAME: (state, {
|
username,
|
realname,
|
welcome
|
}) => {
|
state.username = username
|
state.realname = realname
|
state.welcome = welcome
|
},
|
SET_AVATAR: (state, avatar) => {
|
state.avatar = avatar
|
},
|
SET_ID: (state, id) => {
|
state.userid = id
|
},
|
SET_INFO: (state, userInfo) => {
|
state.user = userInfo
|
},
|
SET_TENANTID: (state, tenantId) => {
|
state.tenantId = tenantId
|
},
|
// 产线相关mutations
|
SET_LINE_LIST(state, list) {
|
state.lineList = list
|
console.log('Vuex SET_LINE_LIST: 产线列表已更新', list);
|
},
|
SET_CURRENT_LINE_ID(state, lineId) {
|
state.currentLineId = lineId
|
uni.setStorageSync('currentLineId', lineId)
|
|
// 同步更新当前产线信息、名称和类型
|
if (lineId && state.lineList.length > 0) {
|
state.currentLineInfo = state.lineList.find(line => line.id === lineId) || null
|
// 从产线信息中提取名称并保存
|
state.currentLineName = state.currentLineInfo ? state.currentLineInfo.name : ''
|
// 新增:从产线信息中提取类型并保存
|
state.currentLineType = state.currentLineInfo ? state.currentLineInfo.type : ''
|
|
uni.setStorageSync('currentLineName', state.currentLineName)
|
// 新增:将类型同步保存到本地存储
|
uni.setStorageSync('currentLineType', state.currentLineType)
|
} else {
|
state.currentLineInfo = null
|
state.currentLineName = ''
|
state.currentLineType = '' // 新增:清空类型
|
|
uni.removeStorageSync('currentLineName')
|
uni.removeStorageSync('currentLineType') // 新增:清除本地存储的类型
|
}
|
console.log('Vuex SET_CURRENT_LINE_ID: 当前产线ID已更新', lineId);
|
console.log('Vuex SET_CURRENT_LINE_NAME: 当前产线名称已更新', state.currentLineName);
|
console.log('Vuex SET_CURRENT_LINE_TYPE: 当前产线类型已更新', state.currentLineType); // 新增:打印类型信息
|
}
|
},
|
actions: {
|
// 账号登录
|
mLogin({
|
commit
|
}, userInfo) {
|
return new Promise((resolve, reject) => {
|
api.login(userInfo).then(response => {
|
if (response.data.code == 200) {
|
const result = response.data.result
|
const userInfo = result.userInfo
|
uni.setStorageSync(ACCESS_TOKEN, result.token);
|
uni.setStorageSync("isLogin", true);
|
uni.setStorageSync("userId", userInfo.id);
|
uni.setStorageSync(USER_INFO, userInfo);
|
commit('SET_TOKEN', result.token)
|
commit('SET_AVATAR', userInfo.avatar)
|
commit('SET_NAME', {
|
username: userInfo.username,
|
realname: userInfo.realname
|
})
|
commit('SET_ID', userInfo.id)
|
commit('SET_INFO', userInfo)
|
resolve(response)
|
} else {
|
resolve(response)
|
}
|
}).catch(error => {
|
console.log("登录失败:", error)
|
reject(error)
|
})
|
})
|
},
|
// 手机号登录
|
PhoneLogin({
|
commit
|
}, userInfo) {
|
return new Promise((resolve, reject) => {
|
api.phoneNoLogin(userInfo).then(response => {
|
if (response.data.code == 200) {
|
const result = response.data.result
|
const userInfo = result.userInfo
|
uni.setStorageSync(ACCESS_TOKEN, result.token);
|
uni.setStorageSync(USER_INFO, userInfo);
|
commit('SET_TOKEN', result.token)
|
commit('SET_NAME', {
|
username: userInfo.username,
|
realname: userInfo.realname
|
})
|
commit('SET_AVATAR', userInfo.avatar)
|
commit('SET_ID', userInfo.id)
|
commit('SET_INFO', userInfo)
|
resolve(response)
|
} else {
|
reject(response)
|
}
|
}).catch(error => {
|
reject(error)
|
})
|
})
|
},
|
// 第三方登录
|
ThirdLogin({
|
commit
|
}, param) {
|
return new Promise((resolve, reject) => {
|
api.thirdLogin(param.token, param.thirdType).then(response => {
|
if (response.data.code == '200') {
|
const result = response.data.result
|
const userInfo = result.userInfo
|
uni.setStorageSync(ACCESS_TOKEN, result.token);
|
uni.setStorageSync(USER_INFO, userInfo);
|
uni.setStorageSync("userId", userInfo.id);
|
uni.setStorageSync(X_TENANT_ID, userInfo.loginTenantId);
|
|
commit('SET_TOKEN', result.token)
|
commit('SET_AVATAR', userInfo.avatar)
|
commit('SET_NAME', {
|
username: userInfo.username,
|
realname: userInfo.realname
|
})
|
commit('SET_ID', userInfo.id)
|
commit('SET_INFO', userInfo)
|
commit('SET_TENANTID', userInfo.loginTenantId)
|
resolve(response)
|
} else {
|
reject(response)
|
}
|
}).catch(error => {
|
reject(error)
|
})
|
})
|
},
|
// 登出
|
Logout({
|
commit,
|
state
|
}) {
|
return new Promise((resolve) => {
|
let logoutToken = state.token;
|
commit('SET_TOKEN', '')
|
uni.removeStorageSync(ACCESS_TOKEN)
|
uni.removeStorageSync("isLogin")
|
uni.removeStorageSync("userId")
|
// 清除产线选择状态
|
commit('SET_CURRENT_LINE_ID', null)
|
api.logout(logoutToken).then(() => {
|
resolve()
|
}).catch(() => {
|
resolve()
|
})
|
})
|
},
|
saveAuth({
|
commit
|
}, auth) {
|
commit('SET_AUTH', auth)
|
},
|
|
// 获取产线列表(已包含type字段)
|
fetchLineList({ commit, state }) {
|
return new Promise((resolve, reject) => {
|
api.getLineList({
|
headers: {
|
'Authorization': `Bearer ${state.token}`
|
}
|
}).then(response => {
|
console.log('fetchLineList接口返回原始数据:', response);
|
if (response.data.code === 200) {
|
const lineList = (response.data.result || []).map(line => ({
|
id: line.value,
|
name: line.text,
|
type: line.type // 确保映射type字段
|
}));
|
commit('SET_LINE_LIST', lineList);
|
resolve(lineList);
|
} else {
|
reject(new Error(`获取产线列表失败: ${response.data.message || '未知错误'}`));
|
}
|
}).catch(error => {
|
console.error('获取产线列表出错:', error);
|
reject(error);
|
});
|
});
|
},
|
|
// 设置当前产线(保持不变,类型通过mutation自动同步)
|
setCurrentLine({ commit }, lineId) {
|
return new Promise((resolve) => {
|
commit('SET_CURRENT_LINE_ID', lineId)
|
resolve()
|
})
|
}
|
},
|
getters: {
|
getAuth: state => state.auth,
|
token: state => state.token,
|
username: state => state.username || uni.getStorageSync(USER_INFO)?.username || '',
|
nickname: state => state.realname || uni.getStorageSync(USER_INFO)?.realname || '',
|
avatar: state => state.avatar || uni.getStorageSync(USER_INFO)?.avatar || '',
|
userid: state => state.userid || uni.getStorageSync(USER_INFO)?.id || '',
|
// 产线相关getters
|
lineList: state => state.lineList,
|
currentLineId: state => state.currentLineId,
|
currentLineName: state => state.currentLineName,
|
currentLineInfo: state => state.currentLineInfo,
|
currentLineType: state => state.currentLineType, // 新增:获取当前产线类型的getter
|
hasSelectedLine: state => !!state.currentLineId
|
}
|
})
|