app.js 1.6KB

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