index.js 4.3 KB

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