VehicleDetail.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * 添加/编辑 Vehicle页面
  3. * @邠心vbe on 2024/01/30
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, TextInput, Image, Pressable, ScrollView } from 'react-native';
  7. import Dialog from '../../components/Dialog';
  8. import apiUser from '../../api/apiUser';
  9. import TextView from '../../components/TextView';
  10. import Dropdown from '../../components/Dropdown';
  11. import { TypeImageList } from '../chargeV2/Charging';
  12. import ImageCropPicker from 'react-native-image-crop-picker';
  13. import { UploadThemes } from '../../components/ThemesConfig';
  14. import apiUpload from '../../api/apiUpload';
  15. import utils from '../../utils/utils';
  16. const options = {
  17. width: 300,
  18. height: 300,
  19. cropping: true,
  20. multiple: false,
  21. mediaType: 'photo',
  22. writeTempFile: false,
  23. compressImageQuality: 0.8,
  24. compressImageMaxWidth: 720,
  25. compressImageMaxHeight: 1280,
  26. ...UploadThemes
  27. }
  28. export default class VehicleDetail extends Component {
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. isEdit: false,
  33. vehicleInfo: {
  34. brand: "",
  35. model: "",
  36. connectorType: ""
  37. },
  38. brandOptions: [],
  39. modelOptions: [],
  40. typeOptions: []
  41. };
  42. }
  43. componentDidMount() {
  44. this.getBrandOptions();
  45. const params = this.props.route.params;
  46. if (params.id) {
  47. this.setState({
  48. isEdit: true
  49. })
  50. Dialog.showProgressDialog();
  51. this.getVehicleData(params.id);
  52. }
  53. }
  54. getBrandOptions() {
  55. apiUser.getVehicleBrandList().then(res => {
  56. if (res.data) {
  57. this.setState({
  58. brandOptions: res.data
  59. })
  60. }
  61. }).catch((err) => {
  62. Dialog.dismissLoading();
  63. toastShort(err)
  64. });
  65. const types = []
  66. TypeImageList.forEach((item, index) => {
  67. types.push(item.name);
  68. })
  69. this.setState({
  70. typeOptions: types
  71. })
  72. }
  73. getModelOptions() {
  74. if (this.state.vehicleInfo.brand) {
  75. apiUser.getVehicleModelByBrand({
  76. brand: this.state.vehicleInfo.brand
  77. }).then(res => {
  78. if (res.data) {
  79. this.setState({
  80. modelOptions: res.data
  81. })
  82. }
  83. }).catch((err) => {
  84. toastShort(err)
  85. this.setState({
  86. modelOptions: []
  87. })
  88. });
  89. } else {
  90. this.setState({
  91. modelOptions: []
  92. })
  93. }
  94. }
  95. getVehicleData(id) {
  96. apiUser.getVehicleById({
  97. vehiclePk: id
  98. }).then(res => {
  99. Dialog.dismissLoading();
  100. if (res.data) {
  101. const info = res.data;
  102. this.setState({
  103. vehicleInfo: info
  104. }, () => {
  105. this.getModelOptions();
  106. });
  107. }
  108. }).catch((err) => {
  109. Dialog.dismissLoading();
  110. toastShort(err)
  111. });
  112. }
  113. changeForm(key, value) {
  114. const form = {
  115. ...this.state.vehicleInfo
  116. }
  117. form[key] = value;
  118. this.setState({
  119. vehicleInfo: form
  120. })
  121. }
  122. uploadImage() {
  123. ImageCropPicker.openPicker({
  124. ...options,
  125. cropperToolbarTitle: $t('common.cropperTitle')
  126. }).then(image => {
  127. if (image.path) {
  128. apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => {
  129. if (res.success && res.data.picturePath) {
  130. this.changeForm("vehiclePhoto", res.data.picturePath)
  131. toastShort($t('common.uploadSuccess'));
  132. } else {
  133. toastShort($t('common.uploadFailed'));
  134. }
  135. }).catch(err => {
  136. toastShort(err);
  137. });
  138. }
  139. }).catch(err1 => {
  140. //console.log(err1);
  141. });
  142. }
  143. validate() {
  144. if (!this.state.vehicleInfo.brand) {
  145. toastShort($t('profile.msgSelectBrand'));
  146. return;
  147. }
  148. if (!this.state.vehicleInfo.vehicleModelId) {
  149. toastShort($t('profile.msgSelectModel'));
  150. return;
  151. }
  152. if (!this.state.vehicleInfo.licensePlate) {
  153. toastShort($t('profile.msgInputPlate'));
  154. return;
  155. }
  156. /*if (!this.state.vehicleInfo.batteryCapacity) {
  157. toastShort($t('profile.msgInputBatCap'));
  158. return;
  159. }
  160. if (!this.state.vehicleInfo.connectorType) {
  161. toastShort($t('profile.msgSelectType'));
  162. return;
  163. }
  164. if (!this.state.vehicleInfo.vehiclePhoto) {
  165. toastShort($t('profile.msgVehiclePhoto'));
  166. return;
  167. }*/
  168. //console.log("车辆信息", this.state.vehicleInfo);
  169. if (this.state.isEdit) {
  170. this.updateVehicle(this.state.vehicleInfo)
  171. } else {
  172. this.addVehicle(this.state.vehicleInfo)
  173. }
  174. }
  175. addVehicle(params) {
  176. Dialog.showProgressDialog();
  177. apiUser.addVehicle(params).then(res => {
  178. Dialog.dismissLoading();
  179. toastShort($t('common.addSuccess'));
  180. goBack();
  181. }).catch((err) => {
  182. Dialog.dismissLoading();
  183. toastShort(err);
  184. });
  185. }
  186. updateVehicle(params) {
  187. Dialog.showProgressDialog();
  188. apiUser.updateVehicle(params).then(res => {
  189. Dialog.dismissLoading();
  190. toastShort($t('common.updateSuccess'));
  191. goBack();
  192. }).catch((err) => {
  193. Dialog.dismissLoading();
  194. toastShort(err);
  195. });
  196. }
  197. render() {
  198. return (
  199. <ScrollView
  200. style={styles.container}
  201. contentContainerStyle={$padding(16)}>
  202. <View style={ui.flexc}>
  203. <MaterialCommunityIcons
  204. name="form-dropdown"
  205. size={20}
  206. color={textPrimary}
  207. style={ui.bold}
  208. />
  209. <TextView style={styles.titleText}>{$t('profile.vehicleBrand')}</TextView>
  210. </View>
  211. <Dropdown
  212. style={styles.formDropdown}
  213. list={this.state.brandOptions}
  214. placeholder={$t('profile.selectBrand')}
  215. title={$t('profile.vehicleBrand')}
  216. value={this.state.vehicleInfo.brand}
  217. autoSelect={false}
  218. onChange={value => {
  219. this.changeForm("brand", value);
  220. this.getModelOptions();
  221. }}
  222. />
  223. <Dropdown
  224. style={styles.formDropdown}
  225. list={this.state.modelOptions}
  226. placeholder={$t('profile.selectModel')}
  227. title={$t('profile.vehicleModel')}
  228. value={this.state.vehicleInfo.vehicleModelId}
  229. valueKey={"vehicleModelId"}
  230. nameKey={"model"}
  231. autoSelect={true}
  232. onChange={value => this.changeForm("vehicleModelId", value)}
  233. />
  234. <TextInput
  235. style={styles.formInput}
  236. defaultValue={this.state.vehicleInfo.licensePlate}
  237. placeholder={$t('profile.enterLicensePlate')}
  238. placeholderTextColor={textPlacehoder}
  239. maxLength={20}
  240. onChangeText={text => this.changeForm("licensePlate", text)}
  241. />
  242. <View style={styles.formInputRow}>
  243. <TextInput
  244. style={styles.itemInput}
  245. defaultValue={this.state.vehicleInfo.batteryCapacity}
  246. placeholder={$t('profile.enterBatteryCapacity')}
  247. placeholderTextColor={textPlacehoder}
  248. maxLength={20}
  249. keyboardType='decimal-pad'
  250. onChangeText={text => this.changeForm("batteryCapacity", text)}
  251. />
  252. <TextView style={styles.titleText}>kWh</TextView>
  253. </View>
  254. <View style={ui.flexc}>
  255. <MaterialCommunityIcons
  256. name="form-dropdown"
  257. size={20}
  258. color={textPrimary}
  259. style={ui.bold}
  260. />
  261. <TextView style={styles.titleText}>{$t('profile.connecterType')}</TextView>
  262. </View>
  263. <Dropdown
  264. style={styles.formDropdown}
  265. list={this.state.typeOptions}
  266. placeholder={$t('profile.selectConnecterType')}
  267. title={$t('profile.connecterType')}
  268. value={this.state.vehicleInfo.connectorType}
  269. onChange={value => this.changeForm("connectorType", value)}
  270. autoSelect={false}
  271. />
  272. <View style={ui.flexc}>
  273. <MaterialCommunityIcons
  274. name="tray-arrow-up"
  275. size={20}
  276. color={textPrimary}
  277. style={ui.bold}
  278. />
  279. <TextView style={styles.titleText}>{$t('profile.plsUploadImageCar')}</TextView>
  280. </View>
  281. <View style={styles.uploadGroup}>
  282. { this.state.vehicleInfo.vehiclePhoto
  283. ? <Pressable
  284. onPress={() => this.uploadImage()}>
  285. <Image
  286. style={styles.uploadImage}
  287. source={{uri: utils.getImageUrl(this.state.vehicleInfo.vehiclePhoto)}}
  288. />
  289. </Pressable>
  290. : <Pressable
  291. style={styles.uploadView}
  292. onPress={() => this.uploadImage()}>
  293. <MaterialCommunityIcons
  294. name="tray-arrow-up"
  295. size={32}
  296. color={textCancel}
  297. style={ui.bold}
  298. />
  299. <TextView style={styles.uploadTitle}>{$t('profile.selectUpload')}</TextView>
  300. <TextView style={styles.uploadDesc}>{$t('profile.max4mbPhoto')}</TextView>
  301. </Pressable>
  302. }
  303. </View>
  304. <View style={styles.buttonView}>
  305. <Button
  306. text={$t('common.save')}
  307. elevation={1.5}
  308. onClick={() => {
  309. this.validate();
  310. }}/>
  311. </View>
  312. </ScrollView>
  313. );
  314. }
  315. }
  316. const styles = StyleSheet.create({
  317. container: {
  318. flex: 1
  319. },
  320. titleText: {
  321. padding: 8,
  322. fontSize: 14,
  323. color: textPrimary,
  324. fontWeight: 'bold'
  325. },
  326. formInput: {
  327. ...$margin(8, 0),
  328. padding: 12,
  329. borderWidth: 1,
  330. borderRadius: 4,
  331. borderStyle: 'solid',
  332. borderColor: "#EAEAEA",
  333. },
  334. formDropdown: {
  335. ...$margin(8, 0),
  336. padding: 12,
  337. borderWidth: 1,
  338. borderRadius: 4,
  339. borderStyle: 'solid',
  340. borderColor: "#EAEAEA",
  341. alignItems: 'center',
  342. flexDirection: 'row'
  343. },
  344. formInputRow: {
  345. ...$margin(8, 0),
  346. padding: 8,
  347. borderWidth: 1,
  348. borderRadius: 4,
  349. borderStyle: 'solid',
  350. borderColor: "#EAEAEA",
  351. alignItems: 'center',
  352. flexDirection: 'row'
  353. },
  354. itemInput: {
  355. flex: 1,
  356. padding: 4
  357. },
  358. uploadView: {
  359. width: 160,
  360. height: 160,
  361. ...$margin(8, 0),
  362. padding: 8,
  363. borderWidth: 1,
  364. borderRadius: 4,
  365. borderStyle: 'solid',
  366. borderColor: "#EAEAEA",
  367. alignItems: 'center',
  368. justifyContent: 'center'
  369. },
  370. uploadImage: {
  371. width: 160,
  372. height: 160,
  373. ...$margin(8, 0)
  374. },
  375. uploadTitle: {
  376. fontSize: 14,
  377. color: textPrimary,
  378. paddingTop: 8
  379. },
  380. uploadDesc: {
  381. fontSize: 12,
  382. color: textCancel,
  383. paddingTop: 2
  384. },
  385. buttonView: {
  386. marginTop: 32,
  387. marginBottom: 8
  388. }
  389. })