index.js 4.4 KB

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