openCabinet.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="container-view">
  3. <view class="open-bg-view">
  4. <img class="open-bg" src="https://qiniu.bms16.com/FqNzU4Wh2kry5H0EqC5zeM2thRFg" alt="">
  5. </view>
  6. <view class="explain-view">
  7. <view class="explain-num-view flex-row">
  8. <!-- <text><text class="explain-num">{{empty_door_id}} </text>号仓门已打开</text> -->
  9. <text>仓门已打开</text>
  10. </view>
  11. <view class="explain-text">请存入更换电池,取走满电电池,并关好仓门</view>
  12. <!-- <view class="explain-text">请 归还 / 取出 电池并关上仓门</view> -->
  13. <!-- <view class="explain-text">请取出电池并关上仓门</view> -->
  14. <!-- <view class="explain-text-grap">归还完成后电柜会自动打开可租仓门</view> -->
  15. <!-- <view class="explain-text-grap">自动为您匹配最高电量电池</view> -->
  16. </view>
  17. <!-- 完成弹窗 -->
  18. <view v-if="isOverModal" class="modal-group">
  19. <view class="payment-info-main">
  20. <view class="payment-info-top flex-row">
  21. <img class="check-icon" :src="showtxt.img" alt="">
  22. <!-- <img src="https://qiniu.bms16.com/FmGcOOZZm09nx9lH3lez7D3DYHKn" alt=""> -->
  23. </view>
  24. <view class="open-result-view">
  25. <view class="open-result flex-row">{{showtxt.title}}</view>
  26. <view class="open-result-tip flex-row">{{showtxt.text}}</view>
  27. </view>
  28. <view class="over-btn-view">
  29. <view v-if="status==3" @tap="tapToIndex" class="over-btn flex-row">完成.{{countdown}}</view>
  30. <view v-if="status==4" @tap="connectStore" class="over-btn flex-row">联系门店</view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. const http = require('../../common/http.js');
  38. const config = require('../../common/config.js');
  39. const common = require('../../common/common.js');
  40. var bluetooth = require('../../common/bluetooth.js');
  41. const DF_CAB_INFO_DONE = 10000; //机柜信息传输完成
  42. const app = getApp();
  43. export default {
  44. data() {
  45. return {
  46. isOverModal:false,
  47. orderStatusTimer: null,//查询订单状态
  48. order_sn:'',
  49. showtxt:{
  50. img:'',
  51. title:'',
  52. text:''
  53. },
  54. status:0,
  55. countdown:5,
  56. empty_door_id:'', //空仓(归还仓)编号
  57. full_door_id:'', //满电仓(借出电池仓)编号
  58. phone:'', //换电失败联系电话
  59. cabinetInfo:{},
  60. isShow:false,
  61. intervalTime:null
  62. };
  63. },
  64. /**
  65. * 生命周期函数--监听页面加载
  66. */
  67. onLoad: function(options) {
  68. const me=this
  69. const paramsString = decodeURIComponent(options.pdata)
  70. const cabinetInfoString = decodeURIComponent(options.cabinet_info)
  71. this.cabinetInfo= JSON.parse(cabinetInfoString)||{}
  72. const pData=JSON.parse(paramsString)||{}
  73. this.phone=this.cabinetInfo.link_phone||''
  74. if(this.cabinetInfo.online_status==1){
  75. me.order_sn=pData.order_sn
  76. me.clearTimer()
  77. //机柜直接下发指令开始换电
  78. me.orderStatusTimer = setInterval(function () {
  79. me.getChangeBatteryStatus(me.order_sn)
  80. }, 1200)
  81. setTimeout(function () {
  82. wx.showLoading({
  83. title: '正在换电中,请稍后',
  84. })
  85. }, 200)
  86. }else{
  87. // 离线蓝牙换电
  88. me.sendExchangeCommand({...pData,...this.cabinetInfo})
  89. }
  90. },
  91. /**
  92. * 生命周期函数--监听页面显示
  93. */
  94. onShow: function() {
  95. },
  96. methods: {
  97. getChangeBatteryStatus(order_sn){
  98. const me=this
  99. //定时查询换电状态
  100. http.postApi(config.API_DAY_HIRE_CABINRT_CHANGE_BATTERY_STATUS, { order_sn: order_sn }, resp => {
  101. if (resp.data.code === 200) {
  102. const statusInfo = resp.data.data.statusInfo
  103. const status = statusInfo.status
  104. if (status == 0||status == 1 || status == 2) {
  105. return;
  106. }
  107. me.empty_door_id=
  108. uni.hideLoading();
  109. me.clearTimer()
  110. if (status == 3) {
  111. const _showtxt = {
  112. title:'换电成功',
  113. text:'即将跳转至首页,祝您骑行愉快',
  114. img:'https://qiniu.bms16.com/Fg5C4OVF17Q8p5-mHu7CoFne0Zqp'
  115. }
  116. me.setData({
  117. isOverModal:true,
  118. status:3,
  119. showtxt:_showtxt
  120. })
  121. me.startCountdown()
  122. } else {
  123. const _showtxt = {
  124. title:'换电失败',
  125. text:statusInfo.fail_reason,
  126. img:'https://qiniu.bms16.com/FmGcOOZZm09nx9lH3lez7D3DYHKn'
  127. }
  128. me.setData({
  129. isOverModal:true,
  130. status:4,
  131. showtxt:_showtxt
  132. })
  133. }
  134. }
  135. })
  136. },
  137. // 开始倒计时
  138. startCountdown: function() {
  139. let that = this;
  140. this.intervalTime = setInterval(function() {
  141. if (that.countdown > 1) {
  142. that.setData({
  143. countdown: that.countdown - 1
  144. });
  145. } else {
  146. clearInterval(that.intervalTime); // 清除定时器
  147. that.tapToIndex()
  148. }
  149. }, 1000); // 每隔1000毫秒(1秒)执行一次
  150. },
  151. tapToIndex(){
  152. this.isOverModal=false
  153. this.bluetoothClose()
  154. this.clearTimer()
  155. this.clearIntervalTimer()
  156. uni.reLaunch({
  157. url: '/pages/index/index',
  158. })
  159. },
  160. connectStore(){
  161. const me = this
  162. const phone = this.phone
  163. // const phone = 18170410707
  164. uni.showModal({
  165. content: `您是否要拨打电话${phone}?`,
  166. confirmText: '确定',
  167. success: (res) => {
  168. if (res.confirm) {
  169. me.clearTimer()
  170. uni.makePhoneCall({
  171. phoneNumber: phone,
  172. success() {},
  173. fail() {}
  174. })
  175. }
  176. },
  177. fail: (res) => {}
  178. })
  179. },
  180. clearTimer () {
  181. if (this.orderStatusTimer == null) return
  182. clearInterval(this.orderStatusTimer)
  183. this.orderStatusTimer = null
  184. },
  185. clearIntervalTimer () {
  186. if (this.intervalTime == null) return
  187. clearInterval(this.intervalTime)
  188. this.intervalTime = null
  189. },
  190. sendExchangeCommand(pdata) {
  191. console.log(pdata,'pdata');
  192. var device = {};
  193. device.orderNo = pdata.order_sn;
  194. device.battertNum = pdata.cabbatterysn;
  195. device.empityBoxNo = pdata.empty_door_id;
  196. device.fullBoxNo = pdata.full_door_id;
  197. device.dev_id = this.cabinetInfo.dev_id;
  198. device.serialNum = new Date().getTime().toString();
  199. device.key = this.decodeKey(this.cabinetInfo.bt_sec)
  200. device.mac_id = this.cabinetInfo.bt_mac;
  201. app.globalData.reportData = null;
  202. app.globalData.reponseData = null;
  203. const me = this;
  204. if (bluetooth.isConnected(device.mac_id)) {
  205. // me.loadBluetooth()
  206. bluetooth.sendExchangeCommand(
  207. device.mac_id,
  208. device,
  209. function() {
  210. me.clearTimer();
  211. me.orderStatusTimer = setInterval(function() {
  212. if (app.globalData.reponseData !== null) {
  213. var reponseData = app.globalData.reponseData;
  214. if (reponseData.reponse == 1) {
  215. if (reponseData.state != 1) {
  216. if (reponseData.msg != '上次操作未确认') {
  217. uni.hideLoading();
  218. me.clearTimer();
  219. common.simpleToast(reponseData.msg);
  220. } else {
  221. if (bluetooth.isConnected(device.mac_id)) {
  222. bluetooth.sendCancelCommand(
  223. device.mac_id,
  224. reponseData.serialNo,
  225. function() {},
  226. function() {
  227. me.sendExchangeCommand(pdata);
  228. }
  229. );
  230. } else {
  231. // this.loadBluetooth();
  232. //蓝牙未连接
  233. }
  234. }
  235. }
  236. }
  237. }
  238. if (app.globalData.reportData !== null) {
  239. var reportData = app.globalData.reportData;
  240. if (reportData.report == 1) {
  241. common.simpleToast(reportData.msg);
  242. uni.hideLoading();
  243. me.clearTimer();
  244. if (!me.isShow) {
  245. if(reportData.msg=='换电成功'||reportData.msg=='成功'){
  246. // 换电成功
  247. me.isShow = true;
  248. me.isOverModal=true
  249. me.status=3
  250. me.showtxt = {
  251. title:'换电成功',
  252. text:'即将跳转至首页,祝您骑行愉快',
  253. img:'https://qiniu.bms16.com/Fg5C4OVF17Q8p5-mHu7CoFne0Zqp'
  254. }
  255. // countdown
  256. // 倒计时跳转首页
  257. me.clearTimer();
  258. me.bluetoothClose();
  259. me.startCountdown()
  260. }else{
  261. me.isShow = true;
  262. me.status=4
  263. me.isOverModal=true
  264. me.showtxt = {
  265. title:'换电失败',
  266. text:reportData.msg,
  267. img:'https://qiniu.bms16.com/FmGcOOZZm09nx9lH3lez7D3DYHKn'
  268. }
  269. me.clearTimer();
  270. me.bluetoothClose();
  271. }
  272. }
  273. }
  274. }
  275. }, 2000);
  276. uni.showLoading({
  277. title: '正在换电中,请稍后'
  278. });
  279. },
  280. function() {
  281. //common.simpleToast('发送失败')
  282. uni.showModal({
  283. title: '提示',
  284. confirmText: '重新发送',
  285. content: '请重新发起换电',
  286. success: function(res) {
  287. if (res.confirm) {
  288. me.sendExchangeCommand(pdata);
  289. } else {
  290. uni.navigateBack({
  291. delta: 1
  292. });
  293. }
  294. }
  295. });
  296. }
  297. );
  298. } else {
  299. // this.loadBluetooth();
  300. // 重新获取蓝牙
  301. }
  302. },
  303. bluetoothClose: function() {
  304. bluetooth.closeBluetoothAdapter();
  305. bluetooth.closeDevice(
  306. this.cabinetInfo.bt_mac,
  307. () => {
  308. // this.setData({
  309. // bt_loading: false
  310. // });
  311. },
  312. () => {}
  313. );
  314. bluetooth.offCharacteristicStateChange(this.cabinetInfo.bt_mac, 'home');
  315. bluetooth.offConnectionStateChange(this.cabinetInfo.bt_mac, 'home');
  316. },
  317. decodeKey(str) {
  318. var val = []
  319. for (var i = 0; i < str.length / 2; i++) {
  320. val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16))
  321. }
  322. var str = ""
  323. for (var i = 0; val.length > i; i++) {
  324. str += String.fromCharCode(~val[i] & 0xff)
  325. }
  326. return str
  327. },
  328. }
  329. };
  330. </script>
  331. <style>
  332. @import './openCabinet.css';
  333. </style>