app.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const user_res = require('@/mock/json/user_res.json')
  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. pageFooter:uni.getStorageSync('pageFoot') || {}
  21. };
  22. const mutations = {
  23. SETPHONESTATUS(state,val){
  24. state.phoneStatus = val;
  25. },
  26. LOGIN(state, opt) {
  27. state.token = opt.token;
  28. Cache.set(LOGIN_STATUS, opt.token, opt.time);
  29. },
  30. SETUID(state,val){
  31. state.uid = val;
  32. Cache.set(UID, val);
  33. },
  34. UPDATE_LOGIN(state, token) {
  35. state.token = token;
  36. },
  37. LOGOUT(state) {
  38. state.token = false;
  39. state.uid = 0
  40. Cache.clear(LOGIN_STATUS);
  41. Cache.clear(UID);
  42. },
  43. BACKGROUND_COLOR(state, color) {
  44. state.color = color;
  45. document.body.style.backgroundColor = color;
  46. },
  47. UPDATE_USERINFO(state, userInfo) {
  48. state.userInfo = userInfo;
  49. Cache.set(USER_INFO, userInfo);
  50. },
  51. OPEN_HOME(state) {
  52. state.homeActive = true;
  53. },
  54. CLOSE_HOME(state) {
  55. state.homeActive = false;
  56. },
  57. FOOT_UPLOAD(state,data){
  58. state.pageFooter = data
  59. }
  60. };
  61. const actions = {
  62. USERINFO({
  63. state,
  64. commit
  65. }, force) {
  66. if (state.userInfo !== null && !force)
  67. return Promise.resolve(state.userInfo);
  68. else
  69. return new Promise(reslove => {
  70. let res = user_res;
  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. };