index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import storage from '@/utils/storage.js'
  4. Vue.use(Vuex)
  5. let lifeData = {}
  6. // 尝试获取本地是否存在lifeData变量,第一次启动时不存在
  7. try {
  8. lifeData = uni.getStorageSync('lifeData')
  9. } catch (e) {
  10. }
  11. // 标记需要永久存储的变量,在每次启动时取出,在state中的变量名
  12. let saveStateKeys = ['vuex_user']
  13. // 保存变量到本地存储
  14. const saveLifeData = function(key, value) {
  15. // 判断变量是否在存储数组中
  16. if (saveStateKeys.indexOf(key) != -1) {
  17. // 获取本地存储的lifeData对象,将变量添加到对象中
  18. let tmpLifeData = uni.getStorageSync('lifeData')
  19. // 第一次启动时不存在,则放一个空对象
  20. tmpLifeData = tmpLifeData ? tmpLifeData : {},
  21. tmpLifeData[key] = value
  22. // 将变量再次放回本地存储中
  23. uni.setStorageSync('lifeData', tmpLifeData)
  24. }
  25. }
  26. const store = new Vuex.Store({
  27. state: {
  28. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  29. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  30. vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {
  31. name: '速立保'
  32. },
  33. // 如果vuex_version无需保存到本地永久存储,无需lifeData.vuex_version方式
  34. // app版本
  35. vuex_version: "1.0.0",
  36. // 是否使用自定义导航栏
  37. vuex_custom_nav_bar: true,
  38. // 状态栏高度
  39. vuex_status_bar_height: 0,
  40. // 自定义导航栏的高度
  41. vuex_custom_bar_height: 0,
  42. token: null,
  43. openId: null,
  44. userInfo: null,
  45. expires_time: null,
  46. isGOAuth: false, //是否已跳转至登录界面,防止路由重复注入
  47. isFirstLoad: true, //是否第一次加载 第一次加载未认证不需要弹框 直接跳转认证
  48. authPopupShow: false, //x-authorize 弹窗是否显示
  49. engAdmin: false
  50. },
  51. mutations: {
  52. LOGOUT(state) {
  53. state.token = null;
  54. //state.expires_time = null;
  55. state.userInfo = null;
  56. storage.remove('openId');
  57. storage.remove('userInfo');
  58. storage.remove('token');
  59. storage.remove('memberNo');
  60. storage.remove('myUsername');
  61. storage.remove('accountName');
  62. //storage.remove('expires_time')
  63. },
  64. LOGIN(state, {
  65. token,
  66. openId,
  67. expires_time
  68. }) {
  69. state.token = token;
  70. state.openId = openId;
  71. state.expires_time = expires_time;
  72. storage.set('token', token)
  73. storage.set('openId', openId)
  74. storage.set('expires_time', expires_time)
  75. },
  76. UPDATE_USERINFO(state, userInfo) {
  77. state.userInfo = userInfo;
  78. storage.set('userInfo', userInfo);
  79. if (userInfo) {
  80. storage.set('memberNo', userInfo.member_no);
  81. storage.set('accountName', userInfo.account_name);
  82. uni.setStorageSync("myUsername", userInfo.account_name);
  83. // uni.setStorageSync("myUsername",userInfo.account_name+"m"+userInfo.member_no);
  84. state.engAdmin = false;
  85. if (userInfo.engAdmin) {
  86. state.engAdmin = userInfo.engAdmin;
  87. }
  88. }
  89. },
  90. SET_GO_AUTH(state, self) {
  91. state.isGOAuth = self;
  92. },
  93. SHOW_AUTH_POPUP_SHOW(state) {
  94. state.authPopupShow = true;
  95. },
  96. HIDE_AUTH_POPUP_SHOW(state) {
  97. state.authPopupShow = false;
  98. },
  99. $tStore(state, payload) {
  100. // 判断是否多层调用,state中为对象存在的情况,例如user.info.score = 1
  101. let nameArr = payload.name.split('.')
  102. let saveKey = ''
  103. let len = nameArr.length
  104. if (len >= 2) {
  105. let obj = state[nameArr[0]]
  106. for (let i = 1; i < len - 1; i++) {
  107. obj = obj[nameArr[i]]
  108. }
  109. obj[nameArr[len - 1]] = payload.value
  110. saveKey = nameArr[0]
  111. } else {
  112. // 单层级变量
  113. state[payload.name] = payload.value
  114. saveKey = payload.name
  115. }
  116. // 保存变量到本地中
  117. saveLifeData(saveKey, state[saveKey])
  118. }
  119. },
  120. actions: {},
  121. getters: {
  122. token: state => state.token,
  123. userInfo: state => state.userInfo || {},
  124. expires_time: state => state.expires_time,
  125. authPopupShow: state => state.authPopupShow
  126. }
  127. })
  128. export default store