daily_riding.js 17 KB

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