123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import Vue from 'vue';
- import Vuex from 'vuex';
- import axios from 'axios';
- import _ from 'lodash';
- import { Notification } from 'element-ui';
- const jwt = require('jsonwebtoken');
- Vue.use(Vuex);
- const api = {
- interface: `/api/auth/login`,
- getUser: `/api/auth/token`,
- logout: '/api/auth/logout',
- getMenu: `/api/auth/user/menus`,
- updatePassword: '/api/user/pwd_edit',
- };
- const state = () => ({});
- const mutations = {};
- const actions = {
- /**
- user:Object required 登陆信息
- router:router 如果跳转就传
- path:String 跳转到的路由位置
- needReturn: Boolean 是否返回结果
- typeCheck: Boolean 是否检查身份对应匹配的前端项目
- isWx: Boolean 是否是微信登陆
- needNotice:Boolean 是否需要提示
- */
- async login({ commit, dispatch }, { user, router, path = '/', needReturn = false, typeCheck = false, isWx = false, needNotice = true }) {
- let res;
- //wx登陆,openid存在,user中是openid和qrcode;正常登陆,user中是mobile和passwd
- if (isWx) res = await this.$axios.$post(`${api.wxLogin}`, user);
- else res = await this.$axios.$post(`${api.interface}`, user);
- const setUser = (token, commit) => {
- localStorage.setItem('token', token);
- dispatch('toGetUser');
- };
- if (res.errcode == '0') {
- setUser(res.data.key, commit);
- Notification({
- title: '登录成功',
- // message: `欢迎,${user.user_name}`,
- type: 'success',
- duration: 2000,
- });
- return res;
- } else {
- if (needReturn) return res;
- else {
- Notification({
- title: '登录失败',
- message: `失败原因:${res.errmsg || '登陆失败'}`,
- type: 'error',
- });
- }
- }
- },
- async toGetUser({ commit }, payload) {
- let key = localStorage.getItem('token');
- if (!key) {
- console.log('游客身份');
- }
- let res = await axios.post(api.getUser, { key: key });
- if (res.data.errcode == '0') {
- let token = _.get(res, `data.data.token`);
- if (token) {
- let user = jwt.decode(token);
- commit('setUser', user, { root: true });
- }
- }
- },
- async toGetMenu({ commit }, payload) {
- const res = await this.$axios.$get(api.getMenu, payload);
- return res;
- },
- async logout({ commit }, payload) {
- let key = localStorage.getItem('token');
- const res = await this.$axios.$post(api.logout, { key: key });
- commit('deleteUser');
- },
- async update({ commit }, payload) {
- let res = await this.$axios.$post(`${api.updatePassword}`, {
- data: payload,
- });
- return res;
- },
- async bind({ commit }, payload) {
- let res = await this.$axios.$post(`${api.bind}`, payload);
- return res;
- },
- async userBind({ commit }, payload) {
- let res = await this.$axios.$post(`${api.userBind}`, payload);
- return res;
- },
- async getQrcode({ commit }, payload) {
- let res = await this.$axios.$get(`${api.connection}`);
- if (res.errcode === 0) return res.data;
- else {
- console.warn('请求qrcode失败');
- }
- },
- async wxCheck({ commit }, payload) {
- let res = await this.$axios.$post(`${api.wxCheck}`, payload);
- return res;
- },
- };
- export default {
- namespaced: true,
- state,
- mutations,
- actions,
- };
|