1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { rsaEncrypt } from '@/utils/jsencrypt';
- import { login, byPhone } from '@/api/user/login';
- import { info } from '@/api/user/my';
- import { logout } from '@/api/user/my.js';
- const user = {
- namespaced: true,
- state: {
- userInfo: uni.getStorageSync('userInfo') || {},
- token: uni.getStorageSync('token'),
- },
- mutations: {
- SET_USER_INFO: (state, data) => {
- state.userInfo = data;
- uni.setStorageSync('userInfo', data); // 存储userInfo
- },
- SET_TOKEN: (state, token) => {
- state.token = token;
- uni.setStorageSync('token', token); // 存储token
- },
- },
- actions: {
- // 密码登录/验证码登录
- async Login({ commit }, userData) {
- try {
- uni.$c.loading('登录中');
- const userName = rsaEncrypt(userData.userName);
- const { password, code } = userData;
- const params = {
- userName,
- };
- if (password) {
- params.password = rsaEncrypt(password);
- }
- if (code) {
- params.code = code;
- }
- const loginMode = userData.loginMode;
- const requestFn = loginMode === '0' ? login : byPhone;
- const res = await requestFn(params);
- uni.hideLoading();
- const token = res.data.token;
- commit('SET_TOKEN', token); // 存储用户信息
- await uni.$c.toast({
- title: '登录成功',
- icon: 'success',
- mask: true,
- duration: 1000,
- });
- uni.switchTab({ url: '/pages/home/index' }); // 去首页
- return res;
- } catch (e) {
- uni.hideLoading();
- console.error(e);
- }
- },
- // 获取用户信息
- async GetInfo({ commit }) {
- try {
- uni.$c.loading();
- const res = await info();
- uni.hideLoading();
- commit('SET_USER_INFO', res.data);
- return res;
- } catch (e) {
- console.error(e);
- }
- },
- // 退出系统
- async LogOut({ dispatch }) {
- await logout();
- dispatch('ClearUserInfo');
- },
- ClearUserInfo({ commit }) {
- commit('SET_USER_INFO', ''); // 清空用户信息
- commit('SET_TOKEN', ''); // 清空token
- uni.reLaunch({
- // 跳回登录页
- url: '/pages/user/login/index',
- });
- },
- },
- };
- export default user;
|