app.js 2.4 KB

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