SearchV2.js 6.4 KB

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