/* * @Author: liuzhenlin 461480418@qq.ocm * @Date: 2023-01-19 16:03:20 * @LastEditors: liuzhenlin * @LastEditTime: 2023-01-20 10:23:22 * @Description: file content * @FilePath: \crm\store\index.js */ import Vue from 'vue' import Vuex from 'vuex' import userApi from '../api/system/user' import to from 'await-to-js' Vue.use(Vuex) const store = new Vuex.Store({ state: { token: '', //用户token userId: '', username: '', nickName: '', postName: '', avatar: '', followPageDetail: {}, //跟进页面的客户/项目信息 phone: '', }, getters: { followPageDetail: (state) => state.followPageDetail, userId: (state) => state.userId, token: (state) => state.token, username: (state) => state.username, nickName: (state) => state.nickName, postName: (state) => state.postName, avatar: (state) => state.avatar, phone: (state) => state.phone, }, //mutations定义同步操作的方法 mutations: { /** * @description 设置token * @param {*} state * @param {*} token */ setDetails(state, info) { state.followPageDetail = info }, /** * @description 设置token * @param {*} state * @param {*} token */ setToken(state, token) { state.token = token uni.setStorageSync('opms_token', token) }, /** * @description 设置用户id * @param {*} state * @param {*} userId */ setUserId(state, userId) { state.userId = userId }, /** * @description 设置用户名字 * @param {*} state * @param {*} username */ setUsername(state, username) { state.username = username }, /** * @description * @param {*} state * @param {*} nickName */ setNickName(state, nickName) { state.nickName = nickName }, setPostName(state, postName) { state.postName = postName }, /** * @description 设置用头像 * @param {*} state * @param {*} avatar */ setAvatar(state, avatar) { state.avatar = avatar }, /** * @description 手机号 * @param {*} state * @param {*} phone */ setPhone(state, phone) { state.phone = phone }, }, actions: { /** * @description 登录 * @param {*} { commit } * @param {*} userInfo */ async login({ commit, dispatch }, userInfo) { const [err, res] = await to(userApi.login(userInfo)) if (err) return if (res.code == 200) { if (userInfo.rememberPassword[0] && userInfo.rememberPassword[0] == 'checked') { uni.setStorageSync('userInfo', { username: userInfo.username, password: userInfo.password, }) } else { uni.removeStorageSync('userInfo') } uni.showToast({ title: '登录成功', icon: 'none', complete: async () => { commit('setToken', res.data.token) uni.reLaunch({ url: '/pages/home/index', }) await dispatch('getUserInfo') }, }) } }, /** * @description 获取用户信息接口 * @param {*} { commit, dispatch, state } * @returns */ async getUserInfo({ commit }) { const [err, res] = await to(userApi.getUserInfo()) if (err) return const { id, userName, nickName, avatar, postName, phone } = res.data.entity if (id) commit('setUserId', id) if (userName) commit('setUsername', userName) if (nickName) commit('setNickName', nickName) if (postName) commit('setPostName', nickName) if (avatar) commit('setAvatar', avatar) if (phone) commit('setPhone', phone) }, /** * @description 退出登录 * @param {*} { dispatch } */ async logout({ dispatch }) { await userApi.logout() await dispatch('resetAll') }, /** * @description 重置token、roles、permission、router、tabsBar等 * @param {*} { commit, dispatch } */ async resetAll({ commit }) { commit('setUsername', '游客') commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif') commit('setToken', '') uni.removeStorageSync('opms_token') }, }, }) export default store