person.jsx 14 KB

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