import Vue from 'vue'; import Vuex from 'vuex'; import _ from 'lodash'; import { Notification } from 'element-ui'; const jwt = require('jsonwebtoken'); Vue.use(Vuex); const api = { interface: `/api/market/user/login`, 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 }, { 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); commit('setUser', token, { root: true }); }; if (res.errcode == '0') { setUser(res.data, 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 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, };