global.js 4.4 KB

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