person.jsx 14 KB

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