app.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. getUserInfo
  3. } from "../../api/user.js";
  4. import {
  5. LOGIN_STATUS,
  6. UID
  7. } from '../../config/cache';
  8. import Cache from '../../utils/cache';
  9. import {
  10. USER_INFO
  11. } from '../../config/cache';
  12. const state = {
  13. token: Cache.get(LOGIN_STATUS) || null,
  14. backgroundColor: "#fff",
  15. userInfo: null,
  16. uid: Cache.get(UID) || null,
  17. homeActive: false,
  18. };
  19. const mutations = {
  20. LOGIN(state, opt) {
  21. state.token = opt.token;
  22. Cache.set(LOGIN_STATUS, opt.token, opt.time);
  23. },
  24. SETUID(state,val){
  25. state.uid = val;
  26. Cache.set(UID, val);
  27. },
  28. UPDATE_LOGIN(state, token) {
  29. state.token = token;
  30. },
  31. LOGOUT(state) {
  32. state.token = undefined;
  33. state.uid = undefined
  34. Cache.clear(LOGIN_STATUS);
  35. Cache.clear(UID);
  36. },
  37. BACKGROUND_COLOR(state, color) {
  38. state.color = color;
  39. document.body.style.backgroundColor = color;
  40. },
  41. UPDATE_USERINFO(state, userInfo) {
  42. state.userInfo = userInfo;
  43. },
  44. OPEN_HOME(state) {
  45. state.homeActive = true;
  46. },
  47. CLOSE_HOME(state) {
  48. state.homeActive = false;
  49. },
  50. };
  51. const actions = {
  52. USERINFO({
  53. state,
  54. commit
  55. }, force) {
  56. if (state.userInfo !== null && !force)
  57. return Promise.resolve(state.userInfo);
  58. else
  59. return new Promise(reslove => {
  60. getUserInfo().then(res => {
  61. commit("UPDATE_USERINFO", res.data);
  62. Cache.set(USER_INFO, res.data);
  63. reslove(res.data);
  64. });
  65. }).catch(() => {
  66. });
  67. }
  68. };
  69. export default {
  70. state,
  71. mutations,
  72. actions
  73. };