app.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. deviceCode: Cache.get('deviceCode') || '',
  23. deviceName: Cache.get('deviceName') || '',
  24. template: Cache.get('template') || ''
  25. };
  26. const mutations = {
  27. LOGIN(state, opt) {
  28. state.token = opt.token;
  29. Cache.set(LOGIN_STATUS, opt.token);
  30. },
  31. SETUID(state,val){
  32. state.uid = val;
  33. Cache.set(UID, val);
  34. },
  35. OPENID(state, val) {
  36. state.openid = val;
  37. Cache.set(OPENID, val);
  38. },
  39. UPDATE_LOGIN(state, token) {
  40. state.token = token;
  41. },
  42. LOGOUT(state) {
  43. state.token = undefined;
  44. state.uid = undefined
  45. Cache.clear(LOGIN_STATUS);
  46. Cache.clear(UID);
  47. Cache.clear(USER_INFO);
  48. },
  49. BACKGROUND_COLOR(state, color) {
  50. state.color = color;
  51. document.body.style.backgroundColor = color;
  52. },
  53. UPDATE_USERINFO(state, userInfo) {
  54. state.userInfo = userInfo;
  55. Cache.set(USER_INFO, userInfo);
  56. },
  57. OPEN_HOME(state) {
  58. state.homeActive = true;
  59. },
  60. CLOSE_HOME(state) {
  61. state.homeActive = false;
  62. },
  63. SET_CHATURL(state, chatUrl){
  64. state.chatUrl = chatUrl;
  65. },
  66. SYSTEM_PLATFORM(state, systemPlatform){
  67. state.systemPlatform = systemPlatform;
  68. Cache.set(PLATFORM, systemPlatform);
  69. },
  70. //更新useInfo数据
  71. changInfo(state, payload) {
  72. state.userInfo[payload.amount1] = payload.amount2;
  73. Cache.set(USER_INFO, state.userInfo);
  74. },
  75. //商品类型,用于区分视频号商品与一般商品
  76. PRODUCT_TYPE(state, productType) {
  77. state.productType = productType;
  78. Cache.set('productType', productType);
  79. },
  80. DEVICE_CODE(state, deviceCode) {
  81. console.log(deviceCode,'我是vuex里的数据');
  82. state.deviceCode = deviceCode;
  83. Cache.set('deviceCode', deviceCode);
  84. },
  85. DEVICE_NAME(state, deviceName) {
  86. console.log(deviceName,'我是vuex里的数据');
  87. state.deviceName = deviceName;
  88. Cache.set('deviceName', deviceName);
  89. },
  90. TEMPLATE(state,template) {
  91. console.log(template,'我是vuex里的数据');
  92. state.template = template;
  93. Cache.set('template', template);
  94. }
  95. };
  96. const actions = {
  97. USERINFO({
  98. state,
  99. commit
  100. }, force) {
  101. return new Promise(reslove => {
  102. UserApi.getUserInfo().then(res => {
  103. commit("UPDATE_USERINFO", res.data);
  104. reslove(res.data);
  105. })
  106. }).catch(() => {
  107. });
  108. // debugger
  109. // if (state.userInfo !== null && !force)
  110. // return Promise.resolve(state.userInfo);
  111. // else
  112. // return new Promise(reslove => {
  113. // getUserInfo().then(res => {
  114. // commit("UPDATE_USERINFO", res.data);
  115. // reslove(res.data);
  116. // });
  117. // }).catch(() => {
  118. // });
  119. }
  120. };
  121. export default {
  122. state,
  123. mutations,
  124. actions
  125. };