Search.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * 搜索页
  3. * @邠心vbe on 2021/04/12
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, TextInput, FlatList, Pressable, Linking, Image } from 'react-native';
  7. import apiStation from '../../api/apiStation';
  8. import TextRadius from '../../components/TextRadius';
  9. import utils from '../../utils/utils';
  10. import Provider from '../charge/Provider';
  11. import { PageList } from '../Router';
  12. export default class Search extends Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. isSearch: false,
  17. searchResult: [{id:0}]
  18. };
  19. }
  20. componentDidMount() {
  21. this.getGeoLocation();
  22. }
  23. getGeoLocation() {
  24. navigator.geolocation.getCurrentPosition(location => {
  25. let latlng = {
  26. lat: location.coords.latitude,
  27. lng: location.coords.longitude
  28. }
  29. this.searchStation(latlng);
  30. }, error => {
  31. console.warn("getGeoLocation", error);
  32. });
  33. }
  34. searchStation(latlng) {
  35. this.setState({
  36. isSearch: true
  37. });
  38. latlng.siteName = this.searchWorld
  39. apiStation.searchStation(latlng).then(res => {
  40. if (res.data.sites) {
  41. const list = [];
  42. res.data.sites.forEach(item => {
  43. list.push({
  44. id: item.sitePk,
  45. name: item.siteName,
  46. address: item.siteAddress,
  47. latitude: item.locationLatitude,
  48. longitude: item.locationLongitude,
  49. acConnector: item.acConnector,
  50. allConnector: item.allConnector,
  51. dcConnector: item.dcConnector,
  52. siteType: item.siteType,
  53. distance: utils.getDistance(item.distance),
  54. serviceProvider: item.serviceProvider
  55. });
  56. });
  57. this.setState({
  58. isSearch: false,
  59. searchResult: list
  60. });
  61. }
  62. }).catch(err => {
  63. console.log('err', err);
  64. this.setState({
  65. isSearch: false,
  66. searchResult: []
  67. });
  68. });
  69. }
  70. intoStation(info) {
  71. startPage(PageList.chargeDetail, {stationInfo: info, action: 'search'});
  72. }
  73. listItem = ({item, index, separators}) => {
  74. if (item.id) {
  75. return (
  76. <View
  77. style={styles.itemView}
  78. key={index}>
  79. <Pressable
  80. style={styles.stationInfo}
  81. onPress={() => this.intoStation(item)}>
  82. <View style={styles.nameView}>
  83. <Text style={styles.stationName}>{item.name}</Text>
  84. { item.allConnector && item.allConnector.available > 0 &&
  85. <TextRadius style={[styles.infoStatus, styles.available]}>Available</TextRadius>
  86. }
  87. </View>
  88. <Provider providers={item.serviceProvider}/>
  89. <Text style={styles.stationAddress}>{item.address}</Text>
  90. <View style={styles.connectView}>
  91. <ConnectType {...item.acConnector}/>
  92. <ConnectType {...item.dcConnector}/>
  93. </View>
  94. </Pressable>
  95. <Pressable
  96. style={styles.directView}
  97. onPress={() => {
  98. utils.directMaps(item.latitude, item.longitude, item.address);
  99. }}>
  100. <MaterialIcons
  101. name='directions'
  102. size={32}
  103. color={colorAccent}/>
  104. <Text style={styles.distanceText}>{item.distance}</Text>
  105. </Pressable>
  106. </View>
  107. );
  108. } else {
  109. return null;
  110. }
  111. }
  112. render() {
  113. return (
  114. <View style={styles.container}>
  115. <View style={styles.searchView}>
  116. <Feather
  117. name={'search'}
  118. size={20}
  119. color={'#999'}/>
  120. <TextInput
  121. style={styles.searchInput}
  122. autoFocus={true}
  123. maxLength={50}
  124. numberOfLines={1}
  125. returnKeyType={'search'}
  126. clearButtonMode={'while-editing'}
  127. placeholder='Search using location name or service provider'
  128. onChangeText={text => {
  129. this.searchWorld = text;
  130. }}
  131. onSubmitEditing={() => {
  132. this.getGeoLocation();
  133. }}/>
  134. </View>
  135. { this.state.isSearch
  136. ? <View style={styles.searchingView}>
  137. <Image
  138. style={styles.seachingIcon}
  139. source={require('../../images/icon/loading.gif')}/>
  140. </View>
  141. : <FlatList
  142. style={styles.listView}
  143. data={this.state.searchResult}
  144. renderItem={this.listItem}
  145. keyExtractor={item => item.id}
  146. ListEmptyComponent={<Text style={styles.noResult}>No search result</Text>}
  147. />
  148. }
  149. </View>
  150. );
  151. }
  152. }
  153. export const ConnectType = ({type, available, all}) => {
  154. if (type) {
  155. return (
  156. <View style={styles.connectType}>
  157. <Text style={styles.typeLabel}>{type}</Text>
  158. <Text style={styles.typeContent}><Text style={styles.typeBold}>{available}</Text>/{all}</Text>
  159. </View>
  160. );
  161. } else {
  162. return null;
  163. }
  164. }
  165. const styles = StyleSheet.create({
  166. container: {
  167. flex: 1,
  168. backgroundColor: 'white'
  169. },
  170. searchView: {
  171. marginTop: 16,
  172. marginLeft: 16,
  173. marginRight: 16,
  174. marginBottom: 8,
  175. paddingLeft: 16,
  176. paddingRight: 16,
  177. borderRadius: 60,
  178. alignItems: 'center',
  179. flexDirection: 'row',
  180. backgroundColor: '#F5F5F5'
  181. },
  182. searchInput: {
  183. flex: 1,
  184. color: '#333',
  185. ...$padding(6, 8),
  186. fontSize: 15,
  187. marginLeft: 4,
  188. },
  189. searchingView: {
  190. padding: 16,
  191. alignItems: 'center'
  192. },
  193. seachingIcon: {
  194. width: 60,
  195. height: 60
  196. },
  197. noResult: {
  198. color: '#999',
  199. fontSize: 14,
  200. padding: 20,
  201. textAlign: 'center',
  202. },
  203. listView: {
  204. flex: 1
  205. },
  206. itemView: {
  207. alignItems: 'center',
  208. flexDirection: 'row',
  209. borderBottomWidth: 1,
  210. borderBottomColor: '#eee'
  211. },
  212. stationInfo: {
  213. flex: 1,
  214. padding: 16
  215. },
  216. nameView: {
  217. paddingTop: 3,
  218. alignItems: 'center',
  219. flexDirection: 'row'
  220. },
  221. stationName: {
  222. color: '#333',
  223. fontSize: 18,
  224. fontWeight: 'bold'
  225. },
  226. stationAddress: {
  227. color: '#666',
  228. fontSize: 14,
  229. paddingBottom: 8
  230. },
  231. infoStatus: {
  232. fontSize: 10,
  233. paddingTop: 3,
  234. paddingLeft: 8,
  235. paddingRight: 8,
  236. paddingBottom: 3,
  237. borderRadius: 5,
  238. marginLeft: 12,
  239. },
  240. selected: {
  241. color: '#333',
  242. backgroundColor: colorAccent
  243. },
  244. available: {
  245. color: 'white',
  246. backgroundColor: '#90DB0A'
  247. },
  248. unavailable: {
  249. color: '#999',
  250. fontSize: 10.5,
  251. paddingTop: 7,
  252. paddingLeft: 9,
  253. paddingRight: 9,
  254. paddingBottom: 7,
  255. backgroundColor: '#CCC'
  256. },
  257. connectView: {
  258. paddingTop: 4,
  259. paddingBottom: 4,
  260. alignItems: 'center',
  261. flexDirection: 'row'
  262. },
  263. connectType: {
  264. borderWidth: 1,
  265. borderColor: '#333',
  266. borderRadius: 3,
  267. marginRight: 16,
  268. alignItems: 'center',
  269. flexDirection: 'row',
  270. },
  271. typeLabel: {
  272. color: '#fff',
  273. fontSize: 12,
  274. paddingTop: 2,
  275. paddingLeft: 10,
  276. paddingRight: 10,
  277. paddingBottom: 2,
  278. backgroundColor: '#333'
  279. },
  280. typeContent: {
  281. color: '#333',
  282. fontSize: 12,
  283. paddingLeft: 10,
  284. paddingRight: 6,
  285. },
  286. typeBold: {
  287. fontSize: 14,
  288. fontWeight: 'bold'
  289. },
  290. directView: {
  291. zIndex: 1,
  292. paddingTop: 4,
  293. paddingRight: 16,
  294. alignItems: 'center'
  295. },
  296. distanceText: {
  297. color: '#333',
  298. fontSize: 12,
  299. paddingTop: 2
  300. },
  301. });