login.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = async (token, commit) => {
  33. localStorage.setItem('token', token);
  34. let userInfo = await dispatch('toGetUser');
  35. return userInfo;
  36. };
  37. let userInfo = {};
  38. if (res.errcode == '0') {
  39. userInfo = await setUser(res.data.key, commit);
  40. Notification({
  41. title: '登录成功',
  42. // message: `欢迎,${user.user_name}`,
  43. type: 'success',
  44. duration: 2000,
  45. });
  46. return userInfo;
  47. } else {
  48. if (needReturn) return res;
  49. else {
  50. Notification({
  51. title: '登录失败',
  52. message: `失败原因:${res.errmsg || '登陆失败'}`,
  53. type: 'error',
  54. });
  55. }
  56. }
  57. },
  58. async toGetUser({ commit }, payload) {
  59. let key = localStorage.getItem('token');
  60. if (!key) {
  61. if (_.isFunction(payload)) {
  62. payload();
  63. return;
  64. }
  65. console.log('游客身份');
  66. let user = localStorage.getItem('user');
  67. if (user) {
  68. commit('setUser', JSON.parse(user), { root: true });
  69. } else {
  70. let stamp = new Date().getTime();
  71. let name = `游客${stamp}`;
  72. localStorage.setItem('user', JSON.stringify({ name }));
  73. commit('setUser', { name }, { root: true });
  74. }
  75. return;
  76. }
  77. console.log('用户');
  78. let res = await axios.post(api.getUser, { key: key });
  79. let user = {};
  80. if (res.data.errcode == '0') {
  81. let token = _.get(res, `data.data.token`);
  82. if (token) {
  83. user = jwt.decode(token);
  84. commit('setUser', user, { root: true });
  85. }
  86. }
  87. return user;
  88. },
  89. async toGetMenu({ commit }, payload) {
  90. const res = await this.$axios.$get(api.getMenu, payload);
  91. return res;
  92. },
  93. async logout({ commit }, payload) {
  94. let key = localStorage.removeItem('token');
  95. const res = await this.$axios.$post(api.logout, { key: key });
  96. commit('deleteUser');
  97. },
  98. async update({ commit }, payload) {
  99. let res = await this.$axios.$post(`${api.updatePassword}`, {
  100. data: payload,
  101. });
  102. return res;
  103. },
  104. async bind({ commit }, payload) {
  105. let res = await this.$axios.$post(`${api.bind}`, payload);
  106. return res;
  107. },
  108. async userBind({ commit }, payload) {
  109. let res = await this.$axios.$post(`${api.userBind}`, payload);
  110. return res;
  111. },
  112. async getQrcode({ commit }, payload) {
  113. let res = await this.$axios.$get(`${api.connection}`);
  114. if (res.errcode === 0) return res.data;
  115. else {
  116. console.warn('请求qrcode失败');
  117. }
  118. },
  119. async wxCheck({ commit }, payload) {
  120. let res = await this.$axios.$post(`${api.wxCheck}`, payload);
  121. return res;
  122. },
  123. };
  124. export default {
  125. namespaced: true,
  126. state,
  127. mutations,
  128. actions,
  129. };