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 } from "@/common/util/constants" Vue.use(Vuex) export default new Vuex.Store({ state: { token: localStorage.getItem('token')||'', sessionId:localStorage.getItem('sessionId')||'', indexId:localStorage.getItem('indexId')||'', userid:localStorage.getItem('userid')||'', username:localStorage.getItem('username')||'', realname: localStorage.getItem('realname')||'', welcome: localStorage.getItem('welcome')||'', avatar:localStorage.getItem('avatar')||'', orgCode:localStorage.getItem('orgCode')||'', rzdate:localStorage.getItem('rzdate')||'',//入职时间 birthdate:localStorage.getItem('birthdate')||'',//生日 id:localStorage.getItem('id')||'',//查看考勤人员id date:localStorage.getItem('date')||'',//查看考勤人员日期 departId:localStorage.getItem('departId')||'',//查询部门人员信息id teamUserId:localStorage.getItem('teamUserId')||'',//查看团队人员信息id tmsUserId:localStorage.getItem('tmsUserId')||'',//查看团队人员个人信息id wagesDate:localStorage.getItem('wagesDate')||'',//查看个人工资参数 wagesId:localStorage.getItem('wagesId')||'',//查看个人工资参数人事 emilCode:localStorage.getItem('emilCode')||'',//邮箱验证码 chatname:localStorage.getItem('chatname')||'',//邮箱验证码 chatid:localStorage.getItem('chatid')||'', perssion:localStorage.getItem('perssion')||'',//菜单数据 leave:localStorage.getItem('leave')||'',//部门假期 status:localStorage.getItem('status')||'',//人员类别 }, mutations: { SET_STATUS: (state, status) => { localStorage.setItem('status',status) state.status = status }, SET_USERID: (state, userid) => { localStorage.setItem('userid',userid) state.userid = userid }, SET_LEAVE: (state, leave) => { localStorage .setItem('leave',leave) state.leave = leave }, SET_CHAT: (state, {chatname,chatid,chatimg}) => { localStorage.setItem('chatname',chatname) localStorage.setItem('chatid',chatid) localStorage.setItem('chatimg',chatimg) state.chatname = chatname state.chatid=chatid }, SET_INDEXID: (state, indexId) => { localStorage.setItem('indexId',indexId) state.indexId = indexId }, SET_TOKEN: (state, token) => { localStorage.setItem('token',token) state.token = token }, SET_SESSIONID: (state, sessionId) => { localStorage.setItem('sessionId',sessionId) state.sessionId = sessionId }, SET_NAME: (state, { username, realname, welcome }) => { localStorage.setItem('sessionId',username) localStorage.setItem('sessionId',realname) localStorage.setItem('sessionId',welcome) state.username = username state.realname = realname state.welcome = welcome }, SET_RZDATE: (state,rzdate) => { localStorage.setItem('rzdate',rzdate) state.rzdate = rzdate }, SET_NPMUSER: (state, {id,date}) => { localStorage.setItem('id',id) localStorage.setItem('date',date) state.id = id state.date = date }, SET_AVATAR: (state, avatar) => { localStorage .setItem('avatar',avatar) state.avatar = avatar }, SET_DEPARTID: (state,departId) => { localStorage.setItem('departId',departId) state.departId = departId }, SET_TEAMUSERID: (state,teamUserId) => { localStorage.setItem('teamUserId',teamUserId) state.teamUserId = teamUserId }, SET_TEMUSERID: (state,tmsUserId) => { localStorage.setItem('tmsUserId',tmsUserId) state.tmsUserId = tmsUserId }, SET_WAGESDATE: (state,wagesDate) => { localStorage.setItem('wagesDate',wagesDate) state.wagesDate = wagesDate }, SET_WAGESID: (state,wagesId) => { localStorage.setItem('wagesId',wagesId) state.wagesId = wagesId }, SET_BIRTHDATE: (state,birthdate) => { localStorage.setItem('birthdate',birthdate) state.birthdate = birthdate }, SET_EMILCODE: (state,emilCode) => { localStorage.setItem('emilCode',emilCode) state.emilCode = emilCode }, SET_PERSSION:(state,perssion) => { sessionStorage.setItem('perssion',perssion) state.perssion = perssion }, }, actions: { // 登录 mLogin({ commit }, userInfo) { console.log("mLogin",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 let avatar=(userInfo.avatar && userInfo.avatar.length > 0)? api.getFileAccessHttpUrl(userInfo.avatar):'/static/avatar_boy.png' uni.setStorageSync(ACCESS_TOKEN,result.token); uni.setStorageSync(USER_INFO,userInfo); commit('SET_PERSSION', result.permission) commit('SET_TOKEN', result.token) commit('SET_SESSIONID', result.sessionId) commit('SET_AVATAR', avatar) commit('SET_USERID',userInfo.id) commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname}) commit('SET_RZDATE',userInfo.entryDate) commit('SET_BIRTHDATE',userInfo.birthday) resolve(response) }else{ resolve(response) } }).catch(error => { console.log("catch===>response",response) reject(error) }) }) }, //手机号登录 PhoneLogin({ commit }, userInfo) { return new Promise((resolve, reject) => { api.phoneNoLogin(userInfo).then(response => { if(response.code =='200'){ const result = response.result const userInfo = result.userInfo commit('SET_TOKEN', result.token) commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname}) commit('SET_AVATAR', userInfo.avatar) resolve(response) }else{ reject(response) } }).catch(error => { reject(error) }) }) }, // 登出 Logout({ commit, state }) { return new Promise((resolve) => { let logoutToken = state.token; /* commit('SET_TOKEN', '') commit('SET_SESSIONID', '') uni.removeStorageSync(ACCESS_TOKEN) */ api.logout(logoutToken).then(() => { commit('SET_TOKEN', ''); //uni.removeStorageSync(ACCESS_TOKEN); resolve() }).catch(() => { resolve() }) }) }, }, getters:{ token: state => state.token, sessionId: state => state.sessionId, username: state => {state.username=uni.getStorageSync(USER_INFO).username; return state.username}, realname: state => {state.realname=uni.getStorageSync(USER_INFO).realname; return state.realname}, nickname: state => {state.userid=uni.getStorageSync(USER_INFO).realname; return state.user.realname}, userid:state => state.userid, orgCode:state => {state.orgCode=uni.getStorageSync(USER_INFO).orgCode; return state.orgCode}, id:state =>state.id, date:state => state.date, departId:state => state.departId, teamUserId:state => state.teamUserId, tmsUserId:state => state.tmsUserId, wagesDate:state => state.wagesDate, wagesId:state =>state.wagesId, rzdate:state =>state.rzdate, birthdate:state =>state.birthdate, emilCode:state =>state.emilCode, avatar:state => state.avatar, indexId:state => state.indexId, user:state=>state.user, chatname:state=>state.chatname, chatid:state=>state.chatid, perssion:state=>state.perssion, leave:state=>state.leave, status:state=>state.status }, })