login.js 2.4 KB

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