myCars.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <view>
  3. <u-navbar
  4. title-color="#fff"
  5. :custom-back="customBack"
  6. :border-bottom="false"
  7. back-icon-color="#CCE8FF"
  8. :background="{ background: '#008CFF' }"
  9. title="车辆管理"
  10. ></u-navbar>
  11. <view class="header">
  12. <view class="header-title">我的车辆</view>
  13. </view>
  14. <view class="wrap">
  15. <view class="statistics">
  16. <view class="statistics-title">车辆</view>
  17. <view class="statistics-center"></view>
  18. <view class="statistics-number-wrap"
  19. ><span class="number">{{ mycarsTotal }}</span
  20. >辆</view
  21. >
  22. </view>
  23. <view class="new-plate-number">
  24. <view class="message-input-wrap" @click="messageInputClick">
  25. <u-message-input :maxlength="8" width="70" font-size="50" :disabled-keyboard="true" v-model="newPlateNumber"></u-message-input>
  26. </view>
  27. <u-keyboard
  28. ref="uKeyboard"
  29. mode="car"
  30. @change="keyboardChange"
  31. @confirm="keyboardConfirm"
  32. @backspace="backspace"
  33. v-model="keyboardshow"
  34. ></u-keyboard>
  35. </view>
  36. <view class="add-car-btn" @click="handleAddCar">添加车辆</view>
  37. <view class="mycars">
  38. <template v-if="mycars.length">
  39. <view class="mycars-item" v-for="(item, index) in mycars" :key="index">
  40. <view class="mycars-item-name">{{ item.vehicleNo }}</view>
  41. <view class="mycars-item-type">{{ item.energyTpye | energyTpye }}</view>
  42. <view class="mycars-item-sign" v-if="item.contractStatus == 1">已签约</view>
  43. <view class="mycars-item-sign1" v-if="item.contractStatus == 0">未签约</view>
  44. <view class="mycars-item-tool">
  45. <span class="default" v-if="item.isDefault == 1" :class="{ isDefault: item.isDefault == 1 }" @click="handlesetDefault(item.id)"
  46. >默认</span
  47. >
  48. <span class="default" v-else @click="handlesetDefault(item.id)">设为默认</span>
  49. <span @click="handleDelCar(item.id, item.vehicleNo)">删除</span>
  50. </view>
  51. </view>
  52. <u-loadmore :status="status" />
  53. </template>
  54. <template v-else>
  55. <u-empty text="无车辆数据" mode="list"></u-empty>
  56. </template>
  57. </view>
  58. </view>
  59. <u-toast ref="uToast" />
  60. <u-modal v-model="delCarshow" :show-cancel-button="true" @confirm="confirmDelCar" :content="delCarContent"></u-modal>
  61. <u-action-sheet :list="colorList" @click="confirmColor" v-model="colorShow"></u-action-sheet>
  62. </view>
  63. </template>
  64. <script>
  65. export default {
  66. data() {
  67. return {
  68. keyboardshow: false,
  69. delCarshow: false,
  70. delCarId: null,
  71. delCarContent: '',
  72. newPlateNumber: '',
  73. vehicleColor: 0,
  74. mycars: [],
  75. status: 'loadmore',
  76. page: {
  77. num: 1,
  78. size: 10,
  79. total: 0
  80. },
  81. mycarsTotal: 0,
  82. colorShow: false,
  83. colorList: [
  84. { text: '蓝色', colorCode: 0 },
  85. { text: '黄色', colorCode: 1 },
  86. { text: '黑色', colorCode: 2 },
  87. { text: '白色', colorCode: 3 },
  88. { text: '绿色', colorCode: 4 },
  89. { text: '其他', colorCode: 99 }
  90. ]
  91. };
  92. },
  93. onLoad() {
  94. this.mycars = [];
  95. this.handlegetMycars();
  96. },
  97. onPullDownRefresh() {
  98. Object.assign(this, {
  99. keyboardshow: false,
  100. delCarshow: false,
  101. delCarId: null,
  102. delCarContent: '',
  103. newPlateNumber: '',
  104. vehicleColor: 0,
  105. mycars: [],
  106. status: 'loadmore',
  107. page: {
  108. num: 1,
  109. size: 10,
  110. total: 0
  111. },
  112. mycarsTotal: 0,
  113. colorShow: false,
  114. colorList: [
  115. { text: '蓝色', colorCode: 0 },
  116. { text: '黄色', colorCode: 1 },
  117. { text: '黑色', colorCode: 2 },
  118. { text: '白色', colorCode: 3 },
  119. { text: '绿色', colorCode: 4 },
  120. { text: '其他', colorCode: 99 }
  121. ]
  122. });
  123. setTimeout(() => {
  124. this.handlegetMycars();
  125. uni.stopPullDownRefresh(); //停止刷新
  126. }, 1000);
  127. },
  128. onReachBottom() {
  129. if (this.page.num >= this.page.total) return;
  130. this.status = 'loading';
  131. this.page.num = ++this.page.num;
  132. setTimeout(() => {
  133. this.handlegetMycars();
  134. }, 1500);
  135. },
  136. methods: {
  137. customBack() {
  138. uni.getStorage({
  139. key: 'messageBack',
  140. success: (res) => {
  141. this.$u.route({
  142. type: 'switchTab',
  143. url: res.data
  144. });
  145. },
  146. fail: () => {
  147. this.$u.route({
  148. type: 'switchTab',
  149. url: 'pages/index/index'
  150. });
  151. }
  152. });
  153. },
  154. // 获取车辆列表
  155. handlegetMycars() {
  156. const { num, size } = this.page;
  157. this.$u.api.getMycars({ pageNum: num, pageSize: size }).then((res) => {
  158. if (res.code === 200) {
  159. this.mycars = this.mycars.concat(res?.data?.rows ?? []);
  160. this.page.total = Number(res?.data?.pages ?? 0);
  161. this.mycarsTotal = Number(res?.data?.total ?? 0);
  162. if (this.page.num >= this.page.total) this.status = 'nomore';
  163. else this.status = 'loading';
  164. }
  165. });
  166. },
  167. // 添加车辆
  168. async handleAddCar() {
  169. const { newPlateNumber, vehicleColor } = this;
  170. const reg =
  171. /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$/;
  172. if (!reg.test(newPlateNumber) || newPlateNumber.indexOf('I') > -1) {
  173. this.showToast('车牌输入错误,请重新输入!', 'error');
  174. return;
  175. }
  176. try {
  177. const { code, msg } = await this.$u.api.addCar({ vehicleNo: newPlateNumber, vehicleColor });
  178. if (code === 200) {
  179. this.showToast(msg, 'success');
  180. Object.assign(this.page, {
  181. num: 1,
  182. size: 10,
  183. total: 0
  184. });
  185. this.mycars = [];
  186. this.newPlateNumber = '';
  187. this.vehicleColor = '';
  188. this.handlegetMycars();
  189. }
  190. } catch (error) {
  191. this.showToast('操作失败!', 'error');
  192. }
  193. },
  194. // 删除车辆
  195. handleDelCar(id, content) {
  196. this.delCarContent = `是否删除${content}`;
  197. this.delCarId = id;
  198. this.delCarshow = true;
  199. },
  200. // 确认删除
  201. async confirmDelCar() {
  202. try {
  203. const { code, msg } = await this.$u.api.delCar(this.delCarId);
  204. if (code === 200) {
  205. this.showToast(msg, 'success');
  206. Object.assign(this.page, {
  207. num: 1,
  208. size: 10,
  209. total: 0
  210. });
  211. this.mycars = [];
  212. this.handlegetMycars();
  213. }
  214. } catch (error) {
  215. this.showToast('操作失败!', 'error');
  216. }
  217. },
  218. // 点击输入框
  219. messageInputClick() {
  220. this.keyboardshow = true;
  221. },
  222. // 按键被点击(点击退格键不会触发此事件)
  223. keyboardChange(val) {
  224. // 将每次按键的值拼接到value变量中,注意+=写法
  225. this.newPlateNumber += val;
  226. },
  227. // 退格键被点击
  228. backspace() {
  229. // 删除value的最后一个字符
  230. if (this.newPlateNumber.length) this.newPlateNumber = this.newPlateNumber.substr(0, this.newPlateNumber.length - 1);
  231. },
  232. // 键盘输入完成后确认
  233. keyboardConfirm() {
  234. this.colorShow = true;
  235. },
  236. // 确认颜色
  237. confirmColor(e) {
  238. this.vehicleColor = this.colorList[e].colorCode;
  239. },
  240. // 设置默认车辆操作
  241. async handlesetDefault(id) {
  242. try {
  243. const { code, msg } = await this.$u.api.setDefaultCar({ id });
  244. if (code === 200) {
  245. this.showToast(msg, 'success');
  246. Object.assign(this.page, {
  247. num: 1,
  248. size: 10,
  249. total: 0
  250. });
  251. this.mycars = [];
  252. this.handlegetMycars();
  253. }
  254. } catch (error) {
  255. this.showToast('操作失败!', 'error');
  256. }
  257. },
  258. /**
  259. * @description: 显示提示
  260. * @param {*} title
  261. * @param {*} type
  262. * @return {*}
  263. */
  264. showToast(title = '操作失败', type = 'info') {
  265. this.$refs.uToast.show({ title, type });
  266. }
  267. }
  268. };
  269. </script>
  270. <style lang="scss" scoped>
  271. @import url('./myCars.scss');
  272. </style>