user.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. query as queryUsers,
  3. queryCurrent,
  4. queryCurrentV2,
  5. queryUserRole,
  6. queryUnreadNotification,
  7. SetNotificationRead,
  8. } from '@/services/user';
  9. import { queryDepV2 } from '@/services/approval';
  10. import { ShowUnreadNotification } from '@/utils/utils';
  11. import { queryDeviceItemRealtimeData } from '@/services/DeviceAdmin';
  12. import { queryUserList } from '@/services/plant';
  13. import { queryProjectMenu, queryUserDetail, queryRole } from '@/services/SysAdmin';
  14. function getDepUserTree(data) {
  15. data.title = `${data.Name}`;
  16. data.id = data.ID;
  17. data.value = data.ID;
  18. // data.selectable = false;
  19. if (!data.children) data.children = new Array();
  20. if (data.children) {
  21. data.children.forEach(item => {
  22. getDepUserTree(item);
  23. });
  24. }
  25. if (data.Users && data.Users.length !== 0) {
  26. data.Users.forEach(item => {
  27. item.title = item.CName;
  28. item.id = item.ID + '||' + data.ID;
  29. item.value = item.ID + '||' + data.ID;
  30. // item.selectable = true;
  31. item.DepId = data.ID;
  32. data.children.push(item);
  33. });
  34. }
  35. return data;
  36. }
  37. const getRoleList = data => {
  38. let roleList = [];
  39. (data || []).forEach(dep => {
  40. (dep.Role || []).forEach(role => {
  41. roleList.push(role);
  42. });
  43. if (dep.children) {
  44. let res = getRoleList(dep.children, roleList);
  45. roleList = res.concat(res);
  46. }
  47. });
  48. return roleList;
  49. };
  50. export default {
  51. namespace: 'user',
  52. state: {
  53. list: [],
  54. currentUser: { Permission: {} },
  55. message: {},
  56. userList: [],
  57. depRole: [],
  58. depUserTree: [],
  59. roleList: [],
  60. },
  61. effects: {
  62. *fetch(_, { call, put }) {
  63. const response = yield call(queryUsers);
  64. if (response) {
  65. yield put({
  66. type: 'save',
  67. payload: response.data.list,
  68. });
  69. }
  70. },
  71. // *fetchCurrent(_, { call, put }) {
  72. // const response = yield call(queryCurrent);
  73. // if (response) {
  74. // yield put({
  75. // type: 'saveCurrentUser',
  76. // payload: response.data,
  77. // });
  78. // }
  79. // },
  80. *fetchCurrent({ payload }, { call, put }) {
  81. const response = yield call(queryCurrentV2);
  82. let permission = {};
  83. if (response) {
  84. let user = response.data;
  85. user.Permissions?.forEach(item => {
  86. permission = {
  87. ...permission,
  88. ...item.Menus,
  89. };
  90. });
  91. try {
  92. localStorage.setItem('depId', user.DepId);
  93. if (payload?.ID) {
  94. const { data: resData } = yield call(queryProjectMenu, payload);
  95. // permission = resData[0] ? resData[0]?.Menus : {};
  96. }
  97. } catch (error) {
  98. console.error(error);
  99. }
  100. const resRole = yield call(queryUserRole, user.ID);
  101. let roleList = getRoleList(resRole.data.Dep);
  102. // console.log(roleList);
  103. yield put({
  104. type: 'saveCurrentUser',
  105. payload: {
  106. ...user,
  107. // Permission: {},
  108. Permission: permission,
  109. roleList: roleList,
  110. },
  111. });
  112. }
  113. },
  114. *queryDepRole({ payload }, { call, put }) {
  115. const { data } = yield call(queryUserDetail, payload);
  116. let depId = payload.DepId;
  117. let dep = data.Dep.find(item => item.ID == depId);
  118. yield put({
  119. type: 'saveDepRole',
  120. payload: dep.Role,
  121. });
  122. },
  123. *fetchUnreadNotification(_, { call, put }) {
  124. const response = yield call(queryUnreadNotification);
  125. if (response) {
  126. const { data } = response;
  127. yield put({
  128. type: 'savefetchUnreadNotification',
  129. payload: { message: data },
  130. });
  131. }
  132. // ShowUnreadNotification(list, put);
  133. },
  134. *readNotification({ payload }, { call }) {
  135. console.log(payload);
  136. yield call(SetNotificationRead, payload);
  137. },
  138. *fetchUserList({ payload }, { call, put }) {
  139. const response = yield call(queryUserList, payload);
  140. if (response) {
  141. yield put({
  142. type: 'userListHandler',
  143. payload: response.data,
  144. });
  145. }
  146. },
  147. *getRoleList({ payload }, { call, put }) {
  148. const response = yield call(queryRole, payload);
  149. if (response) {
  150. yield put({
  151. type: 'saveState',
  152. payload: { roleList: response.data.list },
  153. });
  154. }
  155. },
  156. *fetchDepV2({ payload, callback }, { call, put }) {
  157. const response = yield call(queryDepV2, { pageSize: 999999 });
  158. if (response) {
  159. const depUserTree = response.data.list.map(item => {
  160. return getDepUserTree(item);
  161. });
  162. yield put({
  163. type: 'saveState',
  164. payload: { depUserTree },
  165. });
  166. }
  167. },
  168. },
  169. reducers: {
  170. save(state, action) {
  171. return {
  172. ...state,
  173. list: action.payload,
  174. };
  175. },
  176. saveDepRole(state, action) {
  177. return {
  178. ...state,
  179. depRole: action.payload,
  180. };
  181. },
  182. saveCurrentUser(state, action) {
  183. return {
  184. ...state,
  185. currentUser: action.payload || {
  186. Permission: {},
  187. },
  188. };
  189. },
  190. changeNotifyCount(state, action) {
  191. return {
  192. ...state,
  193. currentUser: {
  194. ...state.currentUser,
  195. notifyCount: action.payload.totalCount,
  196. unreadCount: action.payload.unreadCount,
  197. },
  198. };
  199. },
  200. savefetchUnreadNotification(state, action) {
  201. return {
  202. ...state,
  203. ...action.payload,
  204. };
  205. },
  206. userListHandler(state, { payload }) {
  207. return {
  208. ...state,
  209. userList: payload,
  210. };
  211. },
  212. saveState(state, action) {
  213. return {
  214. ...state,
  215. ...action.payload,
  216. };
  217. },
  218. },
  219. subscriptions: {
  220. setup({ dispatch, history }) {
  221. history.listen(({ pathname }) => {
  222. if (pathname === '/home') {
  223. // setInterval(function(){
  224. // dispatch({
  225. // type: 'fetchUnreadNotification',
  226. // });
  227. // }(),1000*20);
  228. }
  229. });
  230. },
  231. },
  232. };