inputLicensePlate.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <view class="container">
  3. <view class="code">
  4. <view class="code-tip flex-row">
  5. <view>输入6位车牌号</view>
  6. <view @tap="clickScan" class="code-scan">
  7. <img src="https://qiniu.bms16.com/FvoYYiKMCkQlUbdANiXUBLx7_vQ1" />
  8. 快捷输入
  9. </view>
  10. </view>
  11. <view class="code-input flex-row">
  12. <view @tap="tapChange"
  13. v-for="(item, index) in plateInputs"
  14. :key="index"
  15. :class="(hideValue.length==index&&hideFocus)?'inputed-item':'input-item'">
  16. <view class="input-text">
  17. {{item.value}}
  18. </view>
  19. </view>
  20. </view>
  21. <view>
  22. <!-- #ifdef MP-ALIPAY-->
  23. <input class="inputStyle-ali" :enableNative="false" @blur="inputBlur" @input="inputChange" :value="hideValue" :focus="hideFocus"
  24. type="text"></input>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <input class="inputStyle-wx" @blur="inputBlur" @input="inputChange" :value="hideValue" :focus="hideFocus"
  28. type="text"></input>
  29. <!-- #endif -->
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. const config = require('../../common/config.js');
  36. const common = require('../../common/common.js');
  37. var http = require('../../common/http.js');
  38. var storage = require('../../common/storage.js');
  39. var OCR = require('../../common/OCR.js');
  40. export default {
  41. data() {
  42. return {
  43. isReturnIndex:true,//是否返回首页
  44. isReturnHome: true,
  45. plateInputs: Array.from({
  46. length: 6
  47. }, () => ({
  48. value: ''
  49. })),
  50. preFullPlateNumber: '',
  51. myLocation: {},
  52. order_sn: '',
  53. order_model_id: '',
  54. hideValue: '',
  55. hideFocus: false,
  56. hiddenInput: false,
  57. };
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad: function(options) {
  63. this.order_sn = options.order_sn ? options.order_sn : ''
  64. this.order_model_id = options.order_model_id ? options.order_model_id : ''
  65. this.isReturnIndex = options.isReturnIndex ? false : true
  66. const storedLocation = uni.getStorageSync('user_current_location');
  67. const me = this
  68. if (storedLocation && storedLocation.longitude && storedLocation.latitude) {
  69. // 如果本地有存储的定位信息,则直接使用
  70. me.myLocation = storedLocation
  71. //#ifdef MP-ALIPAY
  72. setTimeout(function() {
  73. me.hideFocus = true
  74. }, 500);
  75. //#endif
  76. //#ifdef MP-WEIXIN
  77. me.hideFocus = true
  78. //#endif
  79. } else {
  80. setTimeout(function() {
  81. me.getLocationAndSave();
  82. }, 100);
  83. }
  84. },
  85. /**
  86. * 生命周期函数--监听页面显示
  87. */
  88. onShow: function() {
  89. this.isReturnHome = true
  90. },
  91. onUnload: function () {
  92. if(this.isReturnIndex && this.isReturnHome){
  93. uni.switchTab({
  94. url: '/pages/my/my'
  95. });
  96. }else if(!this.isReturnIndex && this.isReturnHome){
  97. uni.switchTab({
  98. url: '/pages/index/index' // 首页
  99. });
  100. }
  101. },
  102. methods: {
  103. inputChange(event) {
  104. this.hideValue = event.detail.value
  105. const regex = /^[0-9a-zA-Z]$/;
  106. const isNumOrLetter = regex.test(this.hideValue)
  107. if (!isNumOrLetter) {
  108. this.hideValue = event.detail.value.replace(/[^0-9a-z]/gi, '')
  109. }
  110. if (this.hideValue.length > 6) return
  111. //限制只输入数字字母
  112. const test = [...this.hideValue]
  113. const testSix=this.padArrayToSix(test)
  114. if (this.hideValue.length > 0) {
  115. let len = this.hideValue.length
  116. if (this.hideValue.length > this.preFullPlateNumber.length) {
  117. // this.plateInputs[len - 1].value = test[len - 1]
  118. // this.plateInputs[len - 1].value = test.slice(0, 6)
  119. for(let i=0; i<this.plateInputs.length;i++){
  120. this.plateInputs[i].value=testSix[i]
  121. }
  122. } else {
  123. this.plateInputs[len].value = ''
  124. }
  125. }
  126. if (this.hideValue.length == 0 && this.preFullPlateNumber.length == 1) {
  127. this.plateInputs[0].value = ''
  128. }
  129. //存储前一个输入结果
  130. this.preFullPlateNumber = this.hideValue
  131. if (this.hideValue.length == 6) {
  132. this.submitPlate()
  133. }
  134. },
  135. tapChange() {
  136. this.hideFocus = true
  137. },
  138. inputBlur() {
  139. this.hideFocus = false
  140. },
  141. getLocationAndSave: function() {
  142. const me = this;
  143. uni.getLocation({
  144. type: 'gcj02',
  145. isHighAccuracy: true,
  146. success: function(res) {
  147. uni.hideLoading();
  148. var myLocation = {
  149. longitude: res.longitude,
  150. latitude: res.latitude
  151. }
  152. me.myLocation = myLocation
  153. uni.setStorageSync('user_current_location', myLocation);
  154. },
  155. fail: function(res) {
  156. uni.hideLoading();
  157. },
  158. complete: function(res) {
  159. uni.hideLoading();
  160. }
  161. });
  162. },
  163. clickScan() {
  164. const me = this
  165. uni.chooseImage({
  166. count: 1,
  167. sizeType: ['compressed'],
  168. sourceType: ['camera'],
  169. success: function(res) {
  170. me.upImage(res);
  171. }
  172. });
  173. },
  174. upImage(res) {
  175. const me = this;
  176. let tempFilePaths;
  177. tempFilePaths = res.tempFilePaths[0];
  178. uni.getFileSystemManager().readFile({
  179. filePath: tempFilePaths,
  180. encoding: 'base64',
  181. success: function(res) {
  182. common.loading();
  183. OCR.scanOCRText(res.data).then((words) => {
  184. uni.hideLoading();
  185. me.setData({
  186. hideValue: words,
  187. });
  188. me.submitPlate()
  189. });
  190. //that.scanImageInfo(res.data); // 调用百度API解析图片获取文字
  191. },
  192. fail: function(res) {
  193. console.log('[读取图片数据fail]', res);
  194. },
  195. complete: function(res) {}
  196. });
  197. },
  198. submitPlate() {
  199. const pData = {
  200. longitude: this.myLocation.longitude,
  201. latitude: this.myLocation.latitude,
  202. plate_number: this.hideValue
  203. }
  204. const me = this
  205. http.postApi(config.API_DAYHIRE_CAR_CAR_INFO, pData, (resp) => {
  206. uni.hideLoading()
  207. if (resp.data.code === 200) {
  208. const timestamp = Date.now(); // 获取当前时间戳(毫秒)
  209. const isOffline = (Math.floor(timestamp / 1000) - resp.data.data.last_comm_time) > 1800
  210. if (resp.data.data.last_comm_time === 0 || isOffline) {
  211. common.simpleToast('此车辆已离线,请选择其他车辆')
  212. } else {
  213. let carInfo = JSON.stringify(resp.data.data)
  214. const car_model = resp.data.data.model_info.car_model
  215. const model_images = resp.data.data.model_info.model_images.split(',')
  216. const return_imgs = resp.data.data.return_imgs
  217. var model_id = resp.data.data.model_info.model_id
  218. if (resp.data.data.has_owner) { //车辆正在被使用
  219. if (resp.data.data.is_mine) { //是本人在使用
  220. wx.showModal({
  221. title: '提示',
  222. content: '已有正在使用的车辆,是否跳转至车辆详情页?',
  223. cancelText: '取消',
  224. confirmText: '确定',
  225. success: function(res) {
  226. this.isReturnHome = false
  227. uni.navigateTo({
  228. url: '/pages/battery/battery?plate_number=' +
  229. me
  230. .hideValue
  231. });
  232. },
  233. fail: function(res) {},
  234. complete: function(res) {},
  235. })
  236. } else {
  237. common.simpleToast('此车辆正在被使用')
  238. }
  239. } else {
  240. if (me.order_sn != '') {
  241. if (this.order_model_id != model_id) { // 预租车型与之前预约车型不一致
  242. common.simpleToast('与预约车型不符')
  243. } else {
  244. this.isReturnHome = false
  245. uni.navigateTo({
  246. url: '/pages/activation/activation?plate_number=' + me
  247. .hideValue + '&order_sn=' + this.order_sn +
  248. '&car_model=' + car_model + '&model_image=' +
  249. model_images[0] + '&return_imgs=' + JSON.stringify(
  250. return_imgs),
  251. fail() {}
  252. })
  253. }
  254. } else {
  255. this.isReturnHome = false
  256. uni.navigateTo({
  257. url: '/pages/carIntroduce/carIntroduce?carInfo=' +
  258. encodeURIComponent(carInfo) + '&plate_number=' + me
  259. .hideValue,
  260. fail() {}
  261. })
  262. }
  263. }
  264. setTimeout(() => {
  265. me.setData({
  266. plateInputs: Array.from({
  267. length: 6
  268. }, () => ({
  269. value: ''
  270. })),
  271. hideValue: '',
  272. preFullPlateNumber: ''
  273. })
  274. }, 500)
  275. }
  276. } else {
  277. common.simpleToast(resp.data.msg)
  278. me.setData({
  279. plateInputs: Array.from({
  280. length: 6
  281. }, () => ({
  282. value: ''
  283. })),
  284. hideValue: '',
  285. preFullPlateNumber: ''
  286. })
  287. }
  288. })
  289. },
  290. navToPage(e) {
  291. const url = e.currentTarget.dataset.url;
  292. if (!url) {
  293. return;
  294. }
  295. uni.navigateTo({
  296. url: url
  297. });
  298. },
  299. padArrayToSix(arr,defaultValue = ''){
  300. return Array.from({ length: 6 }, () => defaultValue).map((_, index) => arr[index] || defaultValue);
  301. }
  302. }
  303. };
  304. </script>
  305. <style>
  306. @import './inputLicensePlate.css';
  307. </style>