login.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Vue from 'vue';
  2. import Vuex from 'vuex';
  3. import axios from 'axios';
  4. import _ from 'lodash';
  5. import { Notification } from 'element-ui';
  6. const jwt = require('jsonwebtoken');
  7. Vue.use(Vuex);
  8. const api = {
  9. interface: `/api/auth/login`,
  10. getUser: `/api/auth/token`,
  11. logout: '/api/auth/logout',
  12. getMenu: `/api/auth/user/menus`,
  13. updatePassword: '/api/user/pwd_edit',
  14. };
  15. const state = () => ({});
  16. const mutations = {};
  17. const actions = {
  18. /**
  19. user:Object required 登陆信息
  20. router:router 如果跳转就传
  21. path:String 跳转到的路由位置
  22. needReturn: Boolean 是否返回结果
  23. typeCheck: Boolean 是否检查身份对应匹配的前端项目
  24. isWx: Boolean 是否是微信登陆
  25. needNotice:Boolean 是否需要提示
  26. */
  27. async login({ commit, dispatch }, { user, router, path = '/', needReturn = false, typeCheck = false, isWx = false, needNotice = true }) {
  28. let res;
  29. //wx登陆,openid存在,user中是openid和qrcode;正常登陆,user中是mobile和passwd
  30. if (isWx) res = await this.$axios.$post(`${api.wxLogin}`, user);
  31. else res = await this.$axios.$post(`${api.interface}`, user);
  32. const setUser = (token, commit) => {
  33. localStorage.setItem('token', token);
  34. dispatch('toGetUser');
  35. };
  36. if (res.errcode == '0') {
  37. setUser(res.data.key, commit);
  38. Notification({
  39. title: '登录成功',
  40. // message: `欢迎,${user.user_name}`,
  41. type: 'success',
  42. duration: 2000,
  43. });
  44. return res;
  45. } else {
  46. if (needReturn) return res;
  47. else {
  48. Notification({
  49. title: '登录失败',
  50. message: `失败原因:${res.errmsg || '登陆失败'}`,
  51. type: 'error',
  52. });
  53. }
  54. }
  55. },
  56. async toGetUser({ commit }, payload) {
  57. let key = localStorage.getItem('token');
  58. if (!key) {
  59. console.log('游客身份');
  60. }
  61. let res = await axios.post(api.getUser, { key: key });
  62. if (res.data.errcode == '0') {
  63. let token = _.get(res, `data.data.token`);
  64. if (token) {
  65. let user = jwt.decode(token);
  66. commit('setUser', user, { root: true });
  67. }
  68. }
  69. },
  70. async toGetMenu({ commit }, payload) {
  71. const res = await this.$axios.$get(api.getMenu, payload);
  72. return res;
  73. },
  74. async logout({ commit }, payload) {
  75. let key = localStorage.getItem('token');
  76. const res = await this.$axios.$post(api.logout, { key: key });
  77. commit('deleteUser');
  78. },
  79. async update({ commit }, payload) {
  80. let res = await this.$axios.$post(`${api.updatePassword}`, {
  81. data: payload,
  82. });
  83. return res;
  84. },
  85. async bind({ commit }, payload) {
  86. let res = await this.$axios.$post(`${api.bind}`, payload);
  87. return res;
  88. },
  89. async userBind({ commit }, payload) {
  90. let res = await this.$axios.$post(`${api.userBind}`, payload);
  91. return res;
  92. },
  93. async getQrcode({ commit }, payload) {
  94. let res = await this.$axios.$get(`${api.connection}`);
  95. if (res.errcode === 0) return res.data;
  96. else {
  97. console.warn('请求qrcode失败');
  98. }
  99. },
  100. async wxCheck({ commit }, payload) {
  101. let res = await this.$axios.$post(`${api.wxCheck}`, payload);
  102. return res;
  103. },
  104. };
  105. export default {
  106. namespaced: true,
  107. state,
  108. mutations,
  109. actions,
  110. };