BindMenu.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 {Redirect} from 'react-router-dom'
  8. import {Avatar, Input, Icon, Button} from 'antd'
  9. import {Toast} from 'antd-mobile'
  10. import {fetchGet} from "../../utils/fetchRequest";
  11. import {API} from "../../configs/api.config";
  12. import {switchUser} from 'action/userInfo'
  13. import {getIntValue, getStrValue, isObjEmpty} from "../../utils/common";
  14. import {clearUserInfo} from "../../redux/actions/userInfo";
  15. import {connect} from "react-redux";
  16. class BindMenu extends Component {
  17. constructor() {
  18. super()
  19. this.state = {
  20. bindStatus: 0,
  21. errorMsg: ''
  22. }
  23. }
  24. componentWillMount() {
  25. }
  26. componentDidMount() {
  27. document.title = '账号绑定'
  28. this.paramType = this.props.match.params.type
  29. this.paramId = this.props.match.params.openid
  30. if (this.paramType === 'app') {
  31. if (isObjEmpty(this.paramId)) {
  32. this.setState({
  33. errorMsg: '公众号信息获取失败'
  34. })
  35. } else {
  36. switchUser({
  37. appId: this.paramId,
  38. })()
  39. this.setState({
  40. errorMsg: ''
  41. })
  42. window.location.href =
  43. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  44. this.paramId + '&redirect_uri=https%3a%2f%2fschool-api.ydyhz.com%2fapi%2fschool%2fwxSchool%2fuser%2fuserLogin&response_type=code&scope=snsapi_userinfo&state=' +
  45. this.paramId + '&connect_redirect=1#wechat_redirect'
  46. }
  47. } else if (this.paramType === 'open') {
  48. this.setState({
  49. errorMsg: '获取绑定信息中...'
  50. })
  51. this.openid = this.paramId
  52. this.wxAvatar = this.props.match.params.wxAvatar
  53. //清除用户信息
  54. clearUserInfo()()
  55. switchUser({
  56. userAvatar: decodeURIComponent(this.wxAvatar),
  57. })()
  58. this.obtainBindStatus()
  59. }
  60. }
  61. render() {
  62. const {bindStatus} = this.state
  63. if (bindStatus === 0) {
  64. return this.getLoadingLayout()
  65. } else if (bindStatus === 1) {
  66. return this.getMenuLayout()
  67. } else if (bindStatus === 2) {
  68. return this.getRedirectMain()
  69. }
  70. }
  71. obtainBindStatus = () => {
  72. fetchGet(API.USER_ISBINDING, {
  73. openid: this.openid,
  74. schoolId: 1,//学校id暂时写死,后续需要添加选择学校的步骤
  75. headimgurl: decodeURIComponent(this.wxAvatar),
  76. }).then(response => {
  77. if (response.data) {
  78. switchUser({
  79. userOpenid: this.openid,
  80. token: response.data.token ? response.data.token.token : ''
  81. })()
  82. if (!isObjEmpty(response.data.teacherDOS)) {
  83. const teacherDOS = response.data.teacherDOS[0]
  84. switchUser({
  85. userRole: 2,
  86. teacher: {
  87. teacherId: getIntValue(teacherDOS, 'teacherId'),
  88. userId: getIntValue(teacherDOS, 'userId'),
  89. openid: getStrValue(teacherDOS, 'openid'),
  90. userPhone: getStrValue(teacherDOS, 'userPhone'),
  91. schoolId: getStrValue(teacherDOS, 'schoolId'),
  92. schoolName: getStrValue(teacherDOS, 'schoolName'),
  93. teacherNumber: getStrValue(teacherDOS, 'teacherNumber'),
  94. teacherName: getStrValue(teacherDOS, 'teacherName'),
  95. teacherPhoto: getStrValue(teacherDOS, 'teacherPhoto'),
  96. teacherAddress: getStrValue(teacherDOS, 'teacherAddress'),
  97. teacherEntry: getStrValue(teacherDOS, 'teacherEntry'),
  98. teacherBirthday: getStrValue(teacherDOS, 'teacherBirthday'),
  99. teacherSex: getStrValue(teacherDOS, 'teacherSex'),//1:男,2:女
  100. }
  101. })()
  102. } else {
  103. switchUser({
  104. teacher: ''
  105. })()
  106. }
  107. if (!isObjEmpty(response.data.parentsDOS)) {
  108. const parentsDOS = response.data.parentsDOS[0]
  109. switchUser({
  110. userRole: 1,
  111. parent: {
  112. parentId: getIntValue(parentsDOS, 'parentId'),
  113. userId: getIntValue(parentsDOS, 'userId'),
  114. userPhone: getStrValue(parentsDOS, 'userPhone'),
  115. openid: getStrValue(parentsDOS, 'openid'),
  116. schoolId: getIntValue(parentsDOS, 'schoolId'),
  117. schoolName: getStrValue(parentsDOS, 'schoolName'),
  118. parentsName: getStrValue(parentsDOS, 'parentsName'),
  119. parentsBirthday: getStrValue(parentsDOS, 'parentsBirthday'),
  120. parentsSex: getIntValue(parentsDOS, 'parentsSex'),
  121. parentsPhoto: getStrValue(parentsDOS, 'parentsPhoto'),
  122. parentsAddress: getStrValue(parentsDOS, 'parentsAddress'),
  123. }
  124. })()
  125. } else {
  126. switchUser({
  127. parent: ''
  128. })()
  129. }
  130. this.setState({
  131. bindStatus: 2
  132. })
  133. this.props.history.push('/homePage')
  134. } else {
  135. this.setState({
  136. bindStatus: 1
  137. })
  138. }
  139. }).catch(error => {
  140. if (typeof error === 'string') {
  141. this.setState({
  142. errorMsg: error
  143. })
  144. } else {
  145. this.setState({
  146. errorMsg: '数据请求异常'
  147. })
  148. }
  149. })
  150. }
  151. getRedirectMain = () => {
  152. return (
  153. <div>
  154. {/*<Redirect to='/homePage'></Redirect>*/}
  155. </div>
  156. )
  157. }
  158. getLoadingLayout = () => {
  159. return (
  160. <div className='bindParent' style={{justifyContent: 'center', backgroundImage: 'none'}}>
  161. <Icon type="loading" spin style={{fontSize: '50px', color: '#3db1af'}}/>
  162. <span style={{marginTop: '14px', color: '#666'}}>{this.state.errorMsg}</span>
  163. </div>
  164. )
  165. }
  166. getMenuLayout = () => {
  167. return (
  168. <div className='bindParent'>
  169. <div className='bindTitleLayout'>
  170. <Avatar size={50} src={require('imgs/ic_bind_menu_head.png')}/>
  171. <span className='bindTitleText'>您好<br/>请选择您的身份进行绑定</span>
  172. </div>
  173. <Button type="primary" block
  174. style={{
  175. marginTop: '20px', letterSpacing: '10px',
  176. borderRadius: '9px', fontSize: '14px', borderRadius: '30px',
  177. }}
  178. onClick={this.parentBind}>我是家长</Button>
  179. <Button type="primary" block
  180. style={{
  181. marginTop: '20px', letterSpacing: '10px',
  182. background: '#05DC40', borderRadius: '30px',
  183. fontSize: '14px', border: 'none'
  184. }}
  185. onClick={this.teacherBind}>我是老师</Button>
  186. </div>
  187. )
  188. }
  189. parentBind = () => {
  190. this.props.history.push('/accountBind/parents')
  191. }
  192. teacherBind = () => {
  193. this.props.history.push('/accountBind/teacher')
  194. }
  195. }
  196. let mapStateToProps = (state) => ({
  197. userInfo: {...state.redUserInfo}
  198. })
  199. let mapDispatchToProps = (dispatch) => ({})
  200. export default connect(mapStateToProps, mapDispatchToProps)(BindMenu)