daily_riding.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. var app = getApp()
  2. var interval;
  3. var interval1;
  4. var locationTerval;
  5. var bikelocationTerval;
  6. var settime;
  7. var timeing;
  8. var inter1;
  9. var huancheTime;
  10. import {
  11. BluetoothManager,
  12. BtErrorCode
  13. } from '../../service/BlueMgr'
  14. const bluM = new BluetoothManager;
  15. const util = require('../../utils/utils.js')
  16. Page({
  17. data: {
  18. windowHeight: '',
  19. code: '',
  20. longitude: '',
  21. latitude: '',
  22. order: '',
  23. markers: [], //显示单车、停车的P图标
  24. polygons: [], //区域
  25. id: '', //区域的ID
  26. polygons1: [], //可骑行区域 先用这个变量代表
  27. controls: [], //地图上当前定位的箭头
  28. height: '',
  29. times: '00:00', //当前骑行时间
  30. detail: '', //车辆信息详情
  31. estimate_money: '0.00', //预计金额
  32. minute: '', //骑行总时间秒
  33. data: [],
  34. box_no: '',
  35. screenHeight: '',
  36. change: false,
  37. timing: 5,
  38. isShow: false,
  39. minute1: '',
  40. bikePosition: false,
  41. battery_power:'',
  42. huanche_timeout:0
  43. },
  44. onLoad: function(options) {
  45. var that = this;
  46. wx.showLoading({
  47. title: '加载中...',
  48. mask: true
  49. })
  50. console.log(app.globalData.screenHeight)
  51. that.setData({
  52. screenHeight: app.globalData.screenHeight
  53. })
  54. var date = Date.parse(new Date()) / 1000;
  55. var query = wx.createSelectorQuery();
  56. query.select('.content').boundingClientRect();
  57. query.exec(function(res) {
  58. console.log(res[0].height)
  59. that.setData({
  60. height: res[0].height
  61. });
  62. })
  63. console.log(options)
  64. if (options.order != undefined) {
  65. that.setData({
  66. order: options.order
  67. })
  68. }
  69. if (options.box_no != undefined) {
  70. that.setData({
  71. box_no: options.box_no
  72. })
  73. }
  74. wx.hideLoading();
  75. that.posiLoca();
  76. that.bikePosition();
  77. },
  78. terval: function() {
  79. var that = this;
  80. clearInterval(interval)
  81. clearInterval(huancheTime)
  82. var times = ''; //秒数
  83. var minute = 0; //分钟
  84. var minute1 = '0'; //分钟
  85. var hour = 0; //小时
  86. var hour1 = '0'; //小时
  87. var time = that.data.minute //获取当前已骑行的秒数
  88. console.log(time)
  89. var no_money_time = time;
  90. if(no_money_time<60){
  91. that.setData({huanche_timeout:60-no_money_time})
  92. huancheTime = setInterval(() => {
  93. var no_money_time1 = that.data.huanche_timeout -1
  94. that.setData({huanche_timeout:no_money_time1})
  95. if(no_money_time1==0){
  96. that.setData({huanche_timeout:-1})
  97. clearInterval(huancheTime)
  98. }
  99. }, 1000);
  100. }else{
  101. that.setData({huanche_timeout:-1})
  102. }
  103. if (time > 60) { //秒数大于60 分钟= 秒数/60 秒数= -1*60 40
  104. minute = parseInt(time / 60);
  105. times = time - minute * 60;
  106. //minute代表分钟 times 代表秒数
  107. } else {
  108. minute = parseInt(that.data.minute / 60);
  109. times = time;
  110. }
  111. interval = setInterval(function() {
  112. //定时器 秒数一秒加1
  113. times++
  114. time++
  115. if (times > 59) { //如果秒数大于59 让分钟加1 秒数归0
  116. minute = minute + 1
  117. times = 0
  118. }
  119. if (times < 10) { //如果秒数小于10 加一个0
  120. times = '0' + times
  121. }
  122. if (minute < 10) { //如果分钟小于10 加一个0
  123. minute1 = '0' + minute
  124. } else {
  125. minute1 = minute
  126. }
  127. if (minute > 59) {
  128. //分钟大于59 让小时+1
  129. hour = parseInt(minute / 60)
  130. if (hour < 10) {
  131. hour1 = '0' + hour;
  132. } else {
  133. hour1 = hour;
  134. }
  135. minute1 = minute - hour * 60;
  136. }
  137. if (minute1 < 10 && hour > 0) { //如果分钟小于10 加一个0
  138. minute1 = '0' + minute1
  139. } else {
  140. minute1 = minute1
  141. }
  142. if (hour > 0) {
  143. //如果小时大于0 显示 时分
  144. that.setData({
  145. times: hour1 + ':' + minute1 + ':' + times
  146. })
  147. } else {
  148. //else 显示 分秒
  149. that.setData({
  150. times: minute1 + ':' + times
  151. })
  152. }
  153. var hours = Number(wx.getStorageSync('setting').day_rent_hours), //默认一天可以骑几个小时不收费
  154. money = Number(wx.getStorageSync('setting').per_hours_day_rent_timeout_money), //超出一小时多少钱
  155. capping_money = Number(wx.getStorageSync('setting').day_rent_capping_money), //24小时之内封顶多少钱
  156. rent_money = Number(wx.getStorageSync('setting').day_rent_money); //一天初始钱
  157. var hour2 = Math.ceil(time/60/60)
  158. var day = Math.floor(hour2/24);
  159. //求出当前天数
  160. var day_money = day * capping_money; //算出封顶钱*天数
  161. var hour_s = (hour2 % 24) - hours;//求余 并减去免费骑行时间
  162. if(hour_s<0){ //如果余的时间<0那么就让他为0
  163. hour_s = 0
  164. }
  165. var estimate_money1 = rent_money+hour_s*money; //计算初始钱+超出小时*一小时多少钱
  166. if(estimate_money1<=capping_money){ //如果当前钱数<=封顶钱 那么 赋值的钱就是 封顶钱*天+当前钱
  167. that.setData({ estimate_money: day_money+estimate_money1+'.00'})
  168. }else{//如果大于封顶钱
  169. that.setData({ estimate_money: capping_money+ day_money+'.00' })
  170. }
  171. }, 1000)
  172. },
  173. onReady: function() {
  174. var that = this;
  175. wx.getSystemInfo({
  176. success({
  177. windowHeight
  178. }) {
  179. console.log(windowHeight)
  180. that.setData({
  181. windowHeight
  182. });
  183. }
  184. });
  185. this.dialog = this.selectComponent("#dialog");
  186. },
  187. help: util.throttle(function() {
  188. //点击帮助
  189. wx.navigateTo({
  190. url: '../personal/help/help',
  191. })
  192. },1000),
  193. bikePosition: function () {
  194. //每5秒刷新当前位置
  195. var that = this;
  196. bikelocationTerval = setInterval(function () {
  197. that.bikeLocation()
  198. }, 30000)
  199. },
  200. bikeLocation: function () {
  201. var that = this;
  202. var data = {
  203. bike_no: that.data.code
  204. }
  205. app.request('/order/orderBikePosition', data, 'GET').then(res => {
  206. if (res.statusCode == 200) {
  207. var mark = that.data.markers;
  208. if (that.data.bikePosition) {
  209. mark.pop()
  210. }
  211. that.setData({ markers: mark, battery_power: res.data.battery_power})
  212. var array = {}
  213. array.iconPath = 'http://resource.weilaigo.l4j.cn/dianche.png';
  214. array.width = 28;
  215. array.height = 28;
  216. array.zIndex = 1111;
  217. array.id = 12345;
  218. array.latitude = res.data.latitude;
  219. array.longitude = res.data.longitude;
  220. that.setData({ markers: that.data.markers.concat(array) })
  221. that.setData({bikePosition: true })
  222. }
  223. }).catch(err => {
  224. console.log(err)
  225. })
  226. },
  227. posiLoca: function() {
  228. //每5秒刷新当前位置
  229. var that = this;
  230. locationTerval = setInterval(function() {
  231. wx.getLocation({
  232. type: 'gcj02',
  233. success: (res) => {
  234. console.log('经纬度为:+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
  235. var latitude = res.latitude
  236. var longitude = res.longitude
  237. that.setData({
  238. longitude: longitude,
  239. latitude: latitude
  240. })
  241. },
  242. fail: function(err) {
  243. console.log(err)
  244. }
  245. })
  246. }, 5000)
  247. },
  248. posi: util.throttle(function() {
  249. //点击定位
  250. var that = this;
  251. wx.getLocation({
  252. type: 'gcj02',
  253. success: (res) => {
  254. console.log('经纬度为:+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
  255. var latitude = res.latitude
  256. var longitude = res.longitude
  257. that.setData({
  258. longitude: longitude,
  259. latitude: latitude
  260. })
  261. }
  262. })
  263. },1000),
  264. search: util.throttle(function() {
  265. wx.navigateTo({
  266. url: '../navigation/navigation',
  267. })
  268. },1000),
  269. seek: util.throttle(function() {
  270. //点击响铃
  271. var that = this;
  272. wx.showLoading({
  273. title: '找车中...',
  274. mask: true
  275. })
  276. bluM.connectDeivece(that.data.detail.box_no).then(res => {
  277. console.log('bellbike ===>')
  278. return bluM.bellBike()
  279. }, rej => {
  280. //抛出异常 当前蓝牙不可用
  281. console.log(rej, 123)
  282. app.request('/rent/retry-bell?order_no=' + that.data.order + '&bike_no=' + that.data.code, '', 'GET').then(res => {
  283. console.log(res)
  284. if (res.statusCode == 200) {
  285. res.data.is_ok == true && wx.showToast({
  286. title: '找车成功',
  287. icon: 'none',
  288. duration: 2000
  289. })
  290. res.data.is_ok == false && wx.showToast({
  291. title: '找车失败请重试',
  292. icon: 'none',
  293. duration: 2000
  294. })
  295. }
  296. })
  297. }).then(res => {
  298. console.log(res, 1234)
  299. }, rej => {
  300. console.log(rej, 12345)
  301. app.request('/order/retry-bell-lock?order_no=' + that.data.order + '&bike_no=' + that.data.code, '', 'GET').then(res => {
  302. console.log(res)
  303. if (res.statusCode == 200) {
  304. res.data.is_ok == true && wx.showToast({
  305. title: '找车成功',
  306. icon: 'none',
  307. duration: 2000
  308. })
  309. res.data.is_ok == false && wx.showToast({
  310. title: '找车失败请重试',
  311. icon: 'none',
  312. duration: 2000
  313. })
  314. }
  315. })
  316. })
  317. },1000),
  318. refresh: util.throttle(function() {
  319. //点击刷新
  320. var that = this;
  321. var data = Date.parse(new Date()) / 1000;
  322. that.chushi()
  323. that.terval()
  324. },1000),
  325. chushi: function() {
  326. var that = this;
  327. var date = Date.parse(new Date()) / 1000;
  328. app.request('/rent/use-order?no=' + that.data.order, '', 'GET').then(res => {
  329. console.log(res)
  330. if (res.statusCode == 200) {
  331. if (date - res.data.start_use_bike_time <= 0) {
  332. that.setData({
  333. minute: 0
  334. })
  335. } else {
  336. that.setData({
  337. minute: date - res.data.start_use_bike_time
  338. })
  339. }
  340. that.terval()
  341. wx.showToast({
  342. title: '刷新成功',
  343. icon: 'none'
  344. })
  345. that.setData({
  346. data: res.data,
  347. code: res.data.bike_no,
  348. })
  349. app.request('/bike/' + that.data.code, '', 'GET').then(res => {
  350. console.log(res)
  351. 200 == res.statusCode && that.setData({
  352. battery_power: res.data.battery_power,
  353. box_no:res.data.box_no
  354. })
  355. })
  356. if (res.data.status == 2) {
  357. wx.reLaunch({
  358. url: '../payment/payment?order=' + res.data.no + '&rent=rent'
  359. });
  360. clearInterval(interval);
  361. clearInterval(interval1);
  362. clearInterval(locationTerval);
  363. clearInterval(bikelocationTerval);
  364. }
  365. that.bikeLocation();
  366. }
  367. }).catch(err => {
  368. console.log(err)
  369. })
  370. },
  371. close_bike:util.throttle(function() {
  372. var that = this;
  373. wx.getLocation({
  374. type: 'gcj02',
  375. success: (res) => {
  376. console.log('经纬度为:+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
  377. var latitude = res.latitude
  378. var longitude = res.longitude
  379. var data = {
  380. bike_no: that.data.code,
  381. order_no: that.data.order,
  382. lat: latitude,
  383. lng: longitude
  384. }
  385. // bluM.connectDeivece(that.data.box_no).then(res => {
  386. // console.log('连接成功')
  387. // bluM.lockBike()
  388. // })
  389. app.request('/rent/close-bike', data, 'POST').then(res => {
  390. if (res.statusCode == 200) {
  391. bluM.connectDeivece(that.data.box_no).then(res => {
  392. console.log('连接成功')
  393. bluM.lockBike()
  394. })
  395. wx.showToast({
  396. title: '锁车成功',
  397. icon: 'none'
  398. })
  399. that.setData({
  400. bolear: 1
  401. })
  402. }
  403. })
  404. }
  405. })
  406. },1000),
  407. open_bike:util.throttle(function() {
  408. var that = this;
  409. wx.getLocation({
  410. type: 'gcj02',
  411. success: (res) => {
  412. console.log('经纬度为:+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
  413. var latitude = res.latitude
  414. var longitude = res.longitude
  415. var data = {
  416. bike_no: that.data.code,
  417. order_no: that.data.order,
  418. lat: latitude,
  419. lng: longitude
  420. }
  421. // bluM.connectDeivece(that.data.box_no).then(res => {
  422. // console.log('连接成功')
  423. // bluM.unlockBike()
  424. // })
  425. app.request('/rent/open-bike', data, 'POST').then(res => {
  426. if (res.statusCode == 200) {
  427. bluM.connectDeivece(that.data.box_no).then(res => {
  428. console.log('连接成功')
  429. bluM.unlockBike()
  430. })
  431. wx.showToast({
  432. title: '开车成功',
  433. icon: 'none'
  434. })
  435. that.setData({
  436. bolear: 1
  437. })
  438. }
  439. })
  440. }
  441. })
  442. },1000),
  443. location: function() {
  444. //获取当前经纬度 给地图赋值
  445. wx.showNavigationBarLoading()
  446. var that = this
  447. that.setData({polygons:[]})
  448. wx.getLocation({
  449. type: 'gcj02',
  450. success: (res) => {
  451. console.log('经纬度为:+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
  452. var latitude = res.latitude
  453. var longitude = res.longitude
  454. that.setData({
  455. longitude: longitude,
  456. latitude: latitude
  457. })
  458. var data = {
  459. 'lat': latitude,
  460. 'lng': longitude
  461. }
  462. app.request('/pages/home?include=setting', data, 'POST').then(res => {
  463. if(res.statusCode==200){
  464. var data = res.data;
  465. var points = data.area_fence;
  466. data.points = points;
  467. data.area_fence = []
  468. data.strokeWidth = 4;
  469. data.strokeColor = '#0000FF';
  470. data.zIndex = 1111;
  471. console.log(data)
  472. that.setData({
  473. polygons: that.data.polygons.concat(data),
  474. id: data.id,
  475. polygons1: that.data.polygons.concat(data)
  476. })
  477. var data1 = {
  478. 'area_id': data.id
  479. }
  480. app.request('/parking/stop-sites', data1, 'GET').then(res => {
  481. // console.log(res)
  482. if (res.statusCode == 200) {
  483. var data = res.data
  484. if (data.length == 0 || data == undefined) {
  485. return;
  486. }
  487. that.setData({
  488. polygons: that.data.polygons1.concat(data.polygons),
  489. markers: data.centres
  490. })
  491. }
  492. })
  493. }
  494. }).catch(err => {
  495. console.log(err)
  496. })
  497. }
  498. })
  499. },
  500. coundDown: function() {
  501. var that = this;
  502. that.setData({
  503. isShow: true
  504. })
  505. timeing = setInterval(function() {
  506. var timeing1 = that.data.timing;
  507. timeing1--
  508. that.setData({
  509. timing: timeing1
  510. })
  511. if (timeing1 <= 0) {
  512. that.setData({
  513. timing: 5,
  514. isShow: false
  515. })
  516. clearInterval(timeing)
  517. }
  518. }, 1000)
  519. },
  520. bindregionchange: function(e) {
  521. //实现大头针移动选点
  522. var that = this;
  523. if (e.type == 'begin') {
  524. clearTimeout(settime)
  525. clearInterval(locationTerval);
  526. }
  527. if (e.type == 'end') {
  528. settime = setTimeout(function() {
  529. that.posiLoca()
  530. }, 3000)
  531. }
  532. },
  533. onShow: function() {
  534. this.location();
  535. this.chushi();
  536. this.posi();
  537. },
  538. end:util.throttle(function() {
  539. var that = this;
  540. wx.showLoading({
  541. title: '结算中...',
  542. mask: true
  543. })
  544. wx.getLocation({
  545. type: 'gcj02',
  546. success: (resa) => {
  547. console.log('经纬度为:+++++++++++++++++++++' + resa.latitude + '++++++++' + resa.longitude)
  548. var latitude = resa.latitude
  549. var longitude = resa.longitude
  550. var data = {
  551. bike_no: that.data.code,
  552. order_no: that.data.order,
  553. lat: latitude,
  554. lng: longitude
  555. }
  556. app.request('/rent/check-is-riding-area', data, 'POST').then(res => {
  557. console.log(res)
  558. if (res.statusCode == 200) {
  559. // clearInterval(interval);
  560. // clearInterval(interval1);
  561. clearInterval(locationTerval);
  562. clearInterval(bikelocationTerval);
  563. wx.hideNavigationBarLoading(), wx.hideLoading()
  564. if (res.data.is_close_order == false) {
  565. // that.closeLock(data)
  566. wx.navigateTo({
  567. url: '/pages/confirm_order/confirm_order?bike_no='+that.data.code+'&order_no='+that.data.order+'&lat='+latitude+'&lng='+longitude+'&order=rent',
  568. })
  569. }else{
  570. app.request('/rent/close-order', data, 'POST').then(res => {
  571. console.log(res)
  572. if (res.statusCode == 200) {
  573. clearInterval(interval);
  574. clearInterval(interval1);
  575. clearInterval(locationTerval);
  576. clearInterval(bikelocationTerval);
  577. wx.hideLoading()
  578. bluM.connectDeivece(that.data.box_no).then(res => {
  579. //关锁成功 调用蓝牙关锁
  580. console.log('lockbike')
  581. return bluM.lockBike()
  582. }, reject => {})
  583. wx.reLaunch({
  584. url: '../payment/payment?order=' + res.data.no+ '&rent=rent'
  585. })
  586. } else if (res.statusCode == 450) {
  587. that.coundDown()
  588. }
  589. // wx.hideNavigationBarLoading()
  590. }).catch(err => {
  591. console.log(err)
  592. })
  593. }
  594. } else if (res.statusCode == 450) {
  595. that.coundDown()
  596. wx.hideLoading()
  597. }
  598. }).catch(err => {
  599. console.log(err)
  600. })
  601. }
  602. })
  603. },1000),
  604. onHide: function() {
  605. },
  606. onUnload: function() {
  607. clearInterval(locationTerval)
  608. clearTimeout(settime)
  609. clearInterval(bikelocationTerval)
  610. },
  611. onPullDownRefresh: function() {
  612. },
  613. onReachBottom: function() {
  614. },
  615. onShareAppMessage: function() {
  616. }
  617. })