person.jsx 14 KB

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