index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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','pushRes','myTip','taskTip','contractTip','liangxinTip','packet','linkItem','memberItem','memberItemIndex','chatItem','clientId'];
  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. uni.showTabBarRedDot({
  27. index: 4
  28. });
  29. }
  30. if(key == 'taskTip' && tmp[key] < value){
  31. uni.vibrateLong({
  32. success: function () {
  33. console.log('震动成功');
  34. }
  35. });
  36. uni.showTabBarRedDot({
  37. index: 4
  38. });
  39. }
  40. tmp[key] = value;
  41. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  42. uni.setStorageSync('lifeData', tmp);
  43. }
  44. }
  45. const store = new Vuex.Store({
  46. state: {
  47. hasLogin: false,
  48. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  49. pushRes:lifeData.pushRes?lifeData.pushRes: {},
  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. //群成员 有索引A~Z
  62. memberItem:lifeData.memberItem?lifeData.memberItem:[],
  63. //群成员 没有索引
  64. memberItemIndex: lifeData.memberItemIndex?lifeData.memberItemIndex:[],
  65. // 消息列表
  66. chatItem:lifeData.chatItem?lifeData.chatItem:[],
  67. //我的朋友
  68. firendItem: lifeData.firendItem?lifeData.firendItem: {},
  69. // 红包
  70. packet: lifeData.packet?lifeData.packet:{},
  71. // 消息体
  72. chatObj:{
  73. chatId:'',
  74. chatType:0,
  75. chatName:'测试'
  76. },
  77. // 版本
  78. vuex_version: '1.0.1',
  79. //朋友圈展示信息
  80. circleData: [],
  81. //内置朋友圈相册banner图
  82. circleBgList:[
  83. ],
  84. clientId: lifeData.clientId?lifeData.clientId: "",
  85. },
  86. mutations: {
  87. $uStore(state, payload) {
  88. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  89. let nameArr = payload.name.split('.');
  90. let saveKey = '';
  91. let len = nameArr.length;
  92. if (nameArr.length >= 2) {
  93. let obj = state[nameArr[0]];
  94. for (let i = 1; i < len - 1; i++) {
  95. obj = obj[nameArr[i]];
  96. }
  97. obj[nameArr[len - 1]] = payload.value;
  98. saveKey = nameArr[0];
  99. } else {
  100. // 单层级变量,在state就是一个普通变量的情况
  101. state[payload.name] = payload.value;
  102. saveKey = payload.name;
  103. }
  104. // 保存变量到本地,见顶部函数定义
  105. saveLifeData(saveKey, state[saveKey])
  106. },
  107. login(state, provider) {
  108. state.hasLogin = true;
  109. state.userInfo = provider;
  110. uni.setStorageSync('userInfo', provider)
  111. },
  112. logout(state) {
  113. state.hasLogin = false;
  114. state.userInfo = {};
  115. uni.removeStorageSync('userInfo')
  116. }
  117. },
  118. actions: {
  119. },
  120. getters: {
  121. getUserInfo(state) {
  122. return state.userInfo
  123. }
  124. }
  125. })
  126. export default store