index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. let lifeData = {};
  5. try {
  6. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  7. lifeData = uni.getStorageSync('lifeData');
  8. } catch (e) {}
  9. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  10. let saveStateKeys = ['firendItem', 'userData', 'userInfo', 'firstAuthentication', 'enter', 'pushRes', 'myTip',
  11. 'taskTip', 'contractTip', 'liangxinTip', 'packet', 'linkItem', 'memberItem', 'memberItemIndex', 'chatItem',
  12. 'clientId', 'infoList'
  13. ];
  14. // 保存变量到本地存储中
  15. const saveLifeData = function(key, value) {
  16. // 判断变量名是否在需要存储的数组中
  17. if (saveStateKeys.indexOf(key) != -1) {
  18. // 获取本地存储的lifeData对象,将变量添加到对象中
  19. let tmp = uni.getStorageSync('lifeData');
  20. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  21. tmp = tmp ? tmp : {};
  22. // if(key == 'contractTip' && tmp[key] < value){
  23. // uni.vibrateLong({
  24. // success: function () {
  25. // console.log('震动成功');
  26. // }
  27. // });
  28. // }
  29. // if(key == 'taskTip' && tmp[key] < value){
  30. // uni.vibrateLong({
  31. // success: function () {
  32. // console.log('震动成功');
  33. // }
  34. // });
  35. // }
  36. tmp[key] = value;
  37. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  38. uni.setStorageSync('lifeData', tmp);
  39. }
  40. }
  41. const store = new Vuex.Store({
  42. modules: {},
  43. state: {
  44. sysinfo: uni.getSystemInfoSync(), //系统信息
  45. enter: {},
  46. hasLogin: false,
  47. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  48. pushRes: lifeData.pushRes ? lifeData.pushRes : {},
  49. //期货数据获取
  50. infoList: lifeData.infoList ? lifeData.infoList : [],
  51. //我的气泡提示
  52. myTip: lifeData.myTip ? lifeData.myTip : 0,
  53. //我的气泡提示
  54. taskTip: lifeData.taskTip ? lifeData.taskTip : 0,
  55. //我的气泡提示
  56. contractTip: lifeData.contractTip ? lifeData.contractTip : 0,
  57. //粮信气泡提示
  58. liangxinTip: lifeData.liangxinTip ? lifeData.liangxinTip : 0,
  59. // 链接
  60. linkItem: lifeData.linkItem ? lifeData.linkItem : [],
  61. userData: lifeData.userData ? lifeData.userData : {},
  62. userInfo: lifeData.userInfo ? lifeData.userInfo : {},
  63. //群成员 有索引A~Z
  64. memberItem: lifeData.memberItem ? lifeData.memberItem : [],
  65. //群成员 没有索引
  66. memberItemIndex: lifeData.memberItemIndex ? lifeData.memberItemIndex : [],
  67. // 消息列表
  68. chatItem: lifeData.chatItem ? lifeData.chatItem : [],
  69. //我的朋友
  70. firendItem: lifeData.firendItem ? lifeData.firendItem : {},
  71. // 红包
  72. packet: lifeData.packet ? lifeData.packet : {},
  73. // 消息体
  74. chatObj: {
  75. chatId: '',
  76. chatType: 0,
  77. chatName: '测试'
  78. },
  79. // 版本
  80. vuex_version: '1.0.1',
  81. //朋友圈展示信息
  82. circleData: [],
  83. //内置朋友圈相册banner图
  84. circleBgList: [],
  85. clientId: lifeData.clientId ? lifeData.clientId : "",
  86. // 确定收货
  87. isConfigfreightTransport: true,
  88. choice: "",
  89. firstAuthentication: {}
  90. },
  91. mutations: {
  92. $uStore(state, payload) {
  93. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  94. let nameArr = payload.name.split('.');
  95. let saveKey = '';
  96. let len = nameArr.length;
  97. if (nameArr.length >= 2) {
  98. let obj = state[nameArr[0]];
  99. for (let i = 1; i < len - 1; i++) {
  100. obj = obj[nameArr[i]];
  101. }
  102. obj[nameArr[len - 1]] = payload.value;
  103. saveKey = nameArr[0];
  104. } else {
  105. // 单层级变量,在state就是一个普通变量的情况
  106. state[payload.name] = payload.value;
  107. saveKey = payload.name;
  108. }
  109. // 保存变量到本地,见顶部函数定义
  110. saveLifeData(saveKey, state[saveKey])
  111. },
  112. login(state, provider) {
  113. state.hasLogin = true;
  114. state.userInfo = provider;
  115. // state.firstAuthentication = uni.getStorageSync('firstAuthentication');
  116. uni.setStorageSync('userInfo', provider)
  117. },
  118. logout(state) {
  119. state.hasLogin = false;
  120. state.userInfo = {};
  121. state.firstAuthentication = {};
  122. uni.removeStorageSync('userInfo')
  123. },
  124. configfreightTransport(state, flag) {
  125. state.isConfigfreightTransport = flag
  126. },
  127. configfreightChoice(state, val) {
  128. state.choice = val
  129. },
  130. enterchange(state, provider) {
  131. console.log(state)
  132. console.log(provider)
  133. state.enter = provider
  134. console.log(state)
  135. },
  136. },
  137. actions: {
  138. },
  139. getters: {
  140. getUserInfo(state) {
  141. return state.userInfo
  142. }
  143. }
  144. })
  145. export default store