SearchV2.js 5.5 KB

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