import { setPubKey, getPubKey, getToken, setToken, setUid, getUid, removeAll } from '@/utils/auth.js' import { encrypt } from '@/utils/jsencrypt.js' const user = { state: { // 用户认证token token: getToken(), // 用户ID userId: getUid(), // 用户名 userName: null, // 用户权限 roleList: null, // 最后登陆时间 lastLoginTime: null, // 验证码 codeUrl: null, // 验证码登录参数 uid: null, // 公钥 publicKey: getPubKey(), }, mutations: { SET_TOKEN: (state, value) => { state.token = value }, SET_USER_ID: (state, value) => { state.userId = value }, SET_USER_NAME: (state, value) => { state.userName = value }, SET_ROLE_LIST: (state, value) => { state.roleList = value }, SET_LAST_LOGINTIME: (state, value) => { state.lastLoginTime = value }, SET_CODE_IMG: (state, value) => { state.codeUrl = 'data:image/gif;base64,' + value }, SET_UID: (state, value) => { state.uid = value }, SET_PUBKEY: (state, value) => { state.publicKey = value }, }, actions: { // 获取验证码 getCodeImage({ commit, dispatch }) { return new Promise((resolve, reject) => { uni.$http.get('/user/getCodeImage').then(res => { if (res.data.code === 200) { const { verifyCode, uid } = res.data.data commit('SET_CODE_IMG', verifyCode) commit('SET_UID', uid) dispatch('getPubKey') resolve(res) } }) }) }, // 获取公钥 getPubKey({ commit }) { return new Promise((resolve, reject) => { uni.$http.get('/user/getPublicKey').then(res => { if (res.data.code === 200) { const key = res.data.data.replace('\r\n', '') commit('SET_PUBKEY', key) setPubKey(key) resolve(res) } }) }) }, // 用户登录 login({ commit, state }, data) { const pwd = encrypt(data.password, data.publicKey) const obj = { userName: data.userName, password: pwd, verifyCode: data.verifyCode, uid: state.uid } return new Promise((resolve, reject) => { uni.$http.post('/user/phoneUserLogin', obj).then(res => { if (res.data.code === 200) { const token = res.data.data.token const { userId, userName, lastLoginTime, roleList } = res.data.data.user commit('SET_TOKEN', token) setToken(token) commit('SET_USER_ID', userId) setUid(userId) commit('SET_USER_NAME', userName) commit('SET_ROLE_LIST', roleList) commit('SET_LAST_LOGINTIME', lastLoginTime) } resolve(res) }).catch(err => { reject(err) }) }) }, // 退出登录 logout({ commit }) { return new Promise((resolve, reject) => { uni.$http.post('/user/userLoginOut').then(res => { if (res.data.code === 200) { commit('SET_TOKEN', '') commit('SET_USER_ID', '') removeAll() } resolve(res) }).catch(err => { reject(err) }) }) }, //修改密码 updatePassword({ state, commit }, data) { const pwd = encrypt(data.password, state.publicKey) const newPwd = encrypt(data.newPassword, state.publicKey) const obj = { password: pwd, newPassword: newPwd, userId: state.userId, userName: state.userName } return new Promise((resolve, reject) => { uni.$http.post('/user/updatePassWord', obj).then(res => { if (res.data.code === 200) { commit('SET_TOKEN', '') commit('SET_USER_ID', '') removeAll() } resolve(res) }).catch(err => { reject(err) }) }) }, } } const { state, mutations, actions } = user export default { namespaced: true, state, mutations, actions }