UserInfo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import React, {Component} from 'react'
  2. import 'css/user-info.css'
  3. import 'css/phones.css'
  4. import {Icon, Modal, Upload, Avatar} from "antd";
  5. import icon_userInfo_upload from "../../style/imgs/icon_userInfo_upload.png"
  6. import {API, _baseURL} from "../../configs/api.config";
  7. import {Toast} from 'antd-mobile'
  8. import {connect} from 'react-redux'
  9. import {fetchPost, fetchGet} from "../../utils/fetchRequest";
  10. import UploadEnclosure from "../../components/UploadEnclosure";
  11. import ImagesViewer from '../../components/imagesVIewer/index'
  12. import {isObjEmpty} from "../../utils/common";
  13. import {switchUser} from "../../redux/actions/userInfo";
  14. class UserInfo extends Component {
  15. //老师是1家长是2
  16. static defaultProps = {
  17. type: 1,
  18. userName: '',
  19. school: '',
  20. phone: '',
  21. id: '',
  22. }
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. previewVisible: false,
  27. previewImage: '',
  28. fileList: [],
  29. }
  30. }
  31. componentWillMount() {
  32. if (this.props.match.params.type) {
  33. this.type = this.props.match.params.type
  34. }
  35. document.title = '个人信息'
  36. }
  37. componentDidMount() {
  38. // this.updateUserInfo("");
  39. }
  40. componentWillUnmount() {
  41. Toast.hide()
  42. }
  43. render() {
  44. return <div className={'user-column'}>
  45. {this.showUserInfo()}
  46. <span className={'lineMax'}/>
  47. {this.showLocation()}
  48. <span className={'lineMax'}/>
  49. {this.showUserContact()}
  50. </div>
  51. }
  52. //显示顶部个人信息
  53. showUserInfo() {
  54. const {userInfo} = this.props
  55. let userName = ''
  56. if (this.type == 1) {
  57. userName = userInfo.teacher.teacherName
  58. } else if (this.type == 2) {
  59. userName = userInfo.parent.parentsName
  60. }
  61. return <div className='user-row'>
  62. {isObjEmpty(userInfo.userAvatar) ?
  63. <Avatar size={50} icon='user'/> :
  64. <img className='user-info-avatar' onClick={this.onAvatarClick}
  65. src={userInfo.userAvatar}/>}
  66. <div className="flex_row_center user-info-msg">
  67. <span>{userName}</span>
  68. </div>
  69. </div>
  70. }
  71. //显示个人位置信息
  72. showLocation() {
  73. const {userInfo} = this.props
  74. const sex = (this.type == 1 ? userInfo.teacher.teacherSex : userInfo.parent.student.stuSex)
  75. let schoolName = ''
  76. if (this.type == 1) {
  77. schoolName = userInfo.teacher.schoolName
  78. } else {
  79. schoolName = userInfo.parent.schoolName
  80. }
  81. return <div className={'user-column'}>
  82. {this.type == 1 ? '' : <div>
  83. <div className='user-info-item-top'>
  84. <text className='user-info-item-name'>学生姓名</text>
  85. <text className='user-info-item-phone'>{userInfo.parent.student.stuName}</text>
  86. </div>
  87. <div className='gray-line' style={{height: '1px'}}></div>
  88. </div>}
  89. <div className='user-info-item-top'>
  90. <text className='user-info-item-name'>所在学校</text>
  91. <text className='user-info-item-phone'>{schoolName}</text>
  92. </div>
  93. <div className='gray-line' style={{height: '1px'}}></div>
  94. {this.type == 1 ? '' : (<div>
  95. <div className='user-info-item-top'>
  96. <text className='user-info-item-name'>性别</text>
  97. <text className='user-info-item-phone'>{sex === 1 ? '男' : '女'}</text>
  98. </div>
  99. <div className='gray-line' style={{height: '1px'}}></div>
  100. </div>)}
  101. <div className='user-info-item-top'>
  102. <text className='user-info-item-name'>身份</text>
  103. <text className='user-info-item-phone'>{this.type == 1 ? '教师' : '家长'}</text>
  104. </div>
  105. </div>
  106. }
  107. //显示个人联系方式
  108. showUserContact() {
  109. const {previewVisible, fileList} = this.state;
  110. const {userInfo} = this.props
  111. let pictureUrls = []
  112. if (!isObjEmpty(userInfo.parent, userInfo.parent.student, userInfo.parent.student.stuPhoto)) {
  113. pictureUrls.push(_baseURL + userInfo.parent.student.stuPhoto)
  114. }
  115. let userPhone = ''
  116. if (this.type == 1) {
  117. userPhone = userInfo.teacher.userPhone
  118. } else if (this.type == 2) {
  119. userPhone = userInfo.parent.userPhone
  120. }
  121. return <div className={'user-column'}>
  122. <div className='user-info-item-top'>
  123. <div>
  124. <text className='user-info-item-name'>手机号码</text>
  125. <div className='user-info-item-phone' style={{marginTop: '8px'}}>{userPhone}</div>
  126. </div>
  127. <div className="item_flex_1 flex_row_right">
  128. <div className='gray-line'
  129. style={{height: '34px', background: '#CCCCCC', width: '1px'}}></div>
  130. <text className="margin_left_20" style={{color: "#3680ED"}}
  131. onClick={this.onChangePhone}>更换
  132. </text>
  133. </div>
  134. </div>
  135. <div className='gray-line' style={{height: '1px'}}></div>
  136. {(this.type == 1 || true) ? '' : <div>
  137. <div className='user-info-item-top'>
  138. <text className='user-info-item-name'>人脸照</text>
  139. <div className='gray-line'
  140. style={{height: '34px', background: '#CCCCCC', width: '1px'}}></div>
  141. <span className="fileinput-button margin_left_20" style={{color: "#3680ED"}}>
  142. 上传
  143. {/*<input type="file" accept="image/*" capture="camera" onChange={this.uploadChange}/>*/}
  144. <UploadEnclosure
  145. action={API.UPLOAD_FILE}
  146. fileList={fileList}
  147. count={1}
  148. multiple={false}
  149. handlePreview={this.enclosurePreview.bind(this)}
  150. beforeUpload={this.beforeUpload.bind(this)}
  151. handleChange={this.handleChange.bind(this)}
  152. />
  153. </span>
  154. </div>
  155. <div className='flex' style={{marginTop: '8px'}}>
  156. <img style={{marginLeft: "16px"}}
  157. src={pictureUrls.length === 0 ? icon_userInfo_upload :
  158. pictureUrls[0]}
  159. width={86}
  160. height={100}
  161. onClick={this.handlePreview}/>
  162. <div style={{marginLeft: '14px'}}>
  163. <div className="user-info-photo-text">• 请按照证件照的样式拍摄正面</div>
  164. <div className="user-info-photo-text">• 请保证光线充足,没有遮挡物</div>
  165. <div className="user-info-photo-text">• 请取下您的眼镜帽子保持面部曝光率</div>
  166. </div>
  167. </div>
  168. {previewVisible ?
  169. <ImagesViewer onClose={this.handleCancel} urls={pictureUrls}
  170. index={0}
  171. needPoint={false}/> : ""}
  172. </div>}
  173. </div>
  174. }
  175. onChangePhone = () => {
  176. this.props.history.push('/changephonenumber')
  177. }
  178. handlePreview = () => {
  179. this.setState({
  180. previewVisible: true,
  181. });
  182. }
  183. handleCancel = () => this.setState({previewVisible: false})
  184. beforeUpload = (file, fileList) => {
  185. Toast.loading('人脸照上传中...', 0)
  186. }
  187. handleChange = fileList => {
  188. if (fileList) {
  189. fileList.forEach((value, index) => {
  190. value.url = value.response ? (_baseURL + value.response.data) : value.url
  191. value.picUrl = value.response ? value.response.data : value.picUrl
  192. })
  193. this.setState({fileList})
  194. if (fileList[0].status === 'error') {
  195. Toast.fail('人脸照上传失败,请重试!')
  196. this.setState({
  197. fileList: []
  198. })
  199. } else if (fileList[0].status === 'done') {
  200. this.updateUserInfo(fileList[0].response.data)
  201. } else if (fileList[0].status === 'uploading') {
  202. } else {
  203. Toast.hide()
  204. }
  205. }
  206. }
  207. enclosurePreview = () => {
  208. }
  209. updateUserInfo = (userPhoto) => {
  210. fetchPost(API.UPDATE_STU_PHOTO, {
  211. stuId: this.props.userInfo.parent.student.stuId,
  212. stuPhoto: userPhoto
  213. }).then((response) => {
  214. Toast.success('人脸照上传成功!')
  215. this.setState({
  216. fileList: []
  217. });
  218. switchUser({
  219. parent: {
  220. student: {
  221. ...this.props.userInfo.parent.student,
  222. stuPhoto: userPhoto
  223. }
  224. }
  225. })()
  226. }).catch((error) => {
  227. Toast.hide();
  228. if (typeof error === 'string') {
  229. Toast.fail(error, 2)
  230. } else {
  231. Toast.fail('人脸照上传异常', 2)
  232. }
  233. })
  234. }
  235. //头像点击事件
  236. onAvatarClick = (event) => {
  237. //TODO 点击头像
  238. }
  239. uploadChange = (e) => {
  240. this.uploadFile(e.target.files[0])
  241. }
  242. uploadFile = (file) => {
  243. const formData = new FormData();
  244. formData.append('file', file);
  245. Toast.loading("")
  246. fetch(API.UPLOAD_FILE, {
  247. method: "POST",
  248. body: formData,
  249. mode: 'cors',
  250. }).then(function (response) {
  251. let result = response.json();
  252. if (response.status === 200) {
  253. console.log("text:", result);
  254. return result;
  255. } else {
  256. Toast.success("上传失败!")
  257. }
  258. }).then(result => {
  259. if (result.success) {
  260. Toast.success("上传成功!");
  261. let imageUrl = result.data;
  262. this.state.imageUrl = imageUrl;
  263. this.setState({
  264. imageUrl: imageUrl
  265. });
  266. this.updateUserInfo(imageUrl)
  267. }
  268. }).catch(function (ex) {
  269. Toast.fail("上传失败!")
  270. })
  271. }
  272. }
  273. let mapStateToProps = (state) => ({
  274. userInfo: {...state.redUserInfo},
  275. })
  276. let mapDispatchToProps = (dispatch) => ({})
  277. export default connect(mapStateToProps, mapDispatchToProps)(UserInfo)