NodePublicService.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import React from 'react'
  2. import { Switch, Select } from 'antd'
  3. import cls from 'classnames'
  4. import i18n from 'i18next'
  5. import treeActions from 'actions/tree'
  6. import cooperateActions from 'actions/cooperate'
  7. import treeStores from 'stores/tree'
  8. import globalConfigStore from 'stores/globalConfig'
  9. import { getEventNodeById } from 'stores/funcs/newEvent'
  10. import { isSa, isHySa } from 'utils/commons/ctrlHide'
  11. import externalPath from 'api/externalPath'
  12. import iconStrObj from 'src/assets/icons/iconsStr'
  13. export default class NodePublicService extends React.Component {
  14. constructor(props) {
  15. super(props)
  16. this.state = {
  17. publicService: this.getPublicService(),
  18. servicePath: this.getServicePath(),
  19. responseType: this.getResponseType(),
  20. receiveType: this.getReceiveType(),
  21. workSettings: globalConfigStore.workSettings,
  22. isCusDomain: this.dealCusDomain(globalConfigStore.workSettings)
  23. }
  24. this.responseTypeOptions = ['json', 'xml']
  25. // this.receiveTypeOptions = ['json', 'string', 'array', 'base64']
  26. this.receiveTypeOptions = ['json', 'string', 'array']
  27. this.onConfigChange = this.onConfigChange.bind(this)
  28. this.getServicePath = this.getServicePath.bind(this)
  29. this.getPublicService = this.getPublicService.bind(this)
  30. this.getResponseType = this.getResponseType.bind(this)
  31. this.getReceiveType = this.getReceiveType.bind(this)
  32. this.onChangeProps = this.onChangeProps.bind(this)
  33. this.onChangingPath = this.onChangingPath.bind(this)
  34. this.onChangedPath = this.onChangedPath.bind(this)
  35. this.onCopy = this.onCopy.bind(this)
  36. this.onCopyPublishPath = this.onCopyPublishPath.bind(this)
  37. this.onCopyPreviewPath = this.onCopyPreviewPath.bind(this)
  38. this.onChangeResponseType = this.onChangeResponseType.bind(this)
  39. this.onChangeReceiveType = this.onChangeReceiveType.bind(this)
  40. this.pathPrefix = this.pathPrefix.bind(this)
  41. this.publishPath = this.publishPath.bind(this)
  42. this.previewPath = this.previewPath.bind(this)
  43. this.targetPath = this.targetPath.bind(this)
  44. this.sidPath = this.sidPath.bind(this)
  45. this.isGroupWork = this.isGroupWork.bind(this)
  46. }
  47. componentDidMount() {
  48. this.unsubscribe = globalConfigStore.listen(this.onConfigChange)
  49. }
  50. componentWillUnmount() {
  51. this.unsubscribe()
  52. }
  53. componentDidUpdate(preProps) {
  54. if (this.props.nodeId !== preProps.nodeId) {
  55. this.setState({
  56. servicePath: this.getServicePath(),
  57. publicService: this.getPublicService(),
  58. responseType: this.getResponseType(),
  59. receiveType: this.getReceiveType(),
  60. isCusDomain: this.dealCusDomain(this.state.workSettings)
  61. })
  62. }
  63. }
  64. dealCusDomain(workSettings) {
  65. return [false, undefined].indexOf(treeStores.curCaseInfo.isPublished) >= 0
  66. ? false // 首次发布配置默认使用平台公共域名
  67. : workSettings
  68. ? workSettings.customDomain
  69. : false
  70. }
  71. onConfigChange(status) {
  72. let obj = {}
  73. if (status.types) {
  74. if (status.types.includes('workSettings')) {
  75. obj.workSettings = status.data.workSettings
  76. obj.isCusDomain = this.dealCusDomain(obj.workSettings)
  77. }
  78. }
  79. if (Object.keys(obj).length > 0) {
  80. this.setState(obj)
  81. }
  82. }
  83. getServicePath() {
  84. let node = getEventNodeById(this.props.nodeId)
  85. return node && node.props ? node.props.servicePath || '' : ''
  86. }
  87. getPublicService() {
  88. let node = getEventNodeById(this.props.nodeId)
  89. return node && node.props && node.props.publicService !== undefined
  90. ? node.props.publicService
  91. : false
  92. }
  93. getResponseType() {
  94. let node = getEventNodeById(this.props.nodeId)
  95. return node && node.props && node.props.responseType !== undefined
  96. ? node.props.responseType
  97. : 'json'
  98. }
  99. getReceiveType() {
  100. let node = getEventNodeById(this.props.nodeId)
  101. return node && node.props && node.props.receiveType !== undefined
  102. ? node.props.receiveType
  103. : 'json'
  104. }
  105. onChangeProps(type, value) {
  106. let props = {}
  107. switch (type) {
  108. case 'publicService':
  109. props.publicService = value
  110. if (props.publicService && !this.state.servicePath) {
  111. props.servicePath = this.sidPath()
  112. }
  113. break
  114. case 'servicePath':
  115. props.servicePath = value
  116. if (!value && this.state.publicService) {
  117. props.servicePath = this.sidPath()
  118. }
  119. break
  120. case 'responseType':
  121. props.responseType = value
  122. break
  123. case 'receiveType':
  124. props.receiveType = value
  125. break
  126. }
  127. if (Object.keys(props).length > 0) {
  128. this.setState(props)
  129. treeActions.changeNodeProps({
  130. nodeId: this.props.nodeId,
  131. props: props
  132. })
  133. }
  134. }
  135. onChangingPath(e) {
  136. let value = e && e.target ? e.target.value : e
  137. this.setState({
  138. servicePath: value
  139. })
  140. }
  141. onChangedPath() {
  142. this.onChangeProps('servicePath', this.state.servicePath)
  143. }
  144. onCopy(value) {
  145. let tInput = document.createElement('textarea')
  146. tInput.style.width = '0px'
  147. tInput.style.height = '0px'
  148. tInput.value = value
  149. document.body.appendChild(tInput)
  150. tInput.select()
  151. document.execCommand('copy')
  152. document.body.removeChild(tInput)
  153. }
  154. onCopyPublishPath() {
  155. this.onCopy(this.publishPath())
  156. }
  157. onCopyPreviewPath() {
  158. this.onCopy(this.previewPath())
  159. }
  160. onChangeResponseType(e) {
  161. this.setState(
  162. {
  163. responseType: e
  164. },
  165. () => {
  166. this.onChangeProps('responseType', this.state.responseType)
  167. }
  168. )
  169. }
  170. onChangeReceiveType(e) {
  171. this.setState(
  172. {
  173. receiveType: e
  174. },
  175. () => {
  176. this.onChangeProps('receiveType', this.state.receiveType)
  177. }
  178. )
  179. }
  180. pathPrefix(isPreview) {
  181. let prefix =
  182. (isPreview ? 'https://v4pre.h5sys.' : 'https://v4rel.h5sys.') +
  183. (i18n.language === 'zh' ? 'cn' : 'com')
  184. let { isCusDomain, workSettings } = this.state
  185. if (isCusDomain) {
  186. if (isPreview && workSettings && workSettings.previewDomain) {
  187. prefix = workSettings.previewDomain
  188. } else if (!isPreview && workSettings && workSettings.domain) {
  189. prefix = workSettings.domain
  190. }
  191. }
  192. if (isSa() && isPreview) {
  193. let protocol = location.protocol + '//'
  194. if (workSettings && workSettings.previewDomain) {
  195. prefix = protocol + workSettings.previewDomain
  196. } else {
  197. prefix = protocol + document.domain
  198. }
  199. }
  200. return prefix + '/api/' + treeStores.curCaseId + '/'
  201. }
  202. publishPath() {
  203. return this.pathPrefix() + this.targetPath()
  204. }
  205. previewPath() {
  206. return this.pathPrefix(true) + this.targetPath()
  207. }
  208. targetPath() {
  209. // let node = getEventNodeById(this.props.nodeId)
  210. return this.state.servicePath // || (node && node.id) || ''
  211. }
  212. sidPath() {
  213. let node = getEventNodeById(this.props.nodeId)
  214. return (node && node.id) || ''
  215. }
  216. isGroupWork() {
  217. let showGroupTips = false
  218. cooperateActions.getIsGroupWork(isGroupWork => {
  219. if (isGroupWork) {
  220. showGroupTips = true
  221. }
  222. })
  223. return showGroupTips
  224. }
  225. render() {
  226. // 小模块不需要
  227. if (treeStores.curClassId) {
  228. return null
  229. }
  230. // 多人开发添加client
  231. return (
  232. <div className={cls('jenga-node-public-service')}>
  233. <div className="row-wrap f--hlc">
  234. <div className="title">
  235. {i18n.t('EventView.jenga_extra.public_service')}
  236. </div>
  237. <Switch
  238. checked={this.state.publicService}
  239. onChange={this.onChangeProps.bind(this, 'publicService')}
  240. className="public-service-switch"
  241. />
  242. {!isHySa() && (
  243. <a
  244. className="help-link"
  245. href={externalPath.publicService}
  246. target="_blank"
  247. >
  248. <i
  249. className="ih5-icon-help"
  250. dangerouslySetInnerHTML={{ __html: iconStrObj.help }}
  251. />
  252. </a>
  253. )}
  254. {this.state.publicService ? (
  255. <>
  256. <div className="path-info">
  257. <div className="f--hlc">
  258. {i18n.t('EventView.jenga_extra.service_path')}
  259. <input
  260. className="path-input"
  261. onChange={this.onChangingPath}
  262. onBlur={this.onChangedPath}
  263. value={this.state.servicePath}
  264. placeholder={i18n.t(
  265. 'EventView.jenga_extra.input_path_placeholder'
  266. )}
  267. />
  268. </div>
  269. </div>
  270. <div className="response-type">
  271. {i18n.t('EventView.jenga_extra.receive_type')}
  272. <Select
  273. onChange={this.onChangeReceiveType}
  274. getPopupContainer={triggerNode => triggerNode.parentNode}
  275. value={this.state.receiveType}
  276. size="small"
  277. className="response-type-select"
  278. dropdownClassName="response-type-select-dropdown"
  279. >
  280. {this.receiveTypeOptions.map((op, optIndex) => (
  281. <Select.Option value={op} key={optIndex}>
  282. {i18n.t(
  283. 'EventView.jenga_extra.receive_type_option.' + op
  284. )}
  285. </Select.Option>
  286. ))}
  287. </Select>
  288. </div>
  289. <div className="response-type">
  290. {i18n.t('EventView.jenga_extra.response_type')}
  291. <Select
  292. onChange={this.onChangeResponseType}
  293. getPopupContainer={triggerNode => triggerNode.parentNode}
  294. value={this.state.responseType}
  295. size="small"
  296. className="response-type-select"
  297. dropdownClassName="response-type-select-dropdown"
  298. >
  299. {this.responseTypeOptions.map((op, optIndex) => (
  300. <Select.Option value={op} key={optIndex}>
  301. {i18n.t(
  302. 'EventView.jenga_extra.response_type_option.' + op
  303. )}
  304. </Select.Option>
  305. ))}
  306. </Select>
  307. </div>
  308. </>
  309. ) : null}
  310. </div>
  311. {this.state.publicService ? (
  312. <>
  313. <div className="row-wrap path-wrap f--hlc">
  314. <div className="path-info">
  315. <div className="f--hlc preview-info">
  316. {i18n.t('EventView.jenga_extra.preview_path')}
  317. {this.pathPrefix(true)}
  318. <div className="path-value" title={this.targetPath()}>
  319. {this.targetPath()}
  320. </div>
  321. <button
  322. className="btn-clear btn-copy-path"
  323. onClick={this.onCopyPreviewPath}
  324. >
  325. {i18n.t('EventView.jenga_extra.copy_path')}
  326. </button>
  327. </div>
  328. {this.isGroupWork() && (
  329. <div className="notice-wrap f--hlc">
  330. <div
  331. className="icon"
  332. dangerouslySetInnerHTML={{
  333. __html: iconStrObj.noticeIcon
  334. }}
  335. />
  336. <div className="notice-info">
  337. {i18n.t(
  338. 'EventView.jenga_extra.preview_group_work_notice'
  339. )}
  340. </div>
  341. </div>
  342. )}
  343. </div>
  344. </div>
  345. {!isSa() && (
  346. <div className="row-wrap path-wrap f--hlc">
  347. <div className="path-info">
  348. <div className="f--hlc publish_path">
  349. {i18n.t('EventView.jenga_extra.publish_path')}
  350. {this.pathPrefix()}
  351. <div className="path-value" title={this.targetPath()}>
  352. {this.targetPath()}
  353. </div>
  354. <button
  355. className="btn-clear btn-copy-path"
  356. onClick={this.onCopyPublishPath}
  357. >
  358. {i18n.t('EventView.jenga_extra.copy_path')}
  359. </button>
  360. </div>
  361. <div className="notice-wrap f--hlc">
  362. <div
  363. className="icon"
  364. dangerouslySetInnerHTML={{
  365. __html: iconStrObj.noticeIcon
  366. }}
  367. />
  368. <div className="notice-info">
  369. {i18n.t('EventView.jenga_extra.publish_notice')}
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. )}
  375. </>
  376. ) : null}
  377. </div>
  378. )
  379. }
  380. }