global.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { queryNotices } from '@/services/api';
  2. import { sldCommonService } from '@/utils/utils';
  3. export default {
  4. namespace: 'global',
  5. state: {
  6. collapsed: false,
  7. notices: [],
  8. siteList: [],
  9. currentSite: '',
  10. currentSiteName: ''
  11. },
  12. effects: {
  13. //修改密码
  14. *change_manager_pwd({ payload, callback }, { call }) {
  15. const response = yield call(sldCommonService, payload,'post','v3/system/admin/adminUser/updatePwd');
  16. if (callback) callback(response);
  17. },
  18. *fetchNotices(_, { call, put, select }) {
  19. const data = yield call(queryNotices);
  20. yield put({
  21. type: 'saveNotices',
  22. payload: data,
  23. });
  24. const unreadCount = yield select(
  25. state => state.global.notices.filter(item => !item.read).length
  26. );
  27. yield put({
  28. type: 'user/changeNotifyCount',
  29. payload: {
  30. totalCount: data.length,
  31. unreadCount,
  32. },
  33. });
  34. },
  35. *clearNotices({ payload }, { put, select }) {
  36. yield put({
  37. type: 'saveClearedNotices',
  38. payload,
  39. });
  40. const count = yield select(state => state.global.notices.length);
  41. const unreadCount = yield select(
  42. state => state.global.notices.filter(item => !item.read).length
  43. );
  44. yield put({
  45. type: 'user/changeNotifyCount',
  46. payload: {
  47. totalCount: count,
  48. unreadCount,
  49. },
  50. });
  51. },
  52. *changeNoticeReadState({ payload }, { put, select }) {
  53. const notices = yield select(state =>
  54. state.global.notices.map(item => {
  55. const notice = { ...item };
  56. if (notice.id === payload) {
  57. notice.read = true;
  58. }
  59. return notice;
  60. })
  61. );
  62. yield put({
  63. type: 'saveNotices',
  64. payload: notices,
  65. });
  66. yield put({
  67. type: 'user/changeNotifyCount',
  68. payload: {
  69. totalCount: notices.length,
  70. unreadCount: notices.filter(item => !item.read).length,
  71. },
  72. });
  73. },
  74. // 获取站点列表
  75. * get_site_list_data({ payload, callback }, { call, put, select }) {
  76. const siteList = yield select(state => state.global.siteList);
  77. const response = siteList.length ? {data: siteList, state: 200} : yield call(sldCommonService, payload, 'get', 'v3/system/seller/setting/getSiteSettingList');
  78. if (callback) callback(response);
  79. yield put({ type: 'saveSiteList', payload: response.data });
  80. const currentSite = yield select(state => state.global.currentSite);
  81. if (!currentSite) {
  82. yield put({type: 'setCurrentSite', payload: response.data[0]?.value})
  83. yield put({type: 'setCurrentSiteName', payload: response.data[0]?.title})
  84. }
  85. },
  86. },
  87. reducers: {
  88. saveSiteList(state, action) {
  89. return {
  90. ...state,
  91. siteList: action.payload,
  92. };
  93. },
  94. setCurrentSite(state, { payload }) {
  95. localStorage.setItem('currentSite', payload)
  96. return {
  97. ...state,
  98. currentSite: payload,
  99. };
  100. },
  101. setCurrentSiteName(state, { payload }) {
  102. return {
  103. ...state,
  104. currentSiteName: payload,
  105. };
  106. },
  107. changeLayoutCollapsed(state, { payload }) {
  108. return {
  109. ...state,
  110. collapsed: payload,
  111. };
  112. },
  113. getLayoutCollapsed(state, { payload }) {
  114. return {
  115. ...state,
  116. };
  117. },
  118. saveNotices(state, { payload }) {
  119. return {
  120. ...state,
  121. notices: payload,
  122. };
  123. },
  124. saveClearedNotices(state, { payload }) {
  125. return {
  126. ...state,
  127. notices: state.notices.filter(item => item.type !== payload),
  128. };
  129. },
  130. },
  131. subscriptions: {
  132. setup({ history }) {
  133. // Subscribe history(url) change, trigger `load` action if pathname is `/`
  134. return history.listen(({ pathname, search }) => {
  135. if (typeof window.ga !== 'undefined') {
  136. window.ga('send', 'pageview', pathname + search);
  137. }
  138. });
  139. },
  140. },
  141. };