Search.js 5.3 KB

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