person.jsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 && 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. livePicArr.push(item.response.data);
  154. });
  155. let lifePhotoUrl = livePicArr.join(",");
  156. if (!err) {
  157. this.props.spinState(true);
  158. $.ajax({
  159. method: "POST",
  160. dataType: "json",
  161. crossDomain: false,
  162. url: globalConfig.context + "/api/user/userInfo",
  163. data: {
  164. "id": this.state.id,
  165. "nickname": values.nickname,
  166. "residenceProvince": values.theAddress[0],
  167. "residenceCity": values.theAddress[1],
  168. "residenceArea": values.theAddress[2],
  169. "personPortraitUrl": this.state.avatarUrlData,
  170. "lifePhotoUrl": lifePhotoUrl,
  171. "personalProfile": values.intro,
  172. "email": values.email,
  173. "fixedTelArea": this.state.fixTelArea,
  174. "fixedTel": this.state.fixTel,
  175. "fixedTelExtension": this.state.fixTelExtension,
  176. "qq": values.qq,
  177. "postalAddress": values.address,
  178. "postcode": values.postcode
  179. }
  180. }).done(function (data) {
  181. if (!data.error.length) {
  182. message.success('保存成功!');
  183. this.getData();
  184. } else {
  185. message.warning(data.error[0].message);
  186. }
  187. }.bind(this)).always(function () {
  188. this.props.spinState(false);
  189. }.bind(this));
  190. }
  191. });
  192. },
  193. avatarUrl(e) {
  194. this.state.avatarUrlData = e;
  195. },
  196. livePicFileList(e) {
  197. this.state.lifePhotoList = e;
  198. },
  199. componentWillMount() {
  200. this.getData();
  201. },
  202. fixTelAreaChange(e) {
  203. this.setState({
  204. fixTelArea: e.target.value
  205. });
  206. },
  207. fixTelChange(e) {
  208. this.setState({
  209. fixTel: e.target.value
  210. });
  211. },
  212. fixTelExtensionChange(e) {
  213. this.setState({
  214. fixTelExtension: e.target.value
  215. });
  216. },
  217. render() {
  218. const { getFieldDecorator } = this.props.form;
  219. const formItemLayout = {
  220. labelCol: { span: 3 },
  221. wrapperCol: { span: 12 },
  222. };
  223. const _me = this;
  224. return (
  225. <Form horizontal onSubmit={this.handleSubmit} className="person-form">
  226. <FormItem
  227. {...formItemLayout}
  228. label="社区昵称"
  229. >
  230. {getFieldDecorator('nickname', {
  231. initialValue: this.state.nickname || null
  232. })(
  233. <Input />
  234. )}
  235. </FormItem>
  236. <FormItem
  237. {...formItemLayout}
  238. label="居住地"
  239. >
  240. {getFieldDecorator('theAddress', {
  241. initialValue: this.state.theAddress || []
  242. })(
  243. <Cascader options={areaSelect()} />
  244. )}
  245. </FormItem>
  246. <FormItem
  247. {...formItemLayout}
  248. label="个人头像"
  249. labelCol={{ span: 3 }}
  250. wrapperCol={{ span: 21 }}
  251. >
  252. {getFieldDecorator('avatar')(
  253. <Avatar urlData={this.avatarUrl} name='personPortrait' picUrl={this.state.personPortraitUrl} />
  254. )}
  255. </FormItem>
  256. <div className="set-explain">
  257. <p>请上传清晰的个人身份证正反面原件照片/扫描件 (不能截图、图片内容要求完整 ),</p>
  258. <p>要求2M 以下的jpg、jpeg格式的图片。该资料不会公开展示,仅作为审核材料使用。</p>
  259. </div>
  260. <FormItem
  261. labelCol={{ span: 3 }}
  262. wrapperCol={{ span: 21 }}
  263. label="生活照"
  264. >
  265. {getFieldDecorator('livePic')(
  266. <PicturesWall fileList={this.livePicFileList} lifePhotoList={this.state.lifePhotoList} />
  267. )}
  268. </FormItem>
  269. <FormItem
  270. labelCol={{ span: 3 }}
  271. wrapperCol={{ span: 20 }}
  272. label="个人简介"
  273. >
  274. {getFieldDecorator('intro', {
  275. initialValue: this.state.intro || null
  276. })(
  277. <Input type="textarea"
  278. placeholder="全面介绍自己,让商友更深入了解您,内容长度不超过1000个字。"
  279. rows={6} />
  280. )}
  281. <p>还可输入{
  282. (() => {
  283. if (_me.props.form.getFieldValue('intro') && _me.props.form.getFieldValue('intro').length) {
  284. return 1000 - _me.props.form.getFieldValue('intro').length;
  285. } else {
  286. return 1000;
  287. }
  288. })()
  289. }字/1000</p>
  290. </FormItem>
  291. <FormItem
  292. {...formItemLayout}
  293. label="电子邮箱"
  294. hasFeedback
  295. >
  296. {getFieldDecorator('email', {
  297. rules: [{
  298. type: 'email', message: '请输入正确的邮箱地址!',
  299. }],
  300. initialValue: this.state.email || null
  301. })(
  302. <Input />
  303. )}
  304. </FormItem>
  305. <FormItem
  306. {...formItemLayout}
  307. label="固定电话"
  308. >
  309. {getFieldDecorator('telephone')(
  310. <p>
  311. <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} />
  312. </p>
  313. )}
  314. </FormItem>
  315. <FormItem
  316. {...formItemLayout}
  317. label="QQ"
  318. >
  319. {getFieldDecorator('qq', {
  320. initialValue: this.state.qq || null
  321. })(
  322. <Input />
  323. )}
  324. </FormItem>
  325. <FormItem
  326. {...formItemLayout}
  327. label="通讯地址"
  328. >
  329. {getFieldDecorator('address', {
  330. initialValue: this.state.address || null
  331. })(
  332. <Input />
  333. )}
  334. </FormItem>
  335. <FormItem
  336. {...formItemLayout}
  337. label="邮编"
  338. >
  339. {getFieldDecorator('postcode', {
  340. initialValue: this.state.postcode || null
  341. })(
  342. <Input />
  343. )}
  344. </FormItem>
  345. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  346. <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
  347. </FormItem>
  348. </Form >
  349. );
  350. },
  351. }));
  352. const Person = React.createClass({
  353. getInitialState() {
  354. return {
  355. loading: false
  356. };
  357. },
  358. spinChange(e) {
  359. this.setState({
  360. loading: e
  361. });
  362. },
  363. render() {
  364. return (
  365. <Spin spinning={this.state.loading} className='spin-box'>
  366. <div className="set-person">
  367. <div className="acc-detail">
  368. <p className="acc-title">个人资料</p>
  369. <PersonFrom spinState={this.spinChange} />
  370. </div>
  371. </div>
  372. </Spin>
  373. )
  374. }
  375. });
  376. export default Person;