PhonesSearch.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * Created by RaoMeng on 2019/1/15
  3. * Desc: 通讯录搜索页面
  4. */
  5. import React, {Component} from 'react'
  6. import PhonesBean from 'model/PhonesBean'
  7. import {getStrValue, isObjEmpty} from "../../utils/common";
  8. import RefreshLayout from "../../components/RefreshLayout";
  9. import PhonesItem from "../../components/PhonesItem";
  10. import {Toast, SearchBar} from "antd-mobile";
  11. import {List, Skeleton} from 'antd'
  12. import {_baseURL, API} from "../../configs/api.config";
  13. import {fetchGet, fetchPost} from "../../utils/fetchRequest";
  14. import {connect} from 'react-redux'
  15. const mPageSize = 10
  16. let mPageIndex = 0
  17. let mSearchKey = ''
  18. class PhonesSearch extends Component {
  19. constructor() {
  20. super()
  21. this.state = {
  22. isRefreshing: false,
  23. phonesList: [],
  24. isLoading: false,
  25. searchKey: '',
  26. searchText: '',
  27. }
  28. }
  29. componentWillMount() {
  30. this.mType = this.props.match.params.type
  31. if (this.mType === 'teacher') {
  32. this.title = '搜索老师'
  33. } else if (this.mType === 'parent') {
  34. this.title = '搜索学生'
  35. }
  36. }
  37. componentDidMount() {
  38. this.searchInput.focus()
  39. document.title = this.title
  40. }
  41. componentWillUnmount() {
  42. }
  43. render() {
  44. const {phonesList, isRefreshing, isLoading, searchText} = this.state
  45. return (
  46. <div className='phone-select-root' style={{height: '100%'}}>
  47. <SearchBar placeholder={this.title} maxLength={20}
  48. ref={ref => this.searchInput = ref}
  49. value={searchText} onSubmit={this.searchSubmit}
  50. onChange={this.searchChange} onClear={this.searchClear}
  51. onCancel={this.searchClear}/>
  52. <RefreshLayout
  53. refreshing={isRefreshing}
  54. onRefresh={this.loadPhones}>
  55. <Skeleton loading={isLoading} active paragraph={{rows: 3}}>
  56. <List className='phones-list-layout'
  57. dataSource={phonesList}
  58. renderItem={phonesBean => (
  59. <List.Item>
  60. <PhonesItem phonesBean={phonesBean}/>
  61. </List.Item>
  62. )}/>
  63. </Skeleton>
  64. </RefreshLayout>
  65. </div>
  66. )
  67. }
  68. searchChange = (value) => {
  69. mSearchKey = value
  70. this.setState({
  71. searchText: value
  72. })
  73. }
  74. searchClear = (e) => {
  75. mPageIndex = 0
  76. this.setState({
  77. isLoading: true,
  78. isRefreshing: false,
  79. searchText: '',
  80. searchKey: ''
  81. }, () => {
  82. this.loadPhones()
  83. })
  84. }
  85. searchSubmit = (value) => {
  86. mPageIndex = 0
  87. this.setState({
  88. isLoading: true,
  89. isRefreshing: false,
  90. searchKey: value
  91. }, () => {
  92. this.loadPhones()
  93. })
  94. }
  95. loadPhones = () => {
  96. try {
  97. this.setState({
  98. isRefreshing: true
  99. })
  100. } catch (e) {
  101. }
  102. if (this.mType == 'parent') {
  103. this.url = API.GET_PARENT_PHONES_BYTEACHER
  104. this.params = {
  105. teacherId: this.props.userInfo.user.userFunId
  106. }
  107. this.getPhones()
  108. } else if (this.mType == 'teacher') {
  109. this.url = API.TEACHER_PHONES_LIST
  110. this.params = {
  111. teacherId: this.props.userInfo.user.userFunId
  112. }
  113. this.getPhones()
  114. } else {
  115. Toast.hide()
  116. this.setState({
  117. isRefreshing: false
  118. })
  119. }
  120. }
  121. getPhones = () => {
  122. mPageIndex++
  123. console.log(mPageIndex)
  124. const {phonesList, searchKey} = this.state
  125. if (mPageIndex === 1) {
  126. phonesList.length = 0
  127. }
  128. fetchPost(this.url, {
  129. pageIndex: mPageIndex,
  130. pageSize: mPageSize,
  131. selectKey: searchKey,
  132. ...this.params
  133. }).then(response => {
  134. Toast.hide();
  135. if (response && response.data) {
  136. if (this.mType == 'teacher') {
  137. response.data.forEach((item, index) => {
  138. let phoneBean = new PhonesBean()
  139. phoneBean.icon = require('imgs/ic_head' + (index % 15 + 1) + '.png')
  140. phoneBean.icon = _baseURL + getStrValue(item, 'teacherPhoto')
  141. phoneBean.name = getStrValue(item, 'teacherName')
  142. phoneBean.phone = [getStrValue(item, 'userPhone')]
  143. // phoneBean.claName = getStrValue(item, 'schName')
  144. phoneBean.children = ['']
  145. phonesList.push(phoneBean)
  146. })
  147. } else if (this.mType == 'parent') {
  148. let studentList = []
  149. response.data.forEach((item, index) => {
  150. if (item.studentDOS) {
  151. studentList = studentList.concat(item.studentDOS)
  152. }
  153. })
  154. if (!isObjEmpty(studentList)) {
  155. studentList.forEach((item, index) => {
  156. let phoneBean = new PhonesBean()
  157. let phones = []
  158. phoneBean.icon = require('imgs/ic_head' + (index % 15 + 1) + '.png')
  159. phoneBean.icon = _baseURL + getStrValue(item, 'stuPhoto')
  160. phoneBean.name = getStrValue(item, 'stuName')
  161. phoneBean.claName = this.title
  162. if (item.parentsDOS && item.parentsDOS.length > 0) {
  163. item.parentsDOS.forEach((ite, ind) => {
  164. phoneBean.children.push(getStrValue(ite, 'parentsName'))
  165. phones.push(getStrValue(ite, 'userPhone'))
  166. })
  167. }
  168. // if (phones.length > 0) {
  169. phoneBean.phone = phones
  170. // }
  171. phonesList.push(phoneBean)
  172. })
  173. }
  174. }
  175. } else {
  176. if (mPageIndex > 1) {
  177. mPageIndex--
  178. }
  179. }
  180. this.setState({
  181. phonesList,
  182. isLoading: false,
  183. isRefreshing: false,
  184. })
  185. }).catch(error => {
  186. Toast.hide()
  187. if (mPageIndex > 1) {
  188. mPageIndex--
  189. }
  190. if (typeof error === 'string') {
  191. Toast.fail(error, 2)
  192. } else {
  193. Toast.fail('请求异常')
  194. }
  195. this.setState({
  196. isLoading: false,
  197. isRefreshing: false,
  198. })
  199. })
  200. }
  201. }
  202. let mapStateToProps = (state) => ({
  203. userInfo: {...state.redUserInfo},
  204. })
  205. let mapDispatchToProps = (dispatch) => ({})
  206. export default connect(mapStateToProps, mapDispatchToProps)(PhonesSearch)