scanBtn.vue 28 KB

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