Search.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.info("[Search] 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('searchStation-err', err);
  63. this.setState({
  64. isSearch: false,
  65. searchResult: []
  66. });
  67. });
  68. }
  69. intoStation(info) {
  70. utils.toChargeDetailPage(info.id, 'search', PageList.search);
  71. //startPage(PageList.chargeDetail, {stationInfo: info, action: 'search'});
  72. }
  73. listItem = (props) => {
  74. return <ListView {...props} onPress={() => this.intoStation(props.item)}/>
  75. }
  76. render() {
  77. return (
  78. <View style={styles.container}>
  79. <View style={styles.searchView}>
  80. <Feather
  81. name={'search'}
  82. size={20}
  83. color={'#999'}/>
  84. <TextInput
  85. style={styles.searchInput}
  86. autoFocus={true}
  87. maxLength={50}
  88. numberOfLines={1}
  89. returnKeyType={'search'}
  90. clearButtonMode={'while-editing'}
  91. placeholder={$t('home.searchHint')}
  92. placeholderTextColor={textPlacehoder}
  93. onChangeText={text => {
  94. this.searchWorld = text;
  95. }}
  96. onSubmitEditing={() => {
  97. this.getGeoLocation();
  98. }}/>
  99. </View>
  100. { this.state.isSearch
  101. ? <View style={styles.searchingView}>
  102. <Image
  103. style={styles.seachingIcon}
  104. source={require('../../images/icon/loading.gif')}/>
  105. </View>
  106. : <FlatList
  107. style={styles.listView}
  108. data={this.state.searchResult}
  109. renderItem={this.listItem}
  110. keyExtractor={item => item.id}
  111. ListEmptyComponent={<Text style={styles.noResult}>{$t('home.noSearch')}</Text>}
  112. />
  113. }
  114. </View>
  115. );
  116. }
  117. }
  118. const styles = StyleSheet.create({
  119. container: {
  120. flex: 1,
  121. backgroundColor: colorLight
  122. },
  123. searchView: {
  124. marginTop: 16,
  125. marginLeft: 16,
  126. marginRight: 16,
  127. marginBottom: 8,
  128. paddingLeft: 16,
  129. paddingRight: 16,
  130. borderRadius: 60,
  131. borderWidth: 1,
  132. borderColor: '#E5E5E5',
  133. alignItems: 'center',
  134. flexDirection: 'row',
  135. backgroundColor: '#F5F5F5'
  136. },
  137. searchInput: {
  138. flex: 1,
  139. color: textPrimary,
  140. ...$padding(6, 8),
  141. fontSize: 15,
  142. marginLeft: 4,
  143. lineHeight: 20
  144. },
  145. searchingView: {
  146. padding: 16,
  147. alignItems: 'center'
  148. },
  149. seachingIcon: {
  150. width: 60,
  151. height: 60
  152. },
  153. noResult: {
  154. color: '#999',
  155. fontSize: 14,
  156. padding: 20,
  157. textAlign: 'center',
  158. },
  159. listView: {
  160. flex: 1
  161. },
  162. itemView: {
  163. alignItems: 'center',
  164. flexDirection: 'row',
  165. borderBottomWidth: 1,
  166. borderBottomColor: '#eee'
  167. },
  168. stationInfo: {
  169. flex: 1,
  170. padding: 16
  171. },
  172. nameView: {
  173. paddingTop: 3,
  174. alignItems: 'center',
  175. flexDirection: 'row'
  176. },
  177. stationName: {
  178. color: textPrimary,
  179. fontSize: 18,
  180. fontWeight: 'bold'
  181. },
  182. stationAddress: {
  183. color: '#666',
  184. fontSize: 14,
  185. paddingBottom: 8
  186. },
  187. infoStatus: {
  188. fontSize: 10,
  189. paddingTop: 3,
  190. paddingLeft: 8,
  191. paddingRight: 8,
  192. paddingBottom: 3,
  193. borderRadius: 5,
  194. marginLeft: 12,
  195. },
  196. selected: {
  197. color: textPrimary,
  198. backgroundColor: colorAccent
  199. },
  200. available: {
  201. color: textLight,
  202. backgroundColor: '#90DB0A'
  203. },
  204. unavailable: {
  205. color: '#999',
  206. fontSize: 10.5,
  207. paddingTop: 7,
  208. paddingLeft: 9,
  209. paddingRight: 9,
  210. paddingBottom: 7,
  211. backgroundColor: '#CCC'
  212. },
  213. connectView: {
  214. paddingTop: 4,
  215. paddingBottom: 4,
  216. alignItems: 'center',
  217. flexDirection: 'row'
  218. },
  219. directView: {
  220. zIndex: 1,
  221. paddingTop: 4,
  222. paddingRight: 16,
  223. alignItems: 'center'
  224. },
  225. distanceText: {
  226. color: textPrimary,
  227. fontSize: 12,
  228. paddingTop: 2
  229. },
  230. });