app.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. getUserInfo
  3. } from "../../api/user.js";
  4. import {
  5. LOGIN_STATUS,
  6. UID,
  7. OPENID
  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. open_id: Cache.get(OPENID),
  19. homeActive: false,
  20. phoneStatus:true,
  21. pageFooter:uni.getStorageSync('pageFoot') || {}
  22. };
  23. const mutations = {
  24. SETPHONESTATUS(state,val){
  25. state.phoneStatus = val;
  26. },
  27. LOGIN(state, opt) {
  28. state.token = opt.token;
  29. Cache.set(LOGIN_STATUS, opt.token, opt.time);
  30. },
  31. OPENID(state, val) {
  32. state.open_id = val;
  33. Cache.set(OPENID, val);
  34. },
  35. SETUID(state,val){
  36. state.uid = val;
  37. Cache.set(UID, val);
  38. },
  39. UPDATE_LOGIN(state, token) {
  40. state.token = token;
  41. },
  42. LOGOUT(state) {
  43. state.token = false;
  44. state.uid = 0
  45. Cache.clear(LOGIN_STATUS);
  46. Cache.clear(UID);
  47. },
  48. BACKGROUND_COLOR(state, color) {
  49. state.color = color;
  50. document.body.style.backgroundColor = color;
  51. },
  52. UPDATE_USERINFO(state, userInfo) {
  53. state.userInfo = userInfo;
  54. Cache.set(USER_INFO, userInfo);
  55. },
  56. OPEN_HOME(state) {
  57. state.homeActive = true;
  58. },
  59. CLOSE_HOME(state) {
  60. state.homeActive = false;
  61. },
  62. FOOT_UPLOAD(state,data){
  63. state.pageFooter = data
  64. }
  65. };
  66. const actions = {
  67. USERINFO({
  68. state,
  69. commit
  70. }, force) {
  71. if (state.userInfo !== null && !force)
  72. return Promise.resolve(state.userInfo);
  73. else
  74. return new Promise(reslove => {
  75. getUserInfo().then(res => {
  76. commit("UPDATE_USERINFO", res.data);
  77. Cache.set(USER_INFO, res.data);
  78. reslove(res.data);
  79. });
  80. }).catch(() => {
  81. });
  82. }
  83. };
  84. export default {
  85. state,
  86. mutations,
  87. actions
  88. };