contacts.jsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. @author:李霆
  3. @update:2018/05/29
  4. @descript:复制粘贴,拿起来就是干!!
  5. */
  6. import React from 'react';
  7. import './contacts.less';
  8. import { Row, Col, Button, Card, Input, Form, message, Spin } from 'antd';
  9. import ajax from 'jquery/src/ajax/xhr.js';
  10. import $ from 'jquery/src/ajax';
  11. const FormItem = Form.Item;
  12. class Contancts extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. loading: false,
  17. contactData: [],
  18. oldData: []
  19. };
  20. }
  21. loadData() {
  22. this.setState({
  23. loading: true
  24. });
  25. $.ajax({
  26. method: 'get',
  27. dataType: 'json',
  28. crossDomain: false,
  29. url: globalConfig.context + '/api/user/getUserContacts',
  30. data: {},
  31. success: function(data) {
  32. let theData=data.data,
  33. newData=[];
  34. if (data.error.length) {
  35. if (data.error && data.error.length) {
  36. message.warning(data.error[0].message);
  37. }
  38. } else {
  39. theData.map(item=>{
  40. newData.push({
  41. id: item.id,
  42. name: item.name,
  43. edit: false,
  44. department:item.department,
  45. position: item.position,
  46. mobile:item.mobile,
  47. qq: item.qq,
  48. email: item.email,
  49. })
  50. })
  51. this.setState({
  52. contactData: newData,
  53. });
  54. }
  55. }.bind(this)
  56. }).always(
  57. function() {
  58. this.setState({
  59. loading: false
  60. });
  61. }.bind(this)
  62. );
  63. }
  64. //添加联系人
  65. addContact() {
  66. let contactData = this.state.contactData;
  67. contactData.push({
  68. id: '',
  69. name: '',
  70. edit: true,
  71. department: '',
  72. position: '',
  73. mobile: '',
  74. qq: '',
  75. email: '',
  76. //remarks: ''
  77. });
  78. this.setState({ contactData });
  79. }
  80. //保存函数
  81. saveFun(item, index) {
  82. let contactData = this.state.contactData;
  83. if(!item.name||!item.mobile){
  84. message.warning('请填写带 * 号项!');
  85. return;
  86. }
  87. let api = item.id?'/api/user/udpateUserContact':'/api/user/addUserContcat';
  88. this.setState({
  89. loading: true
  90. });
  91. $.ajax({
  92. method: 'post',
  93. dataType: 'json',
  94. url: globalConfig.context + api,
  95. data: {
  96. id: item.id||'',
  97. name: item.name,
  98. department: item.department,
  99. position: item.position,
  100. mobile: item.mobile,
  101. qq: item.qq,
  102. email: item.email,
  103. //remarks: item.remarks
  104. }
  105. }).done(
  106. function(data) {
  107. this.setState({
  108. loading: false
  109. });
  110. if (!data.error.length) {
  111. message.success('操作成功!');
  112. this.loadData();
  113. } else {
  114. message.warning(data.error[0].message);
  115. }
  116. }.bind(this)
  117. );
  118. }
  119. //删除
  120. delFun(item, index) {
  121. let contactData = this.state.contactData;
  122. if (!item.id) {
  123. contactData.splice(index, 1);
  124. this.setState(contactData);
  125. message.success('删除成功');
  126. } else {
  127. this.setState({
  128. loading: true
  129. });
  130. $.ajax({
  131. method: 'get',
  132. dataType: 'json',
  133. url: globalConfig.context + '/api/user/deleteUserContact',
  134. data: {
  135. id: item.id
  136. }
  137. }).done(
  138. function(data) {
  139. this.setState({
  140. loading: false
  141. });
  142. if (!data.error.length) {
  143. message.success('删除成功!');
  144. this.loadData();
  145. } else {
  146. message.warning(data.error[0].message);
  147. }
  148. }.bind(this)
  149. );
  150. }
  151. }
  152. //编辑时
  153. editFun(item, index) {
  154. this.state.contactData[index].edit = true;
  155. this.setState({
  156. contactData: this.state.contactData
  157. });
  158. }
  159. componentWillMount() {
  160. this.loadData();
  161. }
  162. render() {
  163. const formItemLayout = {
  164. labelCol: {
  165. span: 6
  166. },
  167. wrapperCol: {
  168. span: 14
  169. }
  170. };
  171. return (
  172. <div className="contactWrap">
  173. <Spin spinning={this.state.loading}>
  174. <Row>
  175. <Col span={4} offset={1}>
  176. <h4 style={{fontSize:20}}>常用联系人</h4>
  177. </Col>
  178. <Col span={6} offset={8}>
  179. <Button size="large" type="primary" onClick={this.addContact.bind(this)}>
  180. 添加常用联系人
  181. </Button>
  182. </Col>
  183. </Row>
  184. {this.state.contactData.map((item, index) => {
  185. return (
  186. <Row className="card" key={index}>
  187. <Card>
  188. <Row>
  189. <Col span={6}>
  190. <FormItem {...formItemLayout} label="姓名">
  191. {item.edit && (
  192. <Input
  193. style={{ width: '90%' }}
  194. placeholder="联系人姓名"
  195. value={item.name}
  196. onChange={(e) => {
  197. (item.name = e.target.value),
  198. this.setState({ contactData: this.state.contactData });
  199. }}
  200. />
  201. )}
  202. {item.edit ? (
  203. <span className="color-red"> * </span>
  204. ) : (
  205. <span>{item.name}</span>
  206. )}
  207. </FormItem>
  208. </Col>
  209. <Col span={6}>
  210. <FormItem {...formItemLayout} label="部门">
  211. {item.edit ? (
  212. <Input
  213. style={{ width: '90%' }}
  214. placeholder="联系人部门"
  215. value={item.department}
  216. onChange={(e) => {
  217. (item.department = e.target.value),
  218. this.setState({ contactData: this.state.contactData });
  219. }}
  220. />
  221. ) : (
  222. <span>{item.department}</span>
  223. )}
  224. </FormItem>
  225. </Col>
  226. <Col span={6}>
  227. <FormItem {...formItemLayout} label="职务">
  228. {item.edit ? (
  229. <Input
  230. style={{ width: '90%' }}
  231. placeholder="联系人职务"
  232. value={item.position}
  233. onChange={(e) => {
  234. (item.position = e.target.value),
  235. this.setState({ contactData: this.state.contactData });
  236. }}
  237. />
  238. ) : (
  239. <span>{item.position}</span>
  240. )}
  241. </FormItem>
  242. </Col>
  243. <Col span={6}>
  244. <FormItem {...formItemLayout} label="联系电话">
  245. {item.edit && (
  246. <Input
  247. style={{ width: '90%' }}
  248. placeholder="联系人电话号码"
  249. value={item.mobile}
  250. onChange={(e) => {
  251. (item.mobile = e.target.value),
  252. this.setState({ contactData: this.state.contactData });
  253. }}
  254. />
  255. )}
  256. {item.edit ? (
  257. <span className="color-red"> * </span>
  258. ) : (
  259. <span>{item.mobile}</span>
  260. )}
  261. </FormItem>
  262. </Col>
  263. </Row>
  264. <Row>
  265. <Col span={6}>
  266. <FormItem {...formItemLayout} label="QQ">
  267. {item.edit ? (
  268. <Input
  269. style={{ width: '90%' }}
  270. placeholder="联系人QQ"
  271. value={item.qq}
  272. onChange={(e) => {
  273. (item.qq = e.target.value),
  274. this.setState({ contactData: this.state.contactData });
  275. }}
  276. />
  277. ) : (
  278. <span>{item.qq}</span>
  279. )}
  280. </FormItem>
  281. </Col>
  282. <Col span={6}>
  283. <FormItem {...formItemLayout} label="邮箱">
  284. {item.edit ? (
  285. <Input
  286. style={{ width: '90%' }}
  287. placeholder="联系人电子邮箱"
  288. value={item.email}
  289. onChange={(e) => {
  290. (item.email = e.target.value),
  291. this.setState({ contactData: this.state.contactData });
  292. }}
  293. />
  294. ) : (
  295. <span>{item.email}</span>
  296. )}
  297. </FormItem>
  298. </Col>
  299. <Col span={11} style={{textAlign:'right'}}>
  300. {!item.edit ? (
  301. <Button
  302. type="primary"
  303. icon="edit"
  304. size="large"
  305. style={{ marginRight: 20 }}
  306. onClick={() => {
  307. this.editFun(item, index);
  308. }}
  309. />
  310. ) : (
  311. <Button
  312. type="primary"
  313. size="large"
  314. style={{ marginRight: 20 }}
  315. onClick={() => {
  316. this.saveFun(item, index);
  317. }}
  318. >
  319. 保存
  320. </Button>
  321. )}
  322. <Button
  323. type="danger"
  324. size="large"
  325. onClick={() => {
  326. this.delFun(item, index);
  327. }}
  328. >
  329. 删除
  330. </Button>
  331. </Col>
  332. </Row>
  333. {/*<Row>
  334. <Col span={18}>
  335. <FormItem labelCol={{ span: 2 }} wrapperCol={{ span: 20 }} label="备注">
  336. {item.edit ? (
  337. <Input
  338. type="textarea"
  339. placeholder="输入备注"
  340. rows={4}
  341. value={item.remarks}
  342. onChange={(e, pre) => {
  343. (item.remarks = e.target.value),
  344. this.setState({ contactData: this.state.contactData });
  345. }}
  346. />
  347. ) : (
  348. <span>{item.remarks}</span>
  349. )}
  350. </FormItem>
  351. </Col>
  352. </Row>*/}
  353. </Card>
  354. </Row>
  355. );
  356. })}
  357. </Spin>
  358. </div>
  359. );
  360. }
  361. }
  362. export default Contancts;