sagas.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import omit from 'lodash/omit'
  21. import { call, select, put, all, takeLatest, takeEvery } from 'redux-saga/effects'
  22. import { message } from 'antd'
  23. import request from 'utils/request'
  24. import api from 'utils/api'
  25. import { ActionTypes } from './constants'
  26. import ShareDisplayActions, { ShareDisplayActionType } from './actions'
  27. import { getPasswordUrl } from 'share/util'
  28. import {
  29. makeSelectShareType
  30. } from 'share/containers/App/selectors'
  31. import { displayParamsMigrationRecorder, widgetConfigMigrationRecorder } from 'app/utils/migrationRecorders'
  32. import { SecondaryGraphTypes } from 'app/containers/Display/components/Setting'
  33. import { ILayerRaw, ILayerParams } from 'app/containers/Display/components/types'
  34. import { IWidgetConfig } from 'app/containers/Widget/components/Widget'
  35. export function* getDisplay (action: ShareDisplayActionType) {
  36. if (action.type !== ActionTypes.LOAD_SHARE_DISPLAY) { return }
  37. const { token, resolve, reject } = action.payload
  38. const { loadDisplayFail, displayLoaded } = ShareDisplayActions
  39. const shareType = yield select(makeSelectShareType())
  40. const baseUrl = `${api.share}/display/${token}`
  41. const requestUrl = getPasswordUrl(shareType, token, baseUrl)
  42. try {
  43. const asyncData = yield call(request, requestUrl)
  44. const { header, payload } = asyncData
  45. if (header.code === 401) {
  46. reject(header.msg)
  47. yield put(loadDisplayFail(header.msg))
  48. return
  49. }
  50. const { slides, widgets, views, ...display } = payload
  51. display.config = JSON.parse(display.config || '{}')
  52. slides.sort((s1, s2) => s1.index - s2.index).forEach((slide) => {
  53. slide.config = JSON.parse(slide.config)
  54. slide.relations.forEach((layer: ILayerRaw) => {
  55. const { subType } = layer
  56. const parsedParams: ILayerParams = JSON.parse(layer.params)
  57. layer.params = SecondaryGraphTypes.Label === subType ? displayParamsMigrationRecorder(parsedParams) : parsedParams
  58. })
  59. })
  60. const formedWidgets = widgets.map((widget) => {
  61. const { config, ...rest } = widget
  62. const parsedConfig: IWidgetConfig = JSON.parse(config)
  63. return {
  64. ...rest,
  65. config: widgetConfigMigrationRecorder(parsedConfig, {
  66. viewId: widget.viewId
  67. })
  68. }
  69. })
  70. const formedViews = views.reduce(
  71. (obj, { id, model, variable }) => ({
  72. ...obj,
  73. [id]: {
  74. model: JSON.parse(model),
  75. variable: JSON.parse(variable)
  76. }
  77. }),
  78. {}
  79. )
  80. yield put(displayLoaded(display, slides, formedWidgets, formedViews))
  81. resolve(display, slides, widgets)
  82. } catch (err) {
  83. message.destroy()
  84. yield put(loadDisplayFail(err))
  85. message.error('获取 大屏 信息失败,请刷新重试')
  86. reject(err)
  87. }
  88. }
  89. export function* getData (action: ShareDisplayActionType) {
  90. if (action.type !== ActionTypes.LOAD_LAYER_DATA) { return }
  91. const { renderType, slideNumber, layerId, dataToken, requestParams } = action.payload
  92. const {
  93. filters,
  94. tempFilters, // @TODO combine widget static filters with local filters
  95. linkageFilters,
  96. globalFilters,
  97. variables,
  98. linkageVariables,
  99. globalVariables,
  100. pagination,
  101. ...rest
  102. } = requestParams
  103. const { pageSize, pageNo } = pagination || { pageSize: 0, pageNo: 0 }
  104. const { layerDataLoaded, loadLayerDataFail } = ShareDisplayActions
  105. try {
  106. const response = yield call(request, {
  107. method: 'post',
  108. url: `${api.share}/data/${dataToken}`,
  109. data: {
  110. ...omit(rest, 'customOrders'),
  111. filters: filters.concat(tempFilters).concat(linkageFilters).concat(globalFilters),
  112. params: variables.concat(linkageVariables).concat(globalVariables),
  113. pageSize,
  114. pageNo
  115. }
  116. })
  117. const responsePayload = response.payload || { resultList: [] }
  118. responsePayload.resultList = responsePayload.resultList || []
  119. yield put(layerDataLoaded(renderType, slideNumber, layerId, responsePayload, requestParams))
  120. } catch (err) {
  121. yield put(loadLayerDataFail(slideNumber, layerId, err))
  122. }
  123. }
  124. export default function* rootDisplaySaga () {
  125. yield all([
  126. takeLatest(ActionTypes.LOAD_SHARE_DISPLAY, getDisplay),
  127. takeEvery(ActionTypes.LOAD_LAYER_DATA, getData)
  128. ])
  129. }