model.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import consts from '@/consts'
  2. import { LabelStatusColorMap } from '@/pages/Customer/Company/components/PersonLabel/const'
  3. import { queryClientSourceList, queryTagList } from '@/services/customer'
  4. export default {
  5. namespace: 'client',
  6. state: {
  7. personTags: [], // 个人标签
  8. personTagColorMap: {}, // 个人标签的颜色映射关系
  9. teamTags: [], // 协作标签
  10. teamTagColorMap: {}, // 协作标签的颜色映射关系
  11. sourceList: [] // 来源列表
  12. },
  13. // 用于处理异步操作和业务逻辑,由action触发,但不能修改state
  14. effects: {
  15. *fetchPersonTags({ payload }, { call, put }) {
  16. const response = yield call(queryTagList, payload)
  17. if (response?.code === consts.RET_CODE.SUCCESS) {
  18. const { tagType } = payload
  19. yield put({
  20. type: 'changeState',
  21. payload: {
  22. key: 'personTags',
  23. value: response.data || []
  24. }
  25. })
  26. yield put({
  27. type: 'changeState',
  28. payload: {
  29. key: 'personTagColorMap',
  30. value:
  31. response.data?.reduce((curr, prev, idx) => {
  32. const item = { ...curr, [prev.id]: LabelStatusColorMap[tagType][idx] }
  33. return item
  34. }, {}) || {}
  35. }
  36. })
  37. }
  38. },
  39. *fetchTeamTags({ payload }, { call, put }) {
  40. const response = yield call(queryTagList, payload)
  41. if (response?.code === consts.RET_CODE.SUCCESS) {
  42. const { tagType } = payload
  43. yield put({
  44. type: 'changeState',
  45. payload: {
  46. key: 'teamTags',
  47. value: response.data || []
  48. }
  49. })
  50. yield put({
  51. type: 'changeState',
  52. payload: {
  53. key: 'teamTagColorMap',
  54. value:
  55. response.data?.reduce((curr, prev, idx) => {
  56. const item = { ...curr, [prev.id]: LabelStatusColorMap[tagType][idx] }
  57. return item
  58. }, {}) || {}
  59. }
  60. })
  61. }
  62. },
  63. *fetchSourceList(_, { call, put }) {
  64. const response = yield call(queryClientSourceList)
  65. if (response?.code === consts.RET_CODE.SUCCESS) {
  66. yield put({
  67. type: 'changeState',
  68. payload: {
  69. key: 'sourceList',
  70. value: response.data.map(item => ({ value: item.id, label: item.name }))
  71. }
  72. })
  73. }
  74. }
  75. },
  76. // reducers:用于处理同步操作,由action触发,可修改state
  77. reducers: {
  78. // 通用reducer
  79. changeState(state, action) {
  80. return {
  81. ...state,
  82. [action.payload.key]: action.payload.value
  83. }
  84. }
  85. }
  86. }