app.js 1.8 KB

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