|
@@ -0,0 +1,131 @@
|
|
|
+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 = async (token, commit) => {
|
|
|
+ localStorage.setItem('token', token);
|
|
|
+ let userInfo = await dispatch('toGetUser');
|
|
|
+ return userInfo;
|
|
|
+ };
|
|
|
+ let userInfo = {};
|
|
|
+ if (res.errcode == '0') {
|
|
|
+ userInfo = await setUser(res.data.key, commit);
|
|
|
+ Notification({
|
|
|
+ title: '登录成功',
|
|
|
+ // message: `欢迎,${user.user_name}`,
|
|
|
+ type: 'success',
|
|
|
+ duration: 2000,
|
|
|
+ offset: 50,
|
|
|
+ });
|
|
|
+ return userInfo;
|
|
|
+ } else {
|
|
|
+ if (needReturn) return res;
|
|
|
+ else {
|
|
|
+ Notification({
|
|
|
+ title: '登录失败',
|
|
|
+ message: `失败原因:${res.errmsg || '登陆失败'}`,
|
|
|
+ type: 'error',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ async toGetUser({ commit }, payload) {
|
|
|
+ let key = localStorage.getItem('token');
|
|
|
+ if (!key) {
|
|
|
+ if (_.isFunction(payload)) {
|
|
|
+ payload();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let user = localStorage.getItem('user');
|
|
|
+ if (user) {
|
|
|
+ commit('setUser', JSON.parse(user), { root: true });
|
|
|
+ } else {
|
|
|
+ let stamp = new Date().getTime();
|
|
|
+ let name = `游客${stamp}`;
|
|
|
+ let obj = { name, suid: stamp, role: '7' };
|
|
|
+ localStorage.setItem('user', JSON.stringify(obj));
|
|
|
+ commit('setUser', obj, { root: true });
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let res = await axios.post(api.getUser, { key: key });
|
|
|
+ let user = {};
|
|
|
+ if (res.data.errcode == '0') {
|
|
|
+ let token = _.get(res, `data.data.token`);
|
|
|
+ if (token) {
|
|
|
+ user = jwt.decode(token);
|
|
|
+ // console.log(user);
|
|
|
+ commit('setUser', user, { root: true });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ },
|
|
|
+ async toGetMenu({ commit }, payload) {
|
|
|
+ const res = await this.$axios.$get(api.getMenu, payload);
|
|
|
+ return res;
|
|
|
+ },
|
|
|
+ async logout({ commit }, payload) {
|
|
|
+ let key = localStorage.removeItem('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,
|
|
|
+};
|