Search.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * 搜索页
  3. * @邠心vbe on 2021/04/12
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, TextInput, FlatList, Image } from 'react-native';
  7. import apiStation from '../../api/apiStation';
  8. import utils from '../../utils/utils';
  9. import { PageList } from '../Router';
  10. import ListView from './ListView';
  11. export default class Search extends Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. isSearch: false,
  16. searchResult: [{id:0}]
  17. };
  18. }
  19. componentDidMount() {
  20. this.getGeoLocation();
  21. }
  22. getGeoLocation() {
  23. navigator.geolocation.getCurrentPosition(location => {
  24. let latlng = {
  25. lat: location.coords.latitude,
  26. lng: location.coords.longitude
  27. }
  28. this.searchStation(latlng);
  29. }, error => {
  30. console.warn("getGeoLocation", error);
  31. });
  32. }
  33. searchStation(latlng) {
  34. this.setState({
  35. isSearch: true
  36. });
  37. latlng.siteName = this.searchWorld
  38. apiStation.searchStation(latlng).then(res => {
  39. if (res.data.sites) {
  40. const list = [];
  41. res.data.sites.forEach(item => {
  42. list.push({
  43. id: item.sitePk,
  44. name: item.siteName,
  45. address: item.siteAddress,
  46. latitude: item.locationLatitude,
  47. longitude: item.locationLongitude,
  48. acConnector: item.acConnector,
  49. allConnector: item.allConnector,
  50. dcConnector: item.dcConnector,
  51. siteType: item.siteType,
  52. distance: utils.getDistance(item.distance),
  53. serviceProvider: item.serviceProvider
  54. });
  55. });
  56. this.setState({
  57. isSearch: false,
  58. searchResult: list
  59. });
  60. }
  61. }).catch(err => {
  62. console.log('err', err);
  63. this.setState({
  64. isSearch: false,
  65. searchResult: []
  66. });
  67. });
  68. }
  69. intoStation(info) {
  70. startPage(PageList.chargeDetail, {stationInfo: info, action: 'search'});
  71. }
  72. listItem = (props) => {
  73. return <ListView {...props} onPress={() => this.intoStation(props.item)}/>
  74. }
  75. render() {
  76. return (
  77. <View style={styles.container}>
  78. <View style={styles.searchView}>
  79. <Feather
  80. name={'search'}
  81. size={20}
  82. color={'#999'}/>
  83. <TextInput
  84. style={styles.searchInput}
  85. autoFocus={true}
  86. maxLength={50}
  87. numberOfLines={1}
  88. returnKeyType={'search'}
  89. clearButtonMode={'while-editing'}
  90. placeholder='Search using site name or service provider'
  91. onChangeText={text => {
  92. this.searchWorld = text;
  93. }}
  94. onSubmitEditing={() => {
  95. this.getGeoLocation();
  96. }}/>
  97. </View>
  98. { this.state.isSearch
  99. ? <View style={styles.searchingView}>
  100. <Image
  101. style={styles.seachingIcon}
  102. source={require('../../images/icon/loading.gif')}/>
  103. </View>
  104. : <FlatList
  105. style={styles.listView}
  106. data={this.state.searchResult}
  107. renderItem={this.listItem}
  108. keyExtractor={item => item.id}
  109. ListEmptyComponent={<Text style={styles.noResult}>No search result</Text>}
  110. />
  111. }
  112. </View>
  113. );
  114. }
  115. }
  116. const styles = StyleSheet.create({
  117. container: {
  118. flex: 1,
  119. backgroundColor: colorLight
  120. },
  121. searchView: {
  122. marginTop: 16,
  123. marginLeft: 16,
  124. marginRight: 16,
  125. marginBottom: 8,
  126. paddingLeft: 16,
  127. paddingRight: 16,
  128. borderRadius: 60,
  129. borderWidth: 1,
  130. borderColor: '#E5E5E5',
  131. alignItems: 'center',
  132. flexDirection: 'row',
  133. backgroundColor: '#F5F5F5'
  134. },
  135. searchInput: {
  136. flex: 1,
  137. color: textPrimary,
  138. ...$padding(6, 8),
  139. fontSize: 15,
  140. marginLeft: 4,
  141. lineHeight: 20
  142. },
  143. searchingView: {
  144. padding: 16,
  145. alignItems: 'center'
  146. },
  147. seachingIcon: {
  148. width: 60,
  149. height: 60
  150. },
  151. noResult: {
  152. color: '#999',
  153. fontSize: 14,
  154. padding: 20,
  155. textAlign: 'center',
  156. },
  157. listView: {
  158. flex: 1
  159. },
  160. itemView: {
  161. alignItems: 'center',
  162. flexDirection: 'row',
  163. borderBottomWidth: 1,
  164. borderBottomColor: '#eee'
  165. },
  166. stationInfo: {
  167. flex: 1,
  168. padding: 16
  169. },
  170. nameView: {
  171. paddingTop: 3,
  172. alignItems: 'center',
  173. flexDirection: 'row'
  174. },
  175. stationName: {
  176. color: textPrimary,
  177. fontSize: 18,
  178. fontWeight: 'bold'
  179. },
  180. stationAddress: {
  181. color: '#666',
  182. fontSize: 14,
  183. paddingBottom: 8
  184. },
  185. infoStatus: {
  186. fontSize: 10,
  187. paddingTop: 3,
  188. paddingLeft: 8,
  189. paddingRight: 8,
  190. paddingBottom: 3,
  191. borderRadius: 5,
  192. marginLeft: 12,
  193. },
  194. selected: {
  195. color: textPrimary,
  196. backgroundColor: colorAccent
  197. },
  198. available: {
  199. color: textLight,
  200. backgroundColor: '#90DB0A'
  201. },
  202. unavailable: {
  203. color: '#999',
  204. fontSize: 10.5,
  205. paddingTop: 7,
  206. paddingLeft: 9,
  207. paddingRight: 9,
  208. paddingBottom: 7,
  209. backgroundColor: '#CCC'
  210. },
  211. connectView: {
  212. paddingTop: 4,
  213. paddingBottom: 4,
  214. alignItems: 'center',
  215. flexDirection: 'row'
  216. },
  217. directView: {
  218. zIndex: 1,
  219. paddingTop: 4,
  220. paddingRight: 16,
  221. alignItems: 'center'
  222. },
  223. distanceText: {
  224. color: textPrimary,
  225. fontSize: 12,
  226. paddingTop: 2
  227. },
  228. });