app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {
  2. getUserInfo
  3. } from "../../api/user.js";
  4. import {
  5. LOGIN_STATUS,
  6. UID,
  7. PLATFORM
  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) || '',
  15. backgroundColor: "#fff",
  16. userInfo: Cache.get(USER_INFO)?JSON.parse(Cache.get(USER_INFO)):null,
  17. uid: Cache.get(UID) || null,
  18. homeActive: false,
  19. chatUrl: Cache.get('chatUrl') || '',
  20. systemPlatform: Cache.get(PLATFORM)?Cache.get(PLATFORM):'',
  21. productType: Cache.get('productType') || ''
  22. };
  23. const mutations = {
  24. LOGIN(state, opt) {
  25. state.token = opt.token;
  26. Cache.set(LOGIN_STATUS, opt.token);
  27. },
  28. SETUID(state,val){
  29. state.uid = val;
  30. Cache.set(UID, val);
  31. },
  32. UPDATE_LOGIN(state, token) {
  33. state.token = token;
  34. },
  35. LOGOUT(state) {
  36. state.token = undefined;
  37. state.uid = undefined
  38. Cache.clear(LOGIN_STATUS);
  39. Cache.clear(UID);
  40. Cache.clear(USER_INFO);
  41. },
  42. BACKGROUND_COLOR(state, color) {
  43. state.color = color;
  44. document.body.style.backgroundColor = color;
  45. },
  46. UPDATE_USERINFO(state, userInfo) {
  47. state.userInfo = userInfo;
  48. Cache.set(USER_INFO, userInfo);
  49. },
  50. OPEN_HOME(state) {
  51. state.homeActive = true;
  52. },
  53. CLOSE_HOME(state) {
  54. state.homeActive = false;
  55. },
  56. SET_CHATURL(state, chatUrl){
  57. state.chatUrl = chatUrl;
  58. },
  59. // AuthorizeType(state, authorizeType){
  60. // state.authorizeType = authorizeType;
  61. // },
  62. SYSTEM_PLATFORM(state, systemPlatform){
  63. state.systemPlatform = systemPlatform;
  64. Cache.set(PLATFORM, systemPlatform);
  65. },
  66. //更新useInfo数据
  67. changInfo(state, payload) {
  68. state.userInfo[payload.amount1] = payload.amount2;
  69. Cache.set(USER_INFO, state.userInfo);
  70. },
  71. //商品类型,用于区分视频号商品与一般商品
  72. PRODUCT_TYPE(state, productType) {
  73. state.productType = productType;
  74. Cache.set('productType', productType);
  75. }
  76. };
  77. const actions = {
  78. USERINFO({
  79. state,
  80. commit
  81. }, force) {
  82. return new Promise(reslove => {
  83. getUserInfo().then(res => {
  84. commit("UPDATE_USERINFO", res.data);
  85. reslove(res.data);
  86. });
  87. }).catch(() => {
  88. });
  89. // debugger
  90. // if (state.userInfo !== null && !force)
  91. // return Promise.resolve(state.userInfo);
  92. // else
  93. // return new Promise(reslove => {
  94. // getUserInfo().then(res => {
  95. // commit("UPDATE_USERINFO", res.data);
  96. // reslove(res.data);
  97. // });
  98. // }).catch(() => {
  99. // });
  100. }
  101. };
  102. export default {
  103. state,
  104. mutations,
  105. actions
  106. };