scanBtn.vue 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. <template>
  2. <view class="container-view">
  3. <view style="height: 160rpx;"></view>
  4. <view @tap="sacnBtn" class="refund-btn"><text>扫码换电</text></view>
  5. <view v-if="isModelCenter" class="model_center">
  6. <image class="pack_model" src="https://zxappfile.bms16.com/zx_client/buy_pack_model.png" />
  7. <view v-if="packType==0" class="pack_model_text">您还未购买换电套餐,需先完成购买后再扫码领取电池</view>
  8. <view v-if="packType==2" class="pack_model_text">您还未完成押金授权,现在前往押金授权?</view>
  9. <view v-if="packType==0" class="look_package" @tap="clickLookPackage">查看套餐</view>
  10. <view v-if="packType==2" class="look_package" @tap="clickMyPackage">去押金认证</view>
  11. <view class="line_view"></view>
  12. <image @tap="isModelCenter = false" class="close_package"
  13. src="https://zxappfile.bms16.com/zx_client/close_package.png"></image>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. // var http = require('../../common/http.js');
  19. var http = require('../../common/request.js');
  20. // var config = require('../../common/config.js');
  21. var config = require('../../common/config_gyq.js');
  22. var common = require('../../common/common.js');
  23. var storage = require('../../common/storage.js')
  24. var user = require('../../common/user.js');
  25. // var bluetooth = require('../../common/bluetooth.js');
  26. // var IndexImpl = require('../../pages/index/model/indexImpl.js');
  27. const DF_CAB_INFO_DONE = 10000; //机柜信息传输完成
  28. var subscribeTimer = null;
  29. export default {
  30. props: {
  31. listData:{
  32. type: null,
  33. validator: val => typeof val === 'object' || val === null,
  34. required: true
  35. },
  36. cab_info: {
  37. type: null,
  38. validator: val => typeof val === 'object' || val === null,
  39. required: true
  40. },
  41. dev_id: {
  42. type: String,
  43. required: true
  44. },
  45. is_my: {
  46. type: Boolean,
  47. required: false
  48. }
  49. },
  50. data() {
  51. return {
  52. car_info:{},
  53. isModelCenter: false,
  54. packType:0,
  55. is_battery: false,
  56. free_price: 0,
  57. battery_info: {},
  58. packInfo: {},
  59. reservation_info: [],
  60. myPackageInfo: {},
  61. countdownTimer: {},
  62. blueInfo: {}, //连接蓝牙需要数据
  63. isBluetooth: false, //缓存蓝牙连接状态
  64. cabinetInfo: {}, //机柜信息
  65. num: 0, //免费换电次数
  66. payType: 0, //支付方式
  67. scan_dev_id: '', //扫码机柜编号
  68. online_status: '', //机柜在线状态
  69. isOpenBluetooth: false,
  70. license_plate_number: '',
  71. isShowToBuy: false,
  72. isShowAppoint: false,
  73. wallet_money: 0,
  74. payResp: {},
  75. myLocation: {},
  76. orderInfo: {},
  77. carInfo: {},
  78. is_retuen_battery: false, //判断按钮点击还电还是换电操作
  79. orderStatusTimer: null, //定时查询还电状态
  80. showScanBtn: false,
  81. }
  82. }
  83. /**
  84. * 生命周期函数--监听页面加载
  85. */
  86. ,
  87. watch: {
  88. cab_info: {
  89. handler: function(newVal) {
  90. if (newVal) {
  91. console.log(newVal, "newVal")
  92. this.setData({
  93. reservation_info: JSON.stringify(newVal) == '{}' ? [] : newVal
  94. .reservation_info
  95. });
  96. // this.updateCountdown();
  97. }
  98. },
  99. immediate: true
  100. }
  101. },
  102. mounted: function(options) {
  103. this.loadMyPackageInfo()
  104. const car_list = uni.getStorageSync('user_car_list') || null
  105. const storedLocation = uni.getStorageSync('user_current_location');
  106. this.car_info = uni.getStorageSync('car_info') || {};
  107. if (car_list) {
  108. // this.license_plate_number = car_list.plate_number
  109. this.carInfo = car_list
  110. }
  111. let app = getApp();
  112. if (app.globalData.showScanBtn) {
  113. this.showScanBtn = true
  114. } else {
  115. this.showScanBtn = false
  116. }
  117. if (storedLocation && storedLocation.longitude && storedLocation.latitude) {
  118. // 如果本地有存储的定位信息,则直接使用
  119. this.setData({
  120. myLocation: storedLocation,
  121. // license_plate_number: car_list.plate_number
  122. });
  123. }
  124. //#ifdef MP-ALIPAY
  125. this.payType = 2
  126. //#endif
  127. //#ifdef MP-WEIXIN
  128. this.payType = 0
  129. //#endif
  130. // bluetooth.initBluetooth()
  131. // this.bluetoothClose()
  132. // this.loadBatteryInfo()
  133. //this.loadExchangeInfo()
  134. },
  135. /**
  136. * 生命周期函数--监听页面显示
  137. */
  138. onShow: function() {
  139. },
  140. methods: {
  141. clickLookPackage() {
  142. wx.navigateTo({
  143. url: '/pages/batteryPackage/batteryPackage'
  144. })
  145. },
  146. clickMyPackage() {
  147. wx.navigateTo({
  148. url: '/pages/my/myPackage/myPackage'
  149. })
  150. },
  151. // 更新倒计时
  152. // updateCountdown() {
  153. // if (this.reservation_info.length > 0) {
  154. // console.log(this.reservation_info[0])
  155. // const remainingTime = common.calculateRemainingTime(this.reservation_info[0]
  156. // .lock_expire_time);
  157. // if (remainingTime >= 0) {
  158. // this.setData({
  159. // countdownTimer: common.formatTimeScan(remainingTime)
  160. // })
  161. // }
  162. // if (remainingTime > 0) {
  163. // subscribeTimer = setTimeout(() => this.updateCountdown(), 1000); // 每秒更新一次
  164. // }
  165. // }
  166. // },
  167. loadMyPackageInfo() {
  168. // IndexImpl.loadMyPackageInfo(
  169. // (resp) => {
  170. // this.setData({
  171. // isShowAppoint: resp.isShowAppoint,
  172. // myPackageInfo: resp.myPackageInfo,
  173. // packInfo: resp.packInfo
  174. // })
  175. // this.$emit(
  176. // 'popPackageModel', this.packInfo
  177. // );
  178. // },
  179. // (failMsg) => {
  180. // //common.simpleToast(failMsg)
  181. // }
  182. // )
  183. },
  184. // navToGuild() {
  185. // const url = this.is_battery ? '/pages/my/leadReturnBattery/leadReturnBattery' :
  186. // '/pages/exchangeGuide/exchangeGuide'
  187. // uni.navigateTo({
  188. // url: url
  189. // });
  190. // },
  191. isTagCodeFn(){
  192. let userBattery = []
  193. let boxBattery = []
  194. let tagCode = []
  195. for (let index = 0; index < this.listData.user_battery_list.length; index++) {
  196. let item = this.listData.user_battery_list[index];
  197. userBattery.push(item.tag_info)
  198. }
  199. for (let index = 0; index < this.listData.boxList.length; index++) {
  200. let item = this.listData.boxList[index];
  201. if(item.tag_info) boxBattery.push(item.tag_info)
  202. }
  203. for (let index = 0; index < userBattery.length; index++) {
  204. let item = userBattery[index];
  205. for (var i = 0; i < boxBattery.length; i++) {
  206. var items = boxBattery[i];
  207. if(item.child_tag_code == items.child_tag_code && item.child_tag_code){
  208. tagCode.push(item.child_tag_code)
  209. }else if(item.main_tag_code == items.main_tag_code && item.main_tag_code){
  210. tagCode.push(item.main_tag_code)
  211. }
  212. }
  213. }
  214. return tagCode
  215. },
  216. loadIsLogin() {
  217. uni.navigateTo({
  218. url: '/pages/loginRegister/login',
  219. })
  220. },
  221. async sacnBtn() {
  222. const user_token = storage.getUserToken()
  223. if (!user_token) {
  224. this.loadIsLogin()
  225. return
  226. }
  227. await this.loadBatteryInfo()
  228. if(this.isTagCodeFn().length < 1){
  229. common.simpleToast('未找到可更换的电池!')
  230. return
  231. }
  232. if((this.listData.user_battery_list.length - this.listData.can_exchange_num) <= 0 && this.listData.can_exchange_num === 0 && this.listData.can_exchange_num != -1){
  233. this.packType = 0
  234. this.isModelCenter = true
  235. return
  236. }
  237. uni.showLoading({
  238. title:' 加载中...'
  239. })
  240. let res = await uni.scanCode({
  241. onlyFromCamera: true,
  242. scanType: ['qrCode'],
  243. });
  244. uni.hideLoading()
  245. var cabinet_dev_id = '';
  246. if(res[0]) return
  247. res = res[1]
  248. this.loadGeneralQRData(res)
  249. // if ('path' in res && res.path) {
  250. // if (res.path.split('%26devid%3D').length > 1) {
  251. // cabinet_dev_id = res.path.split('%26devid%3D')[1].split('%26')[0];
  252. // } else if (res.path.split('&devid=').length > 1) {
  253. // cabinet_dev_id = res.path.split('&devid=')[1].split('&')[0];
  254. // }
  255. // }
  256. // if (cabinet_dev_id != '') {
  257. // this.scan_dev_id = cabinet_dev_id
  258. // this.loadNowCabinetDetail(cabinet_dev_id)
  259. // } else {
  260. // common.simpleToast("未找到相应的机柜")
  261. // }
  262. },
  263. sacnBtnReturn() {
  264. const me = this
  265. uni.showModal({
  266. cancelText: '取消',
  267. confirmText: '确定',
  268. content: '您确定要归还电池吗?',
  269. showCancel: true,
  270. title: '提示',
  271. success: (result) => {
  272. if (result.confirm) {
  273. me.is_retuen_battery = true
  274. uni.scanCode({
  275. onlyFromCamera: true,
  276. scanType: [],
  277. success: function(res) {
  278. var cabinet_dev_id = '';
  279. me.loadGeneralQRData(res)
  280. if ('path' in res && res.path) {
  281. if (res.path.split('%26devid%3D').length > 1) {
  282. cabinet_dev_id = res.path.split('%26devid%3D')[1]
  283. .split('%26')[0];
  284. } else if (res.path.split('&devid=').length > 1) {
  285. cabinet_dev_id = res.path.split('&devid=')[1].split(
  286. '&')[0];
  287. }
  288. }
  289. if (cabinet_dev_id != '') {
  290. me.scan_dev_id = cabinet_dev_id
  291. me.loadNowCabinetDetail(cabinet_dev_id)
  292. } else {
  293. //common.simpleToast("未找到相应的机柜")
  294. }
  295. },
  296. fail: function(res) {},
  297. complete: function(res) {}
  298. });
  299. }
  300. },
  301. })
  302. },
  303. loadGeneralQRData(options, stash_sn) {
  304. let qrCode = getApp().globalData.qrCode;
  305. var url = ''
  306. if (qrCode.indexOf("https://zx.uwenya.cc/xcx/s") != -1) {
  307. getApp().globalData.qrCode = ''
  308. url = qrCode
  309. }
  310. if (('result' in options) && options.result.indexOf("https://zx.uwenya.cc/xcx/s") != -1) {
  311. url = decodeURIComponent(options.result);
  312. }
  313. if (url == '') return
  314. var obj = this.getUrlParams(url)
  315. if (('t' in obj) && ('d' in obj)) {
  316. if (obj.t == 1) {
  317. this.scan_dev_id = obj.d
  318. this.loadNowCabinetDetail(obj.d)
  319. return
  320. }
  321. }
  322. },
  323. getUrlParams(url) {
  324. // 通过 ? 分割获取后面的参数字符串
  325. let urlStr = url.split('?')[1]
  326. // 创建空对象存储参数
  327. let obj = {};
  328. // 再通过 & 将每一个参数单独分割出来
  329. let paramsArr = urlStr.split('&')
  330. for (let i = 0, len = paramsArr.length; i < len; i++) {
  331. // 再通过 = 将每一个参数分割为 key:value 的形式
  332. let arr = paramsArr[i].split('=')
  333. obj[arr[0]] = arr[1];
  334. }
  335. return obj
  336. },
  337. cancelReservation(reservation_order_sn) {
  338. const pData = {
  339. order_sn: reservation_order_sn
  340. }
  341. http.postApi(config.API_DAYHIRE_RESERVATION_CANCEL, pData, (resp) => {
  342. if (resp.data.code === 200) {
  343. common.simpleToast("取消预约成功")
  344. this.$emit(
  345. 'refreshCabinet'
  346. );
  347. // this.loadCabinetDetail()
  348. } else {
  349. common.simpleToast(resp.data.msg)
  350. }
  351. })
  352. },
  353. tapReservation() {
  354. // 预约换电
  355. if (this.packInfo.packType === 0||this.packInfo.packType === 2) {
  356. const notShow = 1
  357. this.$emit(
  358. 'popPackageModel', this.packInfo,notShow
  359. );
  360. return
  361. }
  362. if (this.myPackageInfo.effective.list.length === 0) {
  363. common.simpleToast("暂无可预约的电池")
  364. return
  365. }
  366. const pData = {
  367. dev_id: this.dev_id,
  368. battery_sn: this.myPackageInfo.effective.list[0].current_battery_sn
  369. }
  370. if (this.packInfo.packType === 0||this.packInfo.packType === 2) {
  371. const notShow = 1
  372. this.$emit(
  373. 'popPackageModel', this.packInfo,notShow
  374. );
  375. } else{
  376. http.postApi(config.API_DAYHIRE_CABINET_EXCHANGE_RESERVATION, pData, (resp) => {
  377. if (resp.data.code === 200) {
  378. common.simpleToast("预约成功")
  379. this.$emit(
  380. 'refreshCabinet'
  381. );
  382. } else if (resp.data.code === 181102) {
  383. const me = this
  384. uni.showModal({
  385. cancelText: '关闭',
  386. confirmText: '取消预约',
  387. content: '您当前已存在预约其它机柜,是否要取消之前的预约操作?',
  388. showCancel: true,
  389. title: '提示',
  390. success: (result) => {
  391. if (result.confirm) {
  392. me.cancelReservation(resp.data.data.order_sn)
  393. }
  394. },
  395. })
  396. } else {
  397. common.simpleToast(resp.data.msg)
  398. }
  399. })
  400. }
  401. },
  402. async loadNowCabinetDetail(dev_id) {
  403. this.dev_id = dev_id
  404. let battery_sn_list = this.listData.user_battery_list.map(item=>{
  405. return item.battery.battery_sn
  406. })
  407. let {data} = await http.postApi(config.API_FLK_CABINET_CHANGE_BATTERY,{
  408. car_sn:this.car_info.car_sn,
  409. dev_id,
  410. pay_type:0,
  411. battery_sn_list,
  412. })
  413. if(data.code == 200){
  414. uni.navigateTo({
  415. url:`/pages/openCabinet/openCabinet?order_sn=${data.data.order_sn}`
  416. })
  417. }else{
  418. common.simpleToast(data.msg)
  419. }
  420. console.log(data)
  421. // await http.postApi(config.API_FLK_CABINET_CHANGE_BATTERY,{
  422. // car_sn:'',
  423. // dev_id:'',
  424. // pay_type:'',
  425. // from:'',
  426. // battery_sn_list:'',
  427. // })
  428. // const timeNow = Date.now()
  429. // this.scan_dev_id = dev_id
  430. // //扫码机柜信息
  431. // const me = this
  432. // const pData = {
  433. // longitude: this.myLocation.longitude,
  434. // latitude: this.myLocation.latitude,
  435. // dev_id: dev_id || '',
  436. // service: "reservation",
  437. // time: timeNow
  438. // }
  439. // http.postApi(config.API_DAYHIRE_CABINRT_CABINRT_INFO, pData, (resp) => {
  440. // if (resp.data.code === 200) {
  441. // this.online_status = resp.data.data.cabinetInfo.online_status
  442. // const device = {
  443. // device_type: "LSCabinet",
  444. // bt_type: "",
  445. // mac_id: resp.data.data.cabinetInfo.bt_mac,
  446. // btid: resp.data.data.cabinetInfo.bt_mac,
  447. // dev_id: resp.data.data.cabinetInfo.dev_id,
  448. // key: me.decodeKey(resp.data.data.cabinetInfo.bt_sec),
  449. // btkey: resp.data.data.cabinetInfo.bt_mac,
  450. // bt_sec: me.decodeKey(resp.data.data.cabinetInfo.bt_sec),
  451. // bt_mac: resp.data.data.cabinetInfo.bt_mac
  452. // }
  453. // me.setData({
  454. // blueInfo: device,
  455. // cabinetInfo: resp.data.data.cabinetInfo
  456. // })
  457. // // me.updateCountdown()
  458. // me.bluetoothClose()
  459. // if (me.online_status == 1) {
  460. // if (me.is_retuen_battery) {
  461. // //机柜还电流程
  462. // me.noBluetoothBack()
  463. // } else {
  464. // if (me.packInfo.last_num == 0&&me.packInfo.num != 0) {
  465. // if (me.free_price != 0) {
  466. // me.setData({
  467. // isShowToBuy: true
  468. // })
  469. // me.walletInfo()
  470. // } else{
  471. // const pData = {
  472. // from: from,
  473. // battery_sn: this.is_battery ? this.battery_info.battery_sn : this.packInfo.current_battery_sn,
  474. // dev_id: this.scan_dev_id,
  475. // pay_type: 9
  476. // }
  477. // const me = this
  478. // //#ifdef MP-ALIPAY
  479. // const from = 'ali'
  480. // //#endif
  481. // //#ifdef MP-WEIXIN
  482. // const from = 'wx'
  483. // //#endif
  484. // http.postApi(config.API_CABINET_CHANGE_BATTERY2, pData, (resp) => {
  485. // if (resp.data.code === 200) {
  486. // // 钱包支付不需要支付直接换电 有换电次数可以直接换电
  487. // if (!resp.data.data.need_pay) {
  488. // me.orderInfo = {
  489. // order_sn: resp.data.data.order_sn,
  490. // empty_door_id: resp.data.data.empty_door_id,
  491. // full_door_id: resp.data.data.full_door_id,
  492. // cabbatterysn: resp.data.data.rtn_battery_sn || ''
  493. // };
  494. // me.navOpenCabinet(me.orderInfo)
  495. // } else {
  496. // me.setData({
  497. // isShowToBuy: false
  498. // })
  499. // me.wxPayPrice = resp.data.data.price
  500. // me.payResp = resp
  501. // me.doPayBattery({})
  502. // }
  503. // } else {
  504. // common.simpleToast(resp.data.msg)
  505. // }
  506. // })
  507. // }
  508. // } else {
  509. // me.toPayOrFreeExchange()
  510. // }
  511. // }
  512. // } else {
  513. // me.setData({
  514. // isOpenBluetooth: true
  515. // })
  516. // }
  517. // } else {
  518. // common.simpleToast(resp.data.msg)
  519. // }
  520. // })
  521. },
  522. walletInfo() {
  523. const me = this
  524. http.postApi(config.API_DAYHIRE_USER_WALLET_INFO, {}, function(resp) {
  525. if (resp.data.code === 200) {
  526. me.wallet_money = (resp.data.data.balance / 100).toFixed(2)
  527. } else {
  528. common.simpleToast(resp.data.msg)
  529. }
  530. })
  531. },
  532. toPayOrFreeExchange() {
  533. //购买单次换电次数进行换电或者有免费次数直接换电
  534. const me = this
  535. //#ifdef MP-ALIPAY
  536. const from = 'ali'
  537. //#endif
  538. //#ifdef MP-WEIXIN
  539. const from = 'wx'
  540. //#endif
  541. const pData = {
  542. from: from,
  543. battery_sn: this.is_battery ? this.battery_info.battery_sn : this.packInfo.current_battery_sn,
  544. dev_id: this.scan_dev_id,
  545. pay_type: this.payType
  546. }
  547. //如果在线用机柜换电 不在线用蓝牙换电
  548. http.postApi(config.API_CABINET_CHANGE_BATTERY2, pData, (resp) => {
  549. if (resp.data.code === 200) {
  550. // 钱包支付不需要支付直接换电 有换电次数可以直接换电
  551. if (!resp.data.data.need_pay) {
  552. me.orderInfo = {
  553. order_sn: resp.data.data.order_sn,
  554. empty_door_id: resp.data.data.empty_door_id,
  555. full_door_id: resp.data.data.full_door_id,
  556. cabbatterysn: resp.data.data.rtn_battery_sn || ''
  557. };
  558. me.navOpenCabinet(me.orderInfo)
  559. } else {
  560. me.setData({
  561. isShowToBuy: false
  562. })
  563. me.wxPayPrice = resp.data.data.price
  564. me.payResp = resp
  565. me.doPayBattery({})
  566. }
  567. } else {
  568. common.simpleToast(resp.data.msg)
  569. }
  570. })
  571. },
  572. async loadBatteryInfo() {
  573. const me = this
  574. let resp = await http.postApi(config.API_CAR_DEVICE_LIST, {})
  575. if (resp.data.code === 200) {
  576. console.log(resp.data)
  577. if (resp.data.data !== null) {
  578. me.setData({
  579. battery_info: resp.data.data.list,
  580. is_battery: true
  581. })
  582. // me.$emit(
  583. // 'updateBatteryInfo', resp.data.data
  584. // );
  585. } else {
  586. me.setData({
  587. // battery_info: resp.data.data,
  588. is_battery: false
  589. })
  590. }
  591. } else {
  592. common.simpleToast(resp.data.msg)
  593. }
  594. },
  595. doPayBattery: function(pData) {
  596. const me = this
  597. //#ifdef MP-WEIXIN
  598. var payParams = JSON.parse(this.payResp.data.data.payParams);
  599. var order_sn = this.payResp.data.data.order_sn;
  600. user.wxPay(order_sn, payParams, function(isSuccess) {
  601. if (isSuccess) {
  602. common.simpleToast('支付成功')
  603. //跳转换电流程页面
  604. me.orderInfo = {
  605. order_sn: me.payResp.data.data.order_sn,
  606. empty_door_id: me.payResp.data.data.empty_door_id,
  607. full_door_id: me.payResp.data.data.full_door_id,
  608. cabbatterysn: me.payResp.data.data.rtn_battery_sn || ''
  609. };
  610. me.navOpenCabinet(me.orderInfo)
  611. } else {
  612. user.cancelCabExPay(order_sn)
  613. // 取消支付
  614. }
  615. });
  616. //#endif
  617. //#ifdef MP-ALIPAY
  618. my.tradePay({
  619. tradeNO: me.payResp.data.data.trade_no,
  620. success: function(res) {
  621. if (res.resultCode == 9000) {
  622. common.simpleToast('支付成功')
  623. me.orderInfo = {
  624. order_sn: me.payResp.data.data.order_sn,
  625. empty_door_id: me.payResp.data.data.empty_door_id,
  626. full_door_id: me.payResp.data.data.full_door_id,
  627. cabbatterysn: me.payResp.data.data.rtn_battery_sn || ''
  628. };
  629. me.navOpenCabinet(me.orderInfo)
  630. } else {
  631. user.cancelCabExPay(order_sn)
  632. }
  633. },
  634. });
  635. //#endif
  636. },
  637. tapOpenBluetooth(time = null) {
  638. const me = this
  639. const device = me.blueInfo
  640. // 蓝牙换电按钮 加载蓝牙
  641. console.log(111111111);
  642. this.loadBluetooth()
  643. },
  644. loadBluetooth() {
  645. const me = this;
  646. console.log(this.blueInfo,'this.blueInfo');
  647. const device = this.blueInfo;
  648. if (bluetooth.acceptDevice(device)) {
  649. // 打开蓝牙连接
  650. bluetooth.openBluetoothAdapter((res) => {
  651. common.loading()
  652. bluetooth.connectDevice(device, () => {
  653. bluetooth.onCharacteristicStateChange(device.mac_id, 'index', (data) => {
  654. if (JSON.stringify(data) != '{}') {
  655. if (data.state === DF_CAB_INFO_DONE) {
  656. me.reportCabintInfo(me.cabinetInfo.dev_id, data
  657. .commandList);
  658. uni.hideLoading();
  659. common.simpleToast('蓝牙连接成功')
  660. me.setData({
  661. isOpenBluetooth: false,
  662. isBluetooth: true
  663. });
  664. me.bindReturnOrExchange() //还电or换电
  665. }
  666. }
  667. });
  668. bluetooth.onConnectionStateChange(device.mac_id, 'index', (res) => {
  669. uni.hideLoading();
  670. if (!res.connected) {
  671. // 蓝牙未连接
  672. // common.simpleToast('蓝牙连接断开',2000)
  673. me.setData({
  674. isOpenBluetooth: true,
  675. isBluetooth: false
  676. });
  677. } else {
  678. common.simpleToast('蓝牙连接成功', 2000)
  679. // 蓝牙已连接
  680. me.setData({
  681. isOpenBluetooth: false,
  682. isBluetooth: true
  683. });
  684. me.bindReturnOrExchange() //还电or换电
  685. }
  686. });
  687. bluetooth.sendGetCabinetInfoCommand(
  688. device.mac_id,
  689. device,
  690. (res) => {
  691. common.loading();
  692. },
  693. (res) => {
  694. console.log(res,"蓝牙连接1")
  695. me.setData({
  696. isOpenBluetooth: false
  697. });
  698. uni.showModal({
  699. title: '提示',
  700. confirmText: '重新连接',
  701. content: '连接失败,请尝试重新连接',
  702. success: function(res) {
  703. if (res.confirm) {
  704. me.loadBluetooth();
  705. } else {
  706. // uni.navigateBack({
  707. // delta: 1
  708. // });
  709. }
  710. }
  711. });
  712. }
  713. );
  714. },
  715. (res) => {
  716. uni.hideLoading();
  717. var showContent = ""
  718. var text;
  719. //#ifdef MP-ALIPAY
  720. text = '支付宝'
  721. //#endif
  722. //#ifdef MP-WEIXIN
  723. text = '微信'
  724. //#endif
  725. if (res && ("errCode" in res)) {
  726. if (res.errCode == 9000001) {
  727. var showContent= "观察周围是否有其他骑手连接,请等待对方完成 或 "+ text +"是否开启了蓝牙权限!!"
  728. } else {
  729. console.log(res,"蓝牙连接2")
  730. var showContent = "连接失败,请尝试重新连接"
  731. }
  732. } else {
  733. console.log(res,"蓝牙连接3")
  734. var showContent = "连接失败,请尝试重新连接"
  735. }
  736. me.setData({
  737. isOpenBluetooth: false,
  738. isBluetooth: false
  739. });
  740. uni.showModal({
  741. title: '提示',
  742. confirmText: '重新连接',
  743. content: showContent,
  744. success: function(res) {
  745. if (res.confirm) {
  746. me.loadBluetooth();
  747. } else {
  748. // uni.navigateBack({
  749. // delta: 1
  750. // });
  751. }
  752. }
  753. });
  754. }, (res) => {
  755. uni.hideLoading();
  756. me.setData({
  757. isOpenBluetooth: false
  758. });
  759. uni.showModal({
  760. title: '提示',
  761. confirmText: '我知道了',
  762. content: '蓝牙未打开或请在右上角设置授权小程序使用蓝牙',
  763. success: function(res) {
  764. if (res.confirm) {
  765. me.loadBluetooth();
  766. } else {
  767. // uni.navigateBack({
  768. // delta: 1
  769. // });
  770. }
  771. }
  772. });
  773. }
  774. );
  775. }, );
  776. } else {
  777. //蓝牙连接未成功
  778. uni.hideLoading();
  779. uni.showModal({
  780. confirmText: '我知道了',
  781. content: '当前机柜未找到符合的蓝牙类型',
  782. showCancel: false,
  783. title: '提示',
  784. complete: (res) => {
  785. // uni.navigateBack({
  786. // delta: 1
  787. // });
  788. }
  789. });
  790. }
  791. },
  792. reportCabintInfo(dev_id, list) {
  793. var pushList = []
  794. for (var i = 0; list.length > i; i++) {
  795. var sublist = []
  796. for (var j = 0; list[i].length > j; j++) {
  797. sublist.push(parseInt(list[i][j]))
  798. }
  799. pushList.push(sublist)
  800. }
  801. const pData = {
  802. dev_id: dev_id,
  803. data: JSON.stringify(pushList)
  804. }
  805. const me = this
  806. http.postApi(config.API_CABINET_BLUETOOTH_INFO, pData, function(response) {
  807. if (response.data.code === 200) {
  808. // me.setData({
  809. // cabinetInfo: response.data.data.cabinetInfo
  810. // })
  811. } else {
  812. simpleToast(response.data.msg)
  813. }
  814. })
  815. },
  816. navOpenCabinet(pdata, battery_type) {
  817. const me = this
  818. const paramsString = JSON.stringify(pdata)
  819. const cabinetInfoString = JSON.stringify(me.cabinetInfo)
  820. // 跳转机柜换电页面-展示换电流程和结果
  821. uni.navigateTo({
  822. url: '/pages/openCabinetBind/openCabinetBindSn?pdata=' + encodeURIComponent(paramsString) +
  823. '&cabinet_info=' + encodeURIComponent(cabinetInfoString) + '&battery_type=' + battery_type,
  824. success: function(res) {},
  825. fail: function(res) {},
  826. complete: function(res) {},
  827. })
  828. },
  829. // loadExchangeInfo() {
  830. // // 查询免费换电次数/换电价格
  831. // const pData = {
  832. // license_plate_number: this.license_plate_number
  833. // }
  834. // http.postApi(config.API_DAYHIRE_CABINRT_BATTERY_EXCHANGE_INFO, pData, (resp) => {
  835. // if (resp.data.code === 200) {
  836. // this.setData({
  837. // num: resp.data.data.last_free_number,
  838. // free_price: (resp.data.data.price / 100).toFixed(2),
  839. // })
  840. // } else {
  841. // common.simpleToast(resp.data.msg)
  842. // }
  843. // })
  844. // },
  845. bluetoothClose: function() {
  846. bluetooth.closeBluetoothAdapter();
  847. bluetooth.closeDevice(
  848. this.cabinetInfo.bt_mac,
  849. () => {
  850. // this.setData({
  851. // bt_loading: false
  852. // });
  853. },
  854. () => {}
  855. );
  856. bluetooth.offCharacteristicStateChange(this.cabinetInfo.bt_mac, 'home');
  857. bluetooth.offConnectionStateChange(this.cabinetInfo.bt_mac, 'home');
  858. },
  859. changePayType(e) {
  860. const type = e.currentTarget.dataset.type
  861. if (this.wallet_money < this.free_price) {
  862. common.simpleToast('钱包余额不足')
  863. return
  864. }
  865. this.setData({
  866. payType: type
  867. })
  868. console.log(this.free_price, 'free_price');
  869. },
  870. close() {
  871. this.setData({
  872. isOpenBluetooth: false
  873. })
  874. },
  875. claseShowToBuy() {
  876. this.setData({
  877. isShowToBuy: false
  878. })
  879. },
  880. decodeKey(str) {
  881. var val = []
  882. for (var i = 0; i < str.length / 2; i++) {
  883. val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16))
  884. }
  885. var str = ""
  886. for (var i = 0; val.length > i; i++) {
  887. str += String.fromCharCode(~val[i] & 0xff)
  888. }
  889. return str
  890. },
  891. noBluetoothBack() {
  892. common.loading();
  893. const me = this
  894. const pData = {
  895. battery_sn: this.battery_info.battery_sn,
  896. dev_id: this.scan_dev_id
  897. }
  898. http.postApi(config.API_CABINET_RETURN_HIRE_BATTERYI, pData, resp => {
  899. uni.hideLoading()
  900. if (resp.data.code === 200) {
  901. const pData = {
  902. order_sn: resp.data.data.order_sn,
  903. battery_sn: me.battery_info.battery_sn,
  904. }
  905. me.navOpenCabinet(pData, 1)
  906. } else {
  907. common.alert('提示', resp.data.msg);
  908. }
  909. })
  910. },
  911. bindReturnOrExchange() {
  912. const me = this
  913. console.log(me.is_retuen_batterym, 'is_retuen_battery');
  914. if (me.is_retuen_battery) {
  915. common.loading();
  916. const me = this
  917. const pdata = {
  918. battery_sn: this.battery_info.battery_sn,
  919. dev_id: this.scan_dev_id
  920. }
  921. console.log(pdata, 'pdatapdata');
  922. http.postApi(config.API_CABINET_BLUETOOTH_RETURN, pdata, resp => {
  923. wx.hideLoading()
  924. console.log(resp.data, 'resp.data');
  925. if (resp.data.code === 200) {
  926. console.log(resp.data.data, '1314');
  927. const pData = {
  928. order_sn: resp.data.data.order_sn,
  929. empty_door_id: resp.data.data.empty_door_id,
  930. rtn_battery_ca: resp.data.data.rtn_battery_ca || ''
  931. }
  932. //还电
  933. me.navOpenCabinet(pData, 1)
  934. } else {
  935. common.alert('提示', resp.data.msg);
  936. }
  937. })
  938. } else {
  939. //换电
  940. if (me.packInfo.last_num == 0&&me.packInfo.num != 0) {
  941. if (me.free_price != 0) {
  942. me.setData({
  943. isShowToBuy: true
  944. })
  945. me.walletInfo()
  946. } else{
  947. const pData = {
  948. from: from,
  949. battery_sn: this.is_battery ? this.battery_info.battery_sn : this.packInfo.current_battery_sn,
  950. dev_id: this.scan_dev_id,
  951. pay_type: 9
  952. }
  953. const me = this
  954. //#ifdef MP-ALIPAY
  955. const from = 'ali'
  956. //#endif
  957. //#ifdef MP-WEIXIN
  958. const from = 'wx'
  959. //#endif
  960. http.postApi(config.API_CABINET_CHANGE_BATTERY2, pData, (resp) => {
  961. if (resp.data.code === 200) {
  962. // 钱包支付不需要支付直接换电 有换电次数可以直接换电
  963. if (!resp.data.data.need_pay) {
  964. me.orderInfo = {
  965. order_sn: resp.data.data.order_sn,
  966. empty_door_id: resp.data.data.empty_door_id,
  967. full_door_id: resp.data.data.full_door_id,
  968. cabbatterysn: resp.data.data.rtn_battery_sn || ''
  969. };
  970. me.navOpenCabinet(me.orderInfo)
  971. } else {
  972. me.setData({
  973. isShowToBuy: false
  974. })
  975. me.wxPayPrice = resp.data.data.price
  976. me.payResp = resp
  977. me.doPayBattery({})
  978. }
  979. } else {
  980. common.simpleToast(resp.data.msg)
  981. }
  982. })
  983. }
  984. } else {
  985. me.toPayOrFreeExchange()
  986. }
  987. }
  988. },
  989. }
  990. };
  991. </script>
  992. <style>
  993. @import './scanBtn.css';
  994. </style>