login.js 2.8 KB

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