app.js 2.8 KB

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