user.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import store from '@/utils/store';
  2. import dayjs from 'dayjs';
  3. import { ProjectName } from '@/config/config';
  4. export default {
  5. state: {
  6. token: store.get('token') || null,
  7. shopInfo: store.get('shopInfo') || {},
  8. info: store.get('userInfo') || {}
  9. },
  10. actions: {
  11. userLogin({ commit }, form) {
  12. let host = null;
  13. if (ProjectName == 'business') {
  14. host = () => {
  15. return this.$service.common.userLogin(form);
  16. };
  17. } else {
  18. host = () => {
  19. return this.$service.common.saasLogin(form);
  20. };
  21. }
  22. return host(form).then(res => {
  23. commit('SET_TOKEN', res);
  24. return res;
  25. });
  26. },
  27. userLogout({ dispatch }) {
  28. return new Promise(resolve => {
  29. // service.common.userLogout().done(() => {
  30. dispatch('userRemove').then(() => {
  31. resolve();
  32. });
  33. });
  34. // });
  35. },
  36. shopInfo({ commit }) {
  37. return this.$service.common.shopInfo().then(res => {
  38. commit('SET_SHOPINFO', res);
  39. return res;
  40. });
  41. },
  42. userInfo({ commit }) {
  43. let host = null;
  44. if (ProjectName == 'business') {
  45. host = () => {
  46. return this.$service.common.userInfo();
  47. };
  48. } else {
  49. host = () => {
  50. return this.$service.common.saasInfo();
  51. };
  52. }
  53. return host().then(res => {
  54. commit('SET_USERINFO', res);
  55. if (ProjectName == 'business') {
  56. localStorage.setItem('account', res.merchantId);
  57. }
  58. return res;
  59. });
  60. },
  61. userRemove({ commit }) {
  62. commit('CLEAR_USER');
  63. commit('CLEAR_SHOPINFO');
  64. commit('CLEAR_TOKEN');
  65. commit('RESET_PROCESS');
  66. commit('SET_MENU_GROUP', []);
  67. commit('SET_VIEW_ROUTES', []);
  68. commit('SET_MENU_LIST', 0);
  69. }
  70. },
  71. mutations: {
  72. SET_SHOPINFO(state, val) {
  73. state.shopInfo = val;
  74. store.set('shopInfo', val);
  75. },
  76. SET_USERINFO(state, val) {
  77. state.info = val;
  78. store.set('userInfo', val);
  79. },
  80. SET_TOKEN(state, { token, expire }) {
  81. state.token = token;
  82. store.set('token', token, expire && dayjs().add(expire, 'second'));
  83. },
  84. CLEAR_SHOPINFO(state) {
  85. state.shopInfo = {};
  86. store.remove('shopInfo');
  87. },
  88. CLEAR_TOKEN(state) {
  89. state.token = null;
  90. store.remove('token');
  91. },
  92. CLEAR_USER(state) {
  93. state.info = {};
  94. store.remove('userInfo');
  95. }
  96. }
  97. };