edit_related_template.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { connect } from 'dva/index';
  2. import React, { Component } from 'react';
  3. import { Form } from 'antd';
  4. import {
  5. sldLlineRtextAddGoods,
  6. failTip,
  7. sucTip,
  8. getSldEmptyH,
  9. sldComLanguage,
  10. quillEscapeToHtml,
  11. } from '@/utils/utils';
  12. import global from '@/global.less';
  13. import SldEditFormCom from '@/components/SldEditFormCom/SldEditFormCom';
  14. import SldUEditor from '@/components/SldUEditor';
  15. import { Scrollbars } from 'react-custom-scrollbars';
  16. @connect(({ product, global }) => ({
  17. product, global,
  18. }))
  19. @Form.create()
  20. export default class EditRelatedTemplate extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. initEditorFlag: false,//加载百度编辑器
  25. getEditorContentFlag: false,//获取百度编辑器内容标识
  26. initEditorContent: '',//百度编辑器内容
  27. flag: false,
  28. template_con: '',//关联版式内容
  29. query: props.location.query,
  30. operate_data: [{
  31. type: 'input',
  32. label: `${sldComLanguage('版式名称')}`,
  33. name: 'templateName',
  34. placeholder: `${sldComLanguage('请输入版式名称')}`,
  35. initialValue: '',
  36. maxLength: 10,
  37. rules: [{
  38. required: true,
  39. whitespace: true,
  40. message: `${sldComLanguage('请输入版式名称')}`,
  41. }],
  42. }],
  43. positionData: [{
  44. type: 'radio',
  45. label: `${sldComLanguage('版式位置')}`,
  46. name: 'templatePosition',
  47. placeholder: ``,
  48. initialValue: 1,
  49. sel_data: [{
  50. name: '顶部', key: 1,
  51. }, {
  52. name: '底部', key: 2,
  53. }],
  54. onChange: this.changePosition,
  55. }],
  56. };
  57. }
  58. componentDidMount() {
  59. const { query } = this.state;
  60. this.props.dispatch({
  61. type: 'global/getLayoutCollapsed',
  62. });
  63. if (query.id != undefined && query.id > 0) {
  64. this.get_detail();
  65. } else {
  66. this.setState({ initEditorFlag: true });
  67. }
  68. }
  69. componentWillUnmount() {
  70. }
  71. //获取关联版式详情
  72. get_detail = () => {
  73. const { dispatch } = this.props;
  74. let { operate_data, query, positionData } = this.state;
  75. dispatch({
  76. type: 'product/get_related_template_detail',
  77. payload: { templateId: query.id },
  78. callback: (res) => {
  79. for (let i in operate_data) {
  80. operate_data[i].initialValue = res.data[operate_data[i].name];
  81. }
  82. for (let pos in positionData) {
  83. positionData[pos].initialValue = res.data[positionData[pos].name];
  84. }
  85. this.setState({
  86. operate_data,
  87. positionData,
  88. initEditorContent: quillEscapeToHtml(res.data.templateContent),
  89. flag: true,
  90. initEditorFlag: true,
  91. });
  92. },
  93. });
  94. };
  95. changePosition = (val) => {
  96. let { positionData } = this.state;
  97. let tmpData = positionData.filter(item => item.name == 'templatePosition')[0];
  98. tmpData.initialValue = val;
  99. this.setState({ positionData });
  100. };
  101. handleSaveAllData = () => {
  102. this.setState({ getEditorContentFlag: true });
  103. };
  104. //保存并新增事件
  105. saveData = (editorCon) => {
  106. let _this = this;
  107. this.props.form.validateFieldsAndScroll((err, values) => {
  108. if (!err) {
  109. const { dispatch } = this.props;
  110. values.templateContent = editorCon;
  111. const { query } = this.state;
  112. values.templateId = query.id;
  113. let dis_type = '';
  114. if (query.id != undefined && query.id > 0) {
  115. dis_type = 'product/edit_related_template';
  116. } else {
  117. dis_type = 'product/add_related_template';
  118. }
  119. dispatch({
  120. type: dis_type,
  121. payload: values,
  122. callback: (res) => {
  123. if (res.state == 200) {
  124. sucTip(res.msg);
  125. _this.props.history.goBack();
  126. } else {
  127. failTip(res.msg);
  128. }
  129. },
  130. });
  131. }
  132. });
  133. };
  134. //获取编辑器内容
  135. getEditorContent = (con) => {
  136. this.saveData(con);
  137. this.setState({ getEditorContentFlag: false });
  138. };
  139. render() {
  140. const { operate_data, positionData, query, flag, initEditorFlag, getEditorContentFlag, initEditorContent } = this.state;
  141. return (
  142. <div className={global.common_page_20}
  143. style={{ display: 'flex', flexDirection: 'column', justifyContent: 'flex-start' }}>
  144. {sldLlineRtextAddGoods('#69A2F2', `${sldComLanguage('基本信息')}`)}
  145. <Scrollbars
  146. autoHeight
  147. autoHeightMin={100}
  148. autoHeightMax={document.body.clientHeight - 140}
  149. >
  150. <div className={global.tableListFormAdd}>
  151. <div style={{ display: 'flex', flexDirection: 'column' }}>
  152. <Form onSubmit={() => this.handleSaveAllData()} layout="inline">
  153. <SldEditFormCom form={this.props.form} search_data={operate_data}/>
  154. {getSldEmptyH(10)}
  155. {(query.id == undefined || (query.id != undefined && flag)) &&
  156. <SldEditFormCom form={this.props.form} search_data={positionData}/>}
  157. {getSldEmptyH(15)}
  158. {sldLlineRtextAddGoods('#69A2F2', `${sldComLanguage('内容编辑')}`)}
  159. <div className={global.goods_sku_tab} style={{ marginTop: 20, position: 'relative' }}>
  160. {initEditorFlag &&
  161. <SldUEditor id={'agreement'} getContentFlag={getEditorContentFlag}
  162. getEditorContent={this.getEditorContent} initEditorContent={initEditorContent}/>
  163. }
  164. </div>
  165. <div className={global.m_diy_bottom_wrap}
  166. style={{ position: 'fixed', left: this.props.global.collapsed ? 90 : 160 }}>
  167. <div onClick={() => this.props.history.goBack()} className={global.add_goods_bottom_btn}>
  168. {sldComLanguage('返回')}
  169. </div>
  170. <div onClick={() => this.handleSaveAllData()}
  171. className={`${global.add_goods_bottom_btn} ${global.add_goods_bottom_btn_sel}`}>
  172. {sldComLanguage('保存并返回')}
  173. </div>
  174. </div>
  175. </Form>
  176. </div>
  177. </div>
  178. </Scrollbars>
  179. </div>
  180. );
  181. }
  182. }