batchImport.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import { Input, Modal, Button, message } from 'antd';
  3. import { demandTypeList } from '../../dataDic.js';
  4. import ajax from 'jquery/src/ajax/xhr.js';
  5. import $ from 'jquery/src/ajax';
  6. const BatchImport = React.createClass({
  7. getInitialState: function () {
  8. return {
  9. loading: false,
  10. error: false
  11. };
  12. },
  13. showModal() {
  14. this.setState({
  15. visible: true,
  16. });
  17. },
  18. handleOk(e) {
  19. this.setState({
  20. visible: false,
  21. });
  22. },
  23. handleCancel(e) {
  24. this.setState({
  25. visible: false,
  26. });
  27. },
  28. //数据反向转化
  29. getDataCategory(e, row) {
  30. switch (e) {
  31. case "个人需求":
  32. return 0;
  33. case "单位需求":
  34. return 1;
  35. default:
  36. message.warning("第 " + row + " 行中没有对应的数据类型!");
  37. this.state.error = true;
  38. break;
  39. }
  40. },
  41. getDemandType(e, row) {
  42. let theType, _me = this;
  43. if (e) {
  44. demandTypeList.map(function (item) {
  45. if (item.key == e) {
  46. theType = Number(item.value);
  47. };
  48. });
  49. if (theType == null) {
  50. message.warning("第 " + row + " 行中没有对应的需求类型!");
  51. _me.state.error = true;
  52. }
  53. return theType;
  54. };
  55. },
  56. //提交
  57. handleSubmit() {
  58. let rowsArr = this.state.inputValue.split(/[\r\n]/g);
  59. let columnArr = [], _me = this;
  60. if (!rowsArr.length) {
  61. return;
  62. };
  63. let columnLength = 9;//定义数据正常长度
  64. for (let i = 0; i < rowsArr.length; i++) {
  65. if (_me.state.error) {
  66. this.state.error = false;
  67. return;
  68. };
  69. let theArr = rowsArr[i].split(/\t/);
  70. if (theArr.length == columnLength) {
  71. columnArr.push({
  72. dataCategory: _me.getDataCategory(theArr[0], i + 1),
  73. name: theArr[1],
  74. keyword: theArr[2],
  75. demandType: _me.getDemandType(theArr[3], i + 1),
  76. problemDes: theArr[4],
  77. employerName: theArr[5],
  78. employerContacts: theArr[6],
  79. employerContactsMobile: theArr[7],
  80. employerContactsMailbox: theArr[8]
  81. });
  82. } else if (rowsArr[i].replace(/(^\s+)|(\s+$)/g, "").length) {
  83. message.warning("第" + (i + 1) + "行数据长度错误!");
  84. return;
  85. };
  86. };
  87. if (columnArr.length > 1000) {
  88. message.warning("数据条数不能多于1000条!");
  89. return;
  90. };
  91. this.setState({
  92. loading: true
  93. });
  94. $.ajax({
  95. method: "POST",
  96. dataType: "json",
  97. crossDomain: false,
  98. url: globalConfig.context + '/api/admin/demand/importDemand',
  99. data: {
  100. data: JSON.stringify(columnArr)
  101. }
  102. }).done(function (data) {
  103. this.setState({
  104. loading: false
  105. });
  106. if (!data.error.length) {
  107. message.success('导入成功!');
  108. this.props.closeDesc(false, true);
  109. this.handleOk();
  110. } else {
  111. message.warning(data.error[0].message);
  112. };
  113. }.bind(this));
  114. },
  115. render() {
  116. return (
  117. <div>
  118. <Button type="primary" onClick={this.showModal}>批量导入</Button>
  119. <Modal
  120. title="批量导入"
  121. visible={this.state.visible}
  122. onCancel={this.handleCancel}
  123. width='1200px'
  124. footer={[
  125. <Button key="ok" className="set-submit" type="primary" loading={this.state.loading} onClick={this.handleSubmit}>保存</Button>,
  126. <Button key="clancel" className="set-submit" type="ghost" onClick={this.handleCancel}>取消</Button>
  127. ]} >
  128. <Input type="textarea" rows={12} onChange={(e) => { this.state.inputValue = e.target.value; }} />
  129. </Modal>
  130. </div>
  131. );
  132. }
  133. });
  134. export default BatchImport;