login.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. userList: `/api/train/user`,
  9. interface: `/api/train/login`,
  10. user: id => `/api/train/user/update/${id}`,
  11. bind: `/api/train/user/bind`,
  12. userBind: `/api/train/user/userbind`,
  13. connection: `/api/train/qrcode`,
  14. wxCheck: `/api/train/wxcheck`,
  15. wxLogin: `/api/train/wxlogin`,
  16. openidLogin: `/api/train/openidLogin`,
  17. logout: '/api/train/y/logout',
  18. };
  19. const state = () => ({});
  20. const mutations = {};
  21. const actions = {
  22. // 云就业退出
  23. async logout({ commit }, payload) {
  24. const res = await this.$axios.$get(`${api.logout}?id=${payload}`);
  25. return res;
  26. },
  27. /**
  28. user:Object required 登陆信息
  29. router:router 如果跳转就传
  30. path:String 跳转到的路由位置
  31. needReturn: Boolean 是否返回结果
  32. typeCheck: Boolean 是否检查身份对应匹配的前端项目
  33. isWx: Boolean 是否是微信登陆
  34. needNotice:Boolean 是否需要提示
  35. */
  36. async login({ commit }, { user, router, path = '/', needReturn = false, typeCheck = true, isWx = false, needNotice = true }) {
  37. let res;
  38. //wx登陆,openid存在,user中是openid和qrcode;正常登陆,user中是mobile和passwd
  39. if (isWx) res = await this.$axios.$post(`${api.wxLogin}`, user);
  40. else res = await this.$axios.$post(`${api.interface}`, user);
  41. const setUser = (user, commit) => {
  42. localStorage.setItem('user', JSON.stringify(user));
  43. commit('setUser', user, { root: true });
  44. };
  45. if (res.errcode === 0) {
  46. let user = jwt.decode(res.data);
  47. if (!typeCheck) {
  48. setUser(user, commit);
  49. if (needReturn) return res;
  50. else router.push(path);
  51. }
  52. if (user.type == process.env.VUE_APP_USER_TYPE) {
  53. setUser(user, commit);
  54. if (needNotice)
  55. Notification({
  56. title: '登录成功',
  57. message: `欢迎,${user.name}`,
  58. type: 'success',
  59. duration: 2000,
  60. });
  61. if (needReturn) return res;
  62. else router.push(path);
  63. } else {
  64. if (needNotice)
  65. Notification({
  66. title: '请重新登陆',
  67. message: `原因:非当前端用户,需要重新登陆`,
  68. type: 'warning',
  69. });
  70. console.warn('非当前端用户,需要重新登陆');
  71. }
  72. } else {
  73. if (needReturn) return res;
  74. else {
  75. Notification({
  76. title: '登录失败',
  77. message: `失败原因:${res.errmsg}`,
  78. type: 'error',
  79. });
  80. }
  81. }
  82. },
  83. async update({ commit }, { id, ...info }) {
  84. let res = await this.$axios.$post(`${api.user(id)}`, info);
  85. return res;
  86. },
  87. async bind({ commit }, payload) {
  88. let res = await this.$axios.$post(`${api.bind}`, payload);
  89. return res;
  90. },
  91. async userBind({ commit }, payload) {
  92. let res = await this.$axios.$post(`${api.userBind}`, payload);
  93. return res;
  94. },
  95. async getQrcode({ commit }, payload) {
  96. let res = await this.$axios.$get(`${api.connection}`);
  97. if (res.errcode === 0) return res.data;
  98. else {
  99. console.warn('请求qrcode失败');
  100. }
  101. },
  102. async wxCheck({ commit }, payload) {
  103. let res = await this.$axios.$post(`${api.wxCheck}`, payload);
  104. return res;
  105. },
  106. async userList({ commit }, payload) {
  107. const res = await this.$axios.$get(api.userList, payload);
  108. return res;
  109. },
  110. async openidLogin({ commit }, payload) {
  111. let res = await this.$axios.$post(`${api.openidLogin}`, payload);
  112. return res;
  113. },
  114. };
  115. export default {
  116. namespaced: true,
  117. state,
  118. mutations,
  119. actions,
  120. };