person.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import React from 'react';
  2. import { Input, Button, Form, Select, Radio, Upload, Icon, Spin, message, Modal } from 'antd';
  3. import './person.less';
  4. import { addressInit, splitUrl } from '../../tools.js';
  5. import ajax from 'jquery/src/ajax/xhr.js'
  6. import $ from 'jquery/src/ajax';
  7. const FormItem = Form.Item;
  8. const Option = Select.Option;
  9. const RadioGroup = Radio.Group;
  10. function getBase64(img, callback) {
  11. const reader = new FileReader();
  12. reader.addEventListener('load', () => callback(reader.result));
  13. reader.readAsDataURL(img);
  14. };
  15. function beforeUpload(file) {
  16. const isJPG = file.type === 'image/jpeg';
  17. if (!isJPG) {
  18. message.error('You can only upload JPG file!');
  19. }
  20. const isLt2M = file.size / 1024 / 1024 < 2;
  21. if (!isLt2M) {
  22. message.error('Image must smaller than 2MB!');
  23. }
  24. return isJPG && isLt2M;
  25. };
  26. class Avatar extends React.Component {
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. imageUrl: '',
  31. propsbool: true,
  32. }
  33. }
  34. handleChange(info) {
  35. if (info.file.status === 'done') {
  36. // Get this url from response in real world.
  37. getBase64(info.file.originFileObj, imageUrl => this.setState({ imageUrl }));
  38. this.props.urlData(info.file.response.data);
  39. }
  40. }
  41. componentWillReceiveProps(nextProps) {
  42. if (nextProps.picUrl && this.state.propsbool) {
  43. this.state.imageUrl = globalConfig.avatarHost + "/upload" + nextProps.picUrl;
  44. this.state.propsbool = false;
  45. }
  46. }
  47. render() {
  48. const imageUrl = this.state.imageUrl;
  49. return (
  50. <Upload
  51. className="avatar-uploader"
  52. name="avatar"
  53. showUploadList={false}
  54. action={globalConfig.context + "/api/user/avatar/uploadReplace"}
  55. data={{ 'sign': this.props.name }}
  56. beforeUpload={beforeUpload}
  57. onChange={this.handleChange.bind(this)}
  58. >
  59. {
  60. imageUrl ?
  61. <img src={imageUrl} role="presentation" className="avatar" /> :
  62. <Icon type="plus" className="avatar-uploader-trigger" />
  63. }
  64. </Upload>
  65. );
  66. }
  67. };
  68. class PicturesWall extends React.Component {
  69. constructor(props) {
  70. super(props);
  71. this.state = {
  72. previewVisible: false,
  73. propsbool: true,
  74. previewImage: '',
  75. fileList: [],
  76. }
  77. }
  78. handleCancel() {
  79. this.setState({ previewVisible: false })
  80. }
  81. handlePreview(file) {
  82. this.setState({
  83. previewImage: file.url || file.thumbUrl,
  84. previewVisible: true,
  85. });
  86. }
  87. handleChange(info) {
  88. let fileList = info.fileList;
  89. this.setState({ fileList });
  90. this.props.fileList(fileList);
  91. }
  92. componentWillReceiveProps(nextProps) {
  93. if (nextProps.lifePhotoList && this.state.propsbool) {
  94. this.state.fileList = nextProps.lifePhotoList;
  95. this.state.propsbool = false;
  96. }
  97. }
  98. render() {
  99. const { previewVisible, previewImage, fileList } = this.state;
  100. const uploadButton = (
  101. <div>
  102. <Icon type="plus" />
  103. <div className="ant-upload-text">Upload</div>
  104. </div>
  105. );
  106. return (
  107. <div className="clearfix">
  108. <Upload
  109. action={globalConfig.context + "/api/user/avatar/upload"}
  110. listType="picture-card"
  111. fileList={fileList}
  112. onPreview={this.handlePreview}
  113. onChange={this.handleChange.bind(this)}
  114. >
  115. {fileList.length >= 9 ? null : uploadButton}
  116. </Upload>
  117. <Modal visible={previewVisible} footer={null} onCancel={this.handleCancel}>
  118. <img alt="example" style={{ width: '100%' }} src={previewImage} />
  119. </Modal>
  120. </div>
  121. );
  122. }
  123. };
  124. const PersonFrom = Form.create()(React.createClass({
  125. getData() {
  126. this.props.spinState(true);
  127. $.ajax({
  128. method: "get",
  129. dataType: "json",
  130. url: globalConfig.context + "/api/user/member",
  131. success: function (data) {
  132. if (data.data) {
  133. addressInit('cmbProvince', 'cmbCity', 'cmbArea',
  134. data.data.residenceProvince, data.data.residenceCity, data.data.residenceArea);
  135. let theArr = splitUrl(data.data.lifePhotoUrl, ',', globalConfig.avatarHost + '/upload');
  136. this.setState({
  137. nickname: data.data.nickname,
  138. cmbP: data.data.residenceProvince,
  139. cmbC: data.data.residenceCity,
  140. cmbA: data.data.residenceArea,
  141. intro: data.data.personalProfile,
  142. email: data.data.email,
  143. fixTelArea: data.data.fixedTelArea,
  144. fixTel: data.data.fixedTel,
  145. fixTelExtension: data.data.fixedTelExtension,
  146. qq: data.data.qq,
  147. personPortraitUrl: data.data.personPortraitUrl,
  148. address: data.data.postalAddress,
  149. postcode: data.data.postcode,
  150. lifePhotoList: theArr
  151. });
  152. }
  153. }.bind(this),
  154. }).always(function () {
  155. this.props.spinState(false);
  156. }.bind(this));
  157. },
  158. getInitialState() {
  159. return {
  160. loading: false,
  161. livePicUrlArr: []
  162. };
  163. },
  164. handleSubmit(e) {
  165. e.preventDefault();
  166. this.props.form.validateFields((err, values) => {
  167. //地址值
  168. let cmbP = document.getElementById('cmbProvince').value;
  169. let cmbC = document.getElementById('cmbCity').value;
  170. let cmbA = document.getElementById('cmbArea').value;
  171. //lifePhotoUrl
  172. let livePicArr = [];
  173. this.state.lifePhotoList.map(function (item) {
  174. livePicArr.push(item.response.data);
  175. });
  176. let lifePhotoUrl = livePicArr.join(",");
  177. if (!err) {
  178. this.props.spinState(true);
  179. $.ajax({
  180. method: "POST",
  181. dataType: "json",
  182. crossDomain: false,
  183. url: globalConfig.context + "/api/user/userInfo",
  184. data: {
  185. "nickname": values.nickname,
  186. "residenceProvince": cmbP,
  187. "residenceCity": cmbC,
  188. "residenceArea": cmbA,
  189. "personPortraitUrl": this.state.avatarUrlData,
  190. "lifePhotoUrl": lifePhotoUrl,
  191. "personalProfile": values.intro,
  192. "email": values.email,
  193. "fixedTelArea": this.state.fixTelArea,
  194. "fixedTel": this.state.fixTel,
  195. "fixedTelExtension": this.state.fixTelExtension,
  196. "qq": values.qq,
  197. "postalAddress": values.address,
  198. "postcode": values.postcode
  199. }
  200. }).done(function (data) {
  201. if (!data.error.length) {
  202. message.success('保存成功!');
  203. } else {
  204. message.warning(data.error[0].message);
  205. }
  206. }.bind(this)).always(function () {
  207. this.props.spinState(false);
  208. }.bind(this));
  209. }
  210. });
  211. },
  212. avatarUrl(e) {
  213. this.state.avatarUrlData = e;
  214. },
  215. livePicFileList(e) {
  216. this.state.lifePhotoList = e;
  217. },
  218. componentDidMount() {
  219. addressInit('cmbProvince', 'cmbCity', 'cmbArea');
  220. this.getData();
  221. },
  222. fixTelAreaChange(e) {
  223. this.setState({
  224. fixTelArea: e.target.value
  225. });
  226. },
  227. fixTelChange(e) {
  228. this.setState({
  229. fixTel: e.target.value
  230. });
  231. },
  232. fixTelExtensionChange(e) {
  233. this.setState({
  234. fixTelExtension: e.target.value
  235. });
  236. },
  237. render() {
  238. const { getFieldDecorator } = this.props.form;
  239. const formItemLayout = {
  240. labelCol: { span: 3 },
  241. wrapperCol: { span: 12 },
  242. };
  243. const _me = this;
  244. return (
  245. <Form horizontal onSubmit={this.handleSubmit} className="person-form">
  246. <FormItem
  247. {...formItemLayout}
  248. label="社区昵称"
  249. >
  250. {getFieldDecorator('nickname', {
  251. initialValue: this.state.nickname || null
  252. })(
  253. <Input />
  254. )}
  255. </FormItem>
  256. <FormItem
  257. {...formItemLayout}
  258. label="居住地"
  259. >
  260. {getFieldDecorator('province', {
  261. initialValue: this.state.cmbA || null
  262. })(
  263. <div>
  264. <select id="cmbProvince" name="cmbProvince"></select>
  265. <select id="cmbCity" name="cmbCity"></select>
  266. <select id="cmbArea" name="cmbArea"></select>
  267. </div>
  268. )}
  269. </FormItem>
  270. <FormItem
  271. {...formItemLayout}
  272. label="个人头像"
  273. labelCol={{ span: 3 }}
  274. wrapperCol={{ span: 21 }}
  275. >
  276. {getFieldDecorator('avatar')(
  277. <Avatar urlData={this.avatarUrl} name='personPortrait' picUrl={this.state.personPortraitUrl} />
  278. )}
  279. </FormItem>
  280. <div className="set-explain">
  281. <p>请上传清晰的个人身份证正反面原件照片/扫描件 (不能截图、图片内容要求完整 ),</p>
  282. <p>要求2M 以下的jpg、jpeg格式的图片。该资料不会公开展示,仅作为审核材料使用。</p>
  283. </div>
  284. <FormItem
  285. labelCol={{ span: 3 }}
  286. wrapperCol={{ span: 21 }}
  287. label="生活照"
  288. >
  289. {getFieldDecorator('livePic')(
  290. <PicturesWall fileList={this.livePicFileList} lifePhotoList={this.state.lifePhotoList} />
  291. )}
  292. </FormItem>
  293. <FormItem
  294. labelCol={{ span: 3 }}
  295. wrapperCol={{ span: 20 }}
  296. label="个人简介"
  297. >
  298. {getFieldDecorator('intro', {
  299. initialValue: this.state.intro || null
  300. })(
  301. <Input type="textarea"
  302. placeholder="全面介绍自己,让商友更深入了解您,内容长度不超过1000个字。"
  303. rows={6} />
  304. )}
  305. <p>还可输入{
  306. (() => {
  307. if (_me.props.form.getFieldValue('intro') && _me.props.form.getFieldValue('intro').length) {
  308. return 1000 - _me.props.form.getFieldValue('intro').length;
  309. } else {
  310. return 1000;
  311. }
  312. })()
  313. }字/1000</p>
  314. </FormItem>
  315. <FormItem
  316. {...formItemLayout}
  317. label="电子邮箱"
  318. hasFeedback
  319. >
  320. {getFieldDecorator('email', {
  321. rules: [{
  322. type: 'email', message: '请输入正确的邮箱地址!',
  323. }],
  324. initialValue: this.state.email || null
  325. })(
  326. <Input />
  327. )}
  328. </FormItem>
  329. <FormItem
  330. {...formItemLayout}
  331. label="固定电话"
  332. >
  333. {getFieldDecorator('telephone')(
  334. <p>
  335. <Input value={this.state.fixTelArea} id="fixTelArea" onChange={this.fixTelAreaChange} /> - <Input value={this.state.fixTel} id="fixTel" onChange={this.fixTelChange} /> - <Input value={this.state.fixTelExtension} id="fixTelExtension" onChange={this.fixTelExtensionChange} />
  336. </p>
  337. )}
  338. </FormItem>
  339. <FormItem
  340. {...formItemLayout}
  341. label="QQ"
  342. >
  343. {getFieldDecorator('qq', {
  344. initialValue: this.state.qq || null
  345. })(
  346. <Input />
  347. )}
  348. </FormItem>
  349. <FormItem
  350. {...formItemLayout}
  351. label="通讯地址"
  352. >
  353. {getFieldDecorator('address', {
  354. initialValue: this.state.address || null
  355. })(
  356. <Input />
  357. )}
  358. </FormItem>
  359. <FormItem
  360. {...formItemLayout}
  361. label="邮编"
  362. >
  363. {getFieldDecorator('postcode', {
  364. initialValue: this.state.postcode || null
  365. })(
  366. <Input />
  367. )}
  368. </FormItem>
  369. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  370. <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
  371. </FormItem>
  372. </Form >
  373. );
  374. },
  375. }));
  376. const Person = React.createClass({
  377. getInitialState() {
  378. return {
  379. loading: false
  380. };
  381. },
  382. spinChange(e) {
  383. this.setState({
  384. loading: e
  385. });
  386. },
  387. render() {
  388. return (
  389. <Spin spinning={this.state.loading} className='spin-box'>
  390. <div className="set-person">
  391. <div className="acc-detail">
  392. <p className="acc-title">个人资料</p>
  393. <PersonFrom spinState={this.spinChange} />
  394. </div>
  395. </div>
  396. </Spin>
  397. )
  398. }
  399. });
  400. export default Person;