SearchV2.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. };
  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.warn("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. setTimeout(() => {
  57. this.setState({
  58. isSearch: false,
  59. searchResult: list
  60. });
  61. }, 1000);
  62. }
  63. }).catch(err => {
  64. console.log('err', err);
  65. this.setState({
  66. isSearch: false,
  67. searchResult: []
  68. });
  69. });
  70. }
  71. intoStation(info) {
  72. startPage(PageList.chargeDetailPage, {stationInfo: info, action: 'search', from: PageList.search});
  73. }
  74. listItem = (props) => {
  75. return <ListViewV2 {...props} onPress={() => this.intoStation(props.item)}/>
  76. }
  77. render() {
  78. return (
  79. <View style={styles.container}>
  80. <View style={styles.searchView}>
  81. <Feather
  82. name={'search'}
  83. size={20}
  84. color={'#999'}/>
  85. <TextInput
  86. style={styles.searchInput}
  87. autoFocus={true}
  88. maxLength={50}
  89. numberOfLines={1}
  90. returnKeyType={'search'}
  91. clearButtonMode={'while-editing'}
  92. placeholder='Search using site name or service provider'
  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/search-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. keyboardShouldPersistTaps="always"
  112. ListEmptyComponent={<Text style={styles.noResult}>No search result</Text>}
  113. />
  114. }
  115. </View>
  116. );
  117. }
  118. }
  119. const styles = StyleSheet.create({
  120. container: {
  121. flex: 1,
  122. backgroundColor: colorLight //pageBackground
  123. },
  124. searchView: {
  125. marginTop: 16,
  126. marginLeft: 16,
  127. marginRight: 16,
  128. marginBottom: 8,
  129. paddingLeft: 16,
  130. paddingRight: 16,
  131. borderRadius: 60,
  132. borderWidth: 1,
  133. borderColor: '#E5E5E5',
  134. alignItems: 'center',
  135. flexDirection: 'row',
  136. backgroundColor: '#F5F5F5'
  137. },
  138. searchInput: {
  139. flex: 1,
  140. color: textPrimary,
  141. ...$padding(6, 8),
  142. fontSize: 15,
  143. marginLeft: 4,
  144. lineHeight: 20
  145. },
  146. searchingView: {
  147. padding: 0,
  148. alignItems: 'center'
  149. },
  150. seachingIcon: {
  151. width: 120,
  152. height: 120
  153. },
  154. noResult: {
  155. color: '#999',
  156. fontSize: 14,
  157. padding: 20,
  158. textAlign: 'center',
  159. },
  160. listView: {
  161. flex: 1
  162. },
  163. itemView: {
  164. alignItems: 'center',
  165. flexDirection: 'row',
  166. borderBottomWidth: 1,
  167. borderBottomColor: '#eee'
  168. },
  169. stationInfo: {
  170. flex: 1,
  171. padding: 16
  172. },
  173. nameView: {
  174. paddingTop: 3,
  175. alignItems: 'center',
  176. flexDirection: 'row'
  177. },
  178. stationName: {
  179. color: textPrimary,
  180. fontSize: 18,
  181. fontWeight: 'bold'
  182. },
  183. stationAddress: {
  184. color: '#666',
  185. fontSize: 14,
  186. paddingBottom: 8
  187. },
  188. infoStatus: {
  189. fontSize: 10,
  190. paddingTop: 3,
  191. paddingLeft: 8,
  192. paddingRight: 8,
  193. paddingBottom: 3,
  194. borderRadius: 5,
  195. marginLeft: 12,
  196. },
  197. selected: {
  198. color: textPrimary,
  199. backgroundColor: colorAccent
  200. },
  201. available: {
  202. color: textLight,
  203. backgroundColor: '#90DB0A'
  204. },
  205. unavailable: {
  206. color: '#999',
  207. fontSize: 10.5,
  208. paddingTop: 7,
  209. paddingLeft: 9,
  210. paddingRight: 9,
  211. paddingBottom: 7,
  212. backgroundColor: '#CCC'
  213. },
  214. connectView: {
  215. paddingTop: 4,
  216. paddingBottom: 4,
  217. alignItems: 'center',
  218. flexDirection: 'row'
  219. },
  220. directView: {
  221. zIndex: 1,
  222. paddingTop: 4,
  223. paddingRight: 16,
  224. alignItems: 'center'
  225. },
  226. distanceText: {
  227. color: textPrimary,
  228. fontSize: 12,
  229. paddingTop: 2
  230. },
  231. });