AccountBind.jsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * Created by RaoMeng on 2018/11/6
  3. * Desc: 账号绑定
  4. */
  5. import React, {Component} from 'react'
  6. import 'css/account-bind.css'
  7. import {Avatar, Input, Icon, Button} from 'antd'
  8. import {fetchGet, fetchPost} from "../../utils/fetchRequest";
  9. import {API} from "../../configs/api.config";
  10. import {getIntValue, getStrValue, isObjEmpty} from "../../utils/common";
  11. import {Toast} from 'antd-mobile'
  12. import {regExpConfig} from "../../configs/regexp.config";
  13. import {connect} from 'react-redux'
  14. import {switchUser} from "../../redux/actions/userInfo";
  15. let mType = 'parents'
  16. let mSeconds = 0
  17. class AccountBind extends Component {
  18. componentWillMount() {
  19. this.bodyHeight = document.documentElement.clientHeight
  20. if (this.props.match.params.type) {
  21. mType = this.props.match.params.type
  22. }
  23. if (mType == 'parents') {
  24. document.title = '家长端绑定'
  25. } else if (mType == 'teacher') {
  26. document.title = '教职工端绑定'
  27. }
  28. }
  29. constructor() {
  30. super()
  31. this.state = {
  32. account: '',
  33. phone: '',
  34. code: '',
  35. obtainText: '获取验证码',
  36. }
  37. }
  38. render() {
  39. const {account, phone, code, obtainText} = this.state
  40. const idClear = account ?
  41. <Icon type="close-circle" onClick={this.accountEmpty} style={{color: 'white'}}/>
  42. : null;
  43. const phoneClear = phone ?
  44. <Icon type="close-circle" onClick={this.phoneEmpty} style={{color: 'white'}}/>
  45. : null;
  46. const codeClear = code ?
  47. <Icon type="close-circle" onClick={this.codeEmpty} style={{color: 'white'}}/>
  48. : null;
  49. const idIcon = <img src={require('imgs/ic_account_input.png')} className='inputIcon1'/>
  50. const phoneIcon = <img src={require('imgs/ic_phone_input.png')} className='inputIcon2'/>
  51. const codeIcon = <img src={require('imgs/ic_code_input.png')} className='inputIcon3'/>
  52. const obtainCode = <div onClick={this.obtainCode}>{obtainText}</div>
  53. return (
  54. <div className='bindParent' style={{height: this.bodyHeight + 'px'}}>
  55. <div className='bindHeadLayout'>
  56. {isObjEmpty(this.props.userInfo.userAvatar) ?
  57. <Avatar icon='user' size={65}/> :
  58. <Avatar src={this.props.userInfo.userAvatar} size={65}/>}
  59. </div>
  60. {/*<Input placeholder={mType == 'parents' ? '学号' : '工号'}
  61. prefix={idIcon} suffix={idClear}
  62. ref={input => this.accountInput = input} onChange={this.accountChange}
  63. value={account} type='number' onKeyPress={this.phoneKeyPress}/>*/}
  64. <Input placeholder='手机号' prefix={phoneIcon} suffix={phoneClear}
  65. ref={input => this.phoneInput = input} onChange={this.phoneChange}
  66. value={phone} type='number' onKeyPress={this.phoneKeyPress}
  67. addonAfter={obtainCode}/>
  68. <Input placeholder='短信验证码' prefix={codeIcon} suffix={codeClear}
  69. ref={input => this.codeInput = input} onChange={this.codeChange}
  70. value={code} type='number' onKeyPress={this.phoneKeyPress}/>
  71. <Button type="primary" block className='commonButton' style={{marginTop: '35px',borderRadius: '30px',}}
  72. onClick={this.bindEvent}>绑定</Button>
  73. </div>
  74. );
  75. }
  76. phoneKeyPress = (event) => {
  77. const invalidChars = ['-', '+', 'e', '.', 'E']
  78. if (invalidChars.indexOf(event.key) !== -1) {
  79. event.preventDefault()
  80. }
  81. }
  82. accountChange = (e) => {
  83. this.setState({
  84. account: e.target.value
  85. })
  86. }
  87. phoneChange = (e) => {
  88. this.setState({
  89. phone: e.target.value
  90. })
  91. }
  92. codeChange = (e) => {
  93. this.setState({
  94. code: e.target.value
  95. })
  96. }
  97. accountEmpty = () => {
  98. this.accountInput.focus()
  99. this.setState({
  100. account: ''
  101. })
  102. }
  103. phoneEmpty = () => {
  104. this.phoneInput.focus()
  105. this.setState({
  106. phone: ''
  107. })
  108. }
  109. codeEmpty = () => {
  110. this.codeInput.focus()
  111. this.setState({
  112. code: ''
  113. })
  114. }
  115. obtainCode = () => {
  116. if (mSeconds !== 0) {
  117. return
  118. }
  119. const {phone} = this.state
  120. if (isObjEmpty(phone)) {
  121. Toast.info('请输入手机号码!', 2, null, false)
  122. return
  123. }
  124. if (!regExpConfig.mobile.test(phone)) {
  125. Toast.fail('请输入正确的手机号码!', 2, null, false)
  126. return
  127. }
  128. Toast.loading('验证码获取中...', 0)
  129. this.setState({
  130. obtainText: '获取中'
  131. })
  132. fetchGet(API.SEND_CODE, {
  133. userPhone: phone
  134. }).then(response => {
  135. Toast.hide()
  136. if (response.success) {
  137. Toast.success('验证码已发送,请注意查收', 2)
  138. mSeconds = 60
  139. this.setState({
  140. obtainText: '剩余' + mSeconds + '秒'
  141. })
  142. this.countdown()
  143. } else {
  144. Toast.fail(response.data.message, 2)
  145. }
  146. }).catch(error => {
  147. Toast.hide()
  148. this.setState({
  149. obtainText: '获取验证码'
  150. })
  151. Toast.fail(error || '获取验证码失败', 2)
  152. })
  153. }
  154. countdown = () => {
  155. setTimeout(() => {
  156. if (mSeconds > 0) {
  157. mSeconds--
  158. this.setState({
  159. obtainText: '剩余' + mSeconds + '秒'
  160. })
  161. this.countdown()
  162. } else {
  163. this.setState({
  164. obtainText: '获取验证码'
  165. })
  166. }
  167. }, 1000)
  168. }
  169. bindEvent = () => {
  170. Toast.loading('信息绑定中...')
  171. const {phone, code} = this.state
  172. if (isObjEmpty(phone, code)) {
  173. Toast.fail('请完善所有输入项!')
  174. return
  175. }
  176. fetchPost(API.BIND_OPENID, {
  177. userPhone: phone,
  178. code: code,
  179. openid: this.props.userInfo.userOpenid,
  180. headimgurl: this.props.userInfo.userAvatar,
  181. }).then(response => {
  182. Toast.hide()
  183. if (mType == 'parents') {
  184. switchUser({
  185. role: 1,
  186. })()
  187. } else if (mType == 'teacher') {
  188. switchUser({
  189. role: 2,
  190. })()
  191. }
  192. if (response && response.data) {
  193. if (response.data.teacherDOS) {
  194. const teacherDOS = response.data.teacherDOS[0]
  195. switchUser({
  196. userRole: 2,
  197. teacher: {
  198. teacherId: getIntValue(teacherDOS, 'teacherId'),
  199. userId: getIntValue(teacherDOS, 'userId'),
  200. openid: getStrValue(teacherDOS, 'openid'),
  201. userPhone: getStrValue(teacherDOS, 'userPhone'),
  202. schoolId: getStrValue(teacherDOS, 'schoolId'),
  203. schoolName: getStrValue(teacherDOS, 'schoolName'),
  204. teacherNumber: getStrValue(teacherDOS, 'teacherNumber'),
  205. teacherName: getStrValue(teacherDOS, 'teacherName'),
  206. teacherPhoto: getStrValue(teacherDOS, 'teacherPhoto'),
  207. teacherAddress: getStrValue(teacherDOS, 'teacherAddress'),
  208. teacherEntry: getStrValue(teacherDOS, 'teacherEntry'),
  209. teacherBirthday: getStrValue(teacherDOS, 'teacherBirthday'),
  210. teacherSex: getStrValue(teacherDOS, 'teacherSex'),//1:男,2:女
  211. }
  212. })()
  213. } else {
  214. switchUser({
  215. teacher: ''
  216. })()
  217. }
  218. if (response.data.parentsDOS) {
  219. const parentsDOS = response.data.parentsDOS[0]
  220. switchUser({
  221. userRole: 1,
  222. parent: {
  223. parentId: getIntValue(parentsDOS, 'parentId'),
  224. userId: getIntValue(parentsDOS, 'userId'),
  225. userPhone: getStrValue(parentsDOS, 'userPhone'),
  226. openid: getStrValue(parentsDOS, 'openid'),
  227. schoolId: getIntValue(parentsDOS, 'schoolId'),
  228. schoolName: getStrValue(parentsDOS, 'schoolName'),
  229. parentsName: getStrValue(parentsDOS, 'parentsName'),
  230. parentsBirthday: getStrValue(parentsDOS, 'parentsBirthday'),
  231. parentsSex: getIntValue(parentsDOS, 'parentsSex'),
  232. parentsPhoto: getStrValue(parentsDOS, 'parentsPhoto'),
  233. parentsAddress: getStrValue(parentsDOS, 'parentsAddress'),
  234. }
  235. })()
  236. } else {
  237. switchUser({
  238. parent: ''
  239. })()
  240. }
  241. }
  242. this.props.history.push('/homePage')
  243. }).catch(error => {
  244. Toast.hide()
  245. if (typeof error === 'string') {
  246. Toast.fail(error, 2)
  247. } else {
  248. Toast.fail('绑定失败')
  249. }
  250. })
  251. }
  252. }
  253. let mapStateToProps = (state) => ({
  254. userInfo: {...state.redUserInfo}
  255. })
  256. let mapDispatchToProps = (dispatch) => ({})
  257. export default connect(mapStateToProps, mapDispatchToProps)(AccountBind)