app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. power: '',
  19. };
  20. const mutations = {
  21. LOGIN(state, opt) {
  22. state.token = opt.token;
  23. Cache.set(LOGIN_STATUS, opt.token, opt.time);
  24. },
  25. SETUID(state,val){
  26. state.uid = val;
  27. Cache.set(UID, val);
  28. },
  29. UPDATE_LOGIN(state, token) {
  30. state.token = token;
  31. },
  32. LOGOUT(state) {
  33. state.token = undefined;
  34. state.uid = undefined
  35. Cache.clear(LOGIN_STATUS);
  36. Cache.clear(UID);
  37. },
  38. BACKGROUND_COLOR(state, color) {
  39. state.color = color;
  40. document.body.style.backgroundColor = color;
  41. },
  42. UPDATE_USERINFO(state, userInfo) {
  43. state.userInfo = userInfo;
  44. },
  45. OPEN_HOME(state) {
  46. state.homeActive = true;
  47. },
  48. CLOSE_HOME(state) {
  49. state.homeActive = false;
  50. },
  51. setPower(state,val) {
  52. state.power = val
  53. }
  54. };
  55. const actions = {
  56. USERINFO({
  57. state,
  58. commit
  59. }, force) {
  60. if (state.userInfo !== null && !force)
  61. return Promise.resolve(state.userInfo);
  62. else
  63. return new Promise(reslove => {
  64. getUserInfo().then(res => {
  65. commit("UPDATE_USERINFO", res.data);
  66. Cache.set(USER_INFO, res.data);
  67. reslove(res.data);
  68. });
  69. }).catch(() => {
  70. });
  71. }
  72. };
  73. export default {
  74. state,
  75. mutations,
  76. actions
  77. };