SearchV2.js 5.6 KB

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