searchResult.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * 搜索结果页面
  3. */
  4. import React, { Component } from "react";
  5. import {
  6. Platform,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. ImageBackground,
  13. Button,
  14. StatusBar,
  15. Modal,
  16. TouchableHighlight,
  17. DeviceEventEmitter,
  18. FlatList,
  19. TextInput,
  20. ScrollView,
  21. Animated
  22. } from "react-native";
  23. import BasePage from "./BasePage";
  24. import Dimensions from './utils/dimensions';
  25. import ShopBox from "./components/ShopBox";
  26. import TopicTitle from './components/TopicTitle';
  27. import ScrollRow from './components/ScrollRow';
  28. import CourseTitle from './components/CourseTitle';
  29. import search from './services/search'
  30. export default class SearchResult extends BasePage {
  31. // componentDidMount() {
  32. // const { searchText } = this.props.navigation.state.params;
  33. // console.log('searchText',searchText)
  34. // this.setState({
  35. // text: searchText
  36. // })
  37. // this.search(searchText)
  38. // // courseDetails.getCourseDetails(courseId).then(res => {
  39. // // console.log('列表',res.data)
  40. // // const courseList = res.data.course;
  41. // // const wareList = res.data.wareList;
  42. // // this.setState({
  43. // // courseList,
  44. // // wareList,
  45. // // uri: wareList[0].playUrl
  46. // // })
  47. // // }).catch(err => {
  48. // // console.log(err)
  49. // // });
  50. // }
  51. state = {
  52. text: '',
  53. list: [],
  54. ifCourseMenuShow: false,
  55. currentCourseSwitch: { title: '课程', param: 'course' },
  56. switchCourseList: [
  57. // { title: '幼儿园', param: 'KINDERGARTEN' },
  58. { title: '课程', param: 'course' },
  59. { title: '课件', param: 'ware' }
  60. ],
  61. fadeCourseAnim: new Animated.Value(0),
  62. };
  63. search (text) {
  64. if (!text) {
  65. alert('请输入关键字搜索');
  66. return false;
  67. }
  68. const param = this.state.currentCourseSwitch.param;
  69. if (param == 'course') {
  70. search.getSearchCourseList(text).then( res => {
  71. console.log('搜索', res.data)
  72. const list = res.data.list;
  73. if(list.length == 0) {
  74. alert('请换个关键字搜索')
  75. return false;
  76. }
  77. this.setState({
  78. list
  79. })
  80. }).catch( err => {
  81. console.log(err)
  82. })
  83. } else {
  84. search.getSearchWareList(text).then( res => {
  85. console.log('搜索', res.data)
  86. const list = res.data.list;
  87. if(list.length == 0) {
  88. alert('请换个关键字搜索')
  89. return false;
  90. }
  91. this.setState({
  92. list
  93. })
  94. }).catch( err => {
  95. console.log(err)
  96. })
  97. }
  98. }
  99. searchBox = () => {
  100. return (
  101. <View style={styles.searchBox}>
  102. <TextInput
  103. style={styles.searchInput}
  104. onChangeText={(text) => this.setState({ text })}
  105. value={this.state.text}
  106. />
  107. <TouchableOpacity onPress={this.courseCallOutHandler} style={{position: 'absolute', left: 14, top: 10,width: '30%',height: 40}}>
  108. <View style={styles.rightBtn}>
  109. <Image source={require('./images/common/switch.png')} style={styles.rightBtnIcon} />
  110. <Text style={styles.rightBtnText}>{this.state.currentCourseSwitch.title}</Text>
  111. <View style={{ width:2, height:19, backgroundColor: '#63AEFF'}}></View>
  112. </View>
  113. </TouchableOpacity>
  114. <TouchableOpacity onPress={() => this.search(this.state.text)} style={{position: 'absolute', right: 20, top: 15}}>
  115. <Image style={styles.search} source={require('./images/common/search.png')}></Image>
  116. </TouchableOpacity>
  117. </View>
  118. )
  119. }
  120. searchHead = () => {
  121. return (
  122. this.state.list.length == 0 ?
  123. null:
  124. <View style={styles.head}>
  125. <Text style={{ fontSize: 16 }}>以下课程包含:</Text>
  126. <Text style={{ color: '#63AEFF', fontSize: 16 }}>{this.state.text}</Text>
  127. </View>
  128. )
  129. }
  130. renderItem = (item) =>{
  131. const param = this.state.currentCourseSwitch.param;
  132. return (
  133. param == 'course' ?
  134. <View style={styles.course} key={item.id}>
  135. <View style={styles.img}>
  136. <Image source={{
  137. uri: item.iconImg
  138. }} style={styles.iconImg} />
  139. </View>
  140. <Text>{item.title}</Text>
  141. </View>
  142. :
  143. <View style={[ styles.padding, styles.title, styles.border ]} key={item.id}>
  144. <Text style={styles.courseFont}>{item.title}</Text>
  145. <Text style={styles.courseFont}>></Text>
  146. </View>
  147. )
  148. {/* <View style={styles.ware}>
  149. </View> */}
  150. }
  151. // 点击选择
  152. courseCallOutHandler = () => {
  153. this.setState(
  154. {
  155. ifCourseMenuShow: !this.state.ifCourseMenuShow
  156. },
  157. () => {
  158. Animated.timing(this.state.fadeCourseAnim, {
  159. toValue: this.state.ifCourseMenuShow ? 1 : 0,
  160. duration: 300
  161. }).start();
  162. }
  163. );
  164. };
  165. // 如果切换课程在显示状态,那么在它失去焦点时将它隐藏
  166. hideCourseMenu = () => {
  167. if (this.state.ifCourseMenuShow) {
  168. this.setState({
  169. ifCourseMenuShow: false
  170. });
  171. }
  172. };
  173. //渲染按钮
  174. renderCourseBtn = (item, index) => {
  175. return (
  176. <TouchableOpacity
  177. onPress={() => this.switchCourseGrade(index)}
  178. key={index}
  179. style={styles.gradeItemMid}
  180. >
  181. <Text style={styles.gradeText}>{item.title}</Text>
  182. </TouchableOpacity>
  183. );
  184. };
  185. // 点击按钮
  186. switchCourseGrade = (index) => {
  187. const obj = this.state.switchCourseList[index];
  188. this.setState({
  189. currentCourseSwitch: obj
  190. });
  191. this.hideCourseMenu();
  192. // if (obj.param === this.state.currentCourseSwitch.param) return
  193. //这里更新个人信息学龄选项
  194. // var bool = this.updateUserInfo(obj.param);
  195. // this.updateRender(obj.param);
  196. };
  197. render() {
  198. return (
  199. <View style={{backgroundColor: '#f6f7f8'}}>
  200. <View style={{
  201. height: 30,
  202. backgroundColor: 'white'
  203. }}>
  204. <CourseTitle
  205. width={Dimensions.width}
  206. title="搜索"
  207. lefttype={1}
  208. textcolor={'#231F20'}
  209. backPress={() => this.goBack()}
  210. // backPress={() => alert("左侧按钮")}
  211. />
  212. </View>
  213. {
  214. this.searchBox()
  215. }
  216. {
  217. this.searchHead()
  218. }
  219. <ScrollView style={{paddingBottom: 100, backgroundColor: '#fff'}}>
  220. <View style={ this.state.currentCourseSwitch.param == 'coerse' ? styles.list : null}>
  221. {
  222. this.state.list.map(item =>
  223. this.renderItem(item)
  224. )
  225. }
  226. </View>
  227. </ScrollView>
  228. {/* <FlatList
  229. style={styles.list}
  230. data={this.state.list}
  231. ListHeaderComponent={() => this.searchHead()}
  232. renderItem={({item}) => this.renderItem(item)}
  233. /> */}
  234. {this.state.ifCourseMenuShow ? (
  235. <Animated.View
  236. style={{
  237. ...styles.callUpGrade,
  238. opacity: this.state.fadeCourseAnim
  239. }}
  240. >
  241. {this.state.switchCourseList.map((item, index) => this.renderCourseBtn(item, index))}
  242. </Animated.View>
  243. ) : null}
  244. </View>
  245. )
  246. }
  247. }
  248. const styles = StyleSheet.create({
  249. searchBox: {
  250. width: '100%',
  251. height: 60,
  252. paddingLeft: 14,
  253. paddingRight: 14,
  254. position: 'relative',
  255. alignItems: 'center',
  256. justifyContent: 'center'
  257. },
  258. searchInput: {
  259. width: '100%',
  260. height: 40,
  261. borderRadius: 20,
  262. backgroundColor: '#fff',
  263. paddingLeft: '31%'
  264. },
  265. list: {
  266. width: '100%',
  267. display: 'flex',
  268. flexDirection: 'row',
  269. justifyContent: 'space-between',
  270. flexWrap: 'wrap',
  271. paddingLeft: 14,
  272. paddingRight: 14
  273. },
  274. head: {
  275. display: 'flex',
  276. flexDirection: 'row',
  277. backgroundColor: '#fff',
  278. paddingTop: 16,
  279. paddingBottom: 16,
  280. paddingLeft: 14,
  281. paddingRight: 14
  282. },
  283. course: {
  284. width: '30%',
  285. display: 'flex',
  286. flexDirection: 'column',
  287. alignItems: 'center'
  288. },
  289. img: {
  290. width: '100%',
  291. height: 155
  292. },
  293. iconImg: {
  294. width: '100%',
  295. height: '100%'
  296. },
  297. callUpGrade: {
  298. width: 113,
  299. height: 120,
  300. borderRadius: 20,
  301. position: 'absolute',
  302. left: 9,
  303. top: 75,
  304. zIndex: 999,
  305. backgroundColor: '#fff',
  306. flexDirection: 'column',
  307. borderStyle: 'solid',
  308. borderWidth: 1,
  309. borderColor: '#F0F1F5'
  310. },
  311. rightBtn: {
  312. width: '100%',
  313. height: 40,
  314. backgroundColor: '#fff',
  315. borderRadius: 20,
  316. alignItems: 'center',
  317. justifyContent: 'space-around',
  318. flexDirection: 'row',
  319. paddingLeft: 20,
  320. paddingRight: 10,
  321. },
  322. rightBtnIcon: {
  323. width: Dimensions.getWidth(23),
  324. height: Dimensions.getHeight(23)
  325. },
  326. rightBtnText: {
  327. color: '#151515',
  328. fontSize: 18
  329. },
  330. gradeItemMid: {
  331. zIndex: 999,
  332. flex: 1,
  333. alignItems: 'center',
  334. justifyContent: 'center',
  335. borderTopWidth: 1,
  336. borderBottomWidth: 1,
  337. borderColor: '#f0f1f5'
  338. },
  339. title: {
  340. display: 'flex',
  341. flexDirection: 'row',
  342. justifyContent: 'space-between',
  343. alignItems: 'center',
  344. },
  345. courseFont: {
  346. color: '#373737',
  347. fontSize: 16
  348. },
  349. border: {
  350. borderStyle: 'solid',
  351. borderBottomWidth: 1,
  352. borderColor: '#F0F1F5'
  353. },
  354. padding: {
  355. paddingLeft: 20,
  356. paddingRight: 20,
  357. paddingTop: 10,
  358. paddingBottom: 10,
  359. },
  360. })