app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Cache.set(USER_INFO, userInfo);
  44. },
  45. OPEN_HOME(state) {
  46. state.homeActive = true;
  47. },
  48. CLOSE_HOME(state) {
  49. state.homeActive = false;
  50. },
  51. };
  52. const actions = {
  53. USERINFO({
  54. state,
  55. commit
  56. }, force) {
  57. if (state.userInfo !== null && !force)
  58. return Promise.resolve(state.userInfo);
  59. else
  60. return new Promise(reslove => {
  61. getUserInfo().then(res => {
  62. commit("UPDATE_USERINFO", res.data);
  63. Cache.set(USER_INFO, res.data);
  64. reslove(res.data);
  65. });
  66. }).catch(() => {
  67. });
  68. }
  69. };
  70. export default {
  71. state,
  72. mutations,
  73. actions
  74. };