index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import { Notification } from 'element-ui'
  4. import public_data from './data' // 公共数据
  5. import config_data from './config-data' // 配置数据
  6. Vue.use(Vuex)
  7. const store = new Vuex.Store({
  8. state: {
  9. user_name: '' || localStorage.getItem('user_name'),
  10. admin: localStorage.getItem('admin') || false,
  11. token: '' || localStorage.getItem('token'),
  12. name: '' || localStorage.getItem('name'),
  13. backend_type: localStorage.getItem('backend_type') || '1', // 1:站机端、2:中心端
  14. defaultActive: localStorage.getItem('defaultActive') || '/layout/real_time_monitoring',
  15. del_user: ['user_name', 'admin', 'token', 'name', 'backend_type', 'defaultActive'],
  16. notification_list: [], // 存放消息提示,长度超过 3 则删除第一个
  17. },
  18. mutations: {
  19. // 将用户信息存在 localStorage 中
  20. handle_save_user(state, user) {
  21. Object.keys(user).forEach(key => {
  22. state[key] = user[key]
  23. localStorage.setItem(`${key}`, user[key])
  24. })
  25. },
  26. // 保存获取目前所在位置
  27. handle_save_defaultActive(state, defaultActive) {
  28. state.defaultActive = defaultActive
  29. localStorage.setItem('defaultActive', defaultActive)
  30. },
  31. // 保存后台类型
  32. handle_save_backend_type(state, backend_type) {
  33. state.backend_type = backend_type
  34. localStorage.setItem('backend_type', backend_type)
  35. },
  36. // 新预警提示
  37. handle_new_alarm(state, message) {
  38. // 当弹出的预警超过三条时,先删除数组第一个元素,然后消息提示,最后把最新一条报警添加进去
  39. if (state.notification_list.length == 3) {
  40. state.notification_list[0].close()
  41. state.notification_list.shift()
  42. }
  43. let new_alarm = Notification({
  44. type: 'warning',
  45. showClose: true,
  46. title: '告警消息',
  47. dangerouslyUseHTMLString: true,
  48. message: message,
  49. position: 'bottom-right',
  50. duration: 30000,
  51. })
  52. state.notification_list.push(new_alarm)
  53. },
  54. },
  55. // 引入公共数据
  56. modules: {
  57. public_data,
  58. config_data,
  59. },
  60. })
  61. export default store