home.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. const {
  2. formatGlDate,
  3. platform,
  4. cutDownTime,
  5. formatTs,
  6. getDayList
  7. } = require('../../utils/util.js')
  8. const { postGeoCoder } = require('../../api/common')
  9. const { fetchOrderCount, fetchDistanceInfo } = require('./api/index')
  10. const { sessionStorageKey } = require('../../api/request')
  11. const { one, two, three } = formatGlDate()
  12. const app = getApp()
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. sessionId: '',
  19. navBarHeight: app.globalData.navBarHeight,
  20. navBarConfig: {
  21. booFixed: true,
  22. backgroundColor: '',
  23. icon: 'home',
  24. iconPath: '/common/nav_logo@2x.png',
  25. label: '',
  26. title: ''
  27. },
  28. introduceList: [
  29. {
  30. icon: '/home/Frame_291@2x.png',
  31. name: '正规车辆'
  32. },
  33. {
  34. icon: '/home/Home_icon_professional@2x.png',
  35. name: '专业司机'
  36. },
  37. {
  38. icon: '/home/Home_icon_standard@2x.png',
  39. name: '标准定价'
  40. },
  41. {
  42. icon: '/home/Home_icon_bus@2x.png',
  43. name: '车型丰富'
  44. }
  45. ],
  46. originLocation: {},
  47. travelerPopup: false,
  48. isAutoFocus: false,
  49. form: {
  50. 'start_point': {
  51. 'longitude': 0, // 经度
  52. 'latitude': 0, // 纬度
  53. 'address_name': '', // 起点位置名称
  54. 'name': ''
  55. }, // 起点
  56. 'end_point': {
  57. 'longitude': 0, // 经度
  58. 'latitude': 0, // 纬度
  59. 'address_name': '', // 起点位置名称
  60. 'name': ''
  61. }, // 终点
  62. 'order_pass_location': [], // 途径地点
  63. 'order_user_num': '', // 乘车用户数量
  64. 'order_start_time': '', // 开始时间
  65. 'order_end_time': '无返程', // 返程时间
  66. 'order_car': [], // 从订单详情跳转至创建订单页使用参数:车辆信息
  67. 'order_car_action': '', // 车辆用途
  68. 'driver_cost': {}, // 司机费用
  69. 'order_remark': '', // 订单备注
  70. 'gaosu_distance': 0, // 出发的高速里程(米)
  71. 'order_time': 0, // 出发的耗时(分)
  72. 'order_distance': 0, // 出发的总里程(米)
  73. 'gaosu_distance_back': 0, // 返程的高速里程(米)
  74. 'order_time_back': 0, // 返程的耗时(分)
  75. 'order_distance_back': 0 // 返程的总里程(米)
  76. },
  77. booGoTimePicker: false,
  78. multiIndexGoTimePicker: [0, 0, 0],
  79. booBackTimePicker: false,
  80. multiIndexBackTimePicker: [0, 0, 0],
  81. booLock: false,
  82. objOrderCount: {
  83. // 'count': 0, // 订单数量
  84. // 'id': '' // 订单ID(当数量为1时存在)
  85. },
  86. startColumns: [
  87. {
  88. values: [],
  89. defaultIndex: 0
  90. },
  91. {
  92. values: [],
  93. defaultIndex: 0
  94. },
  95. {
  96. values: [],
  97. defaultIndex: 0
  98. }
  99. ],
  100. endColumns: [
  101. {
  102. values: [],
  103. defaultIndex: 0
  104. },
  105. {
  106. values: [],
  107. defaultIndex: 0
  108. },
  109. {
  110. values: [],
  111. defaultIndex: 0
  112. }
  113. ]
  114. },
  115. tempFormKey: '', // 用于当前所在城市、选择出发、途径地、终点的字段名,默认为空:为空表示当前所在城市
  116. tempPostData: {},
  117. tempStartOptions: {},
  118. tempEndOptions: {},
  119. /**
  120. * 生命周期函数--监听页面加载
  121. */
  122. onLoad: async function (options) {
  123. this.tempFormKey = ''
  124. await this.setForm()
  125. await this.getDistance()
  126. // this.handleGetSetting()
  127. },
  128. bindCallBack() {
  129. // this.handleGetSetting()
  130. this.getOrderCount()
  131. this.getDistance()
  132. },
  133. // 从订单详情页进入首页时
  134. async init() {
  135. await this.setForm()
  136. await this.getDistance()
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow: function () {
  147. this.setData({
  148. sessionId: wx.getStorageSync(sessionStorageKey)
  149. })
  150. this.getOrderCount()
  151. },
  152. /**
  153. * 生命周期函数--监听页面隐藏
  154. */
  155. onHide: function () {
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload: function () {
  161. },
  162. /**
  163. * 用户点击右上角分享
  164. */
  165. onShareAppMessage: function () {
  166. return {
  167. title: '来吧上车,我们一起去看看',
  168. path: '/pages/home/home',
  169. imageUrl: 'https://bashi-1311374120.cos.ap-shanghai.myqcloud.com/wxMini/image/common/friend500x400.png'
  170. }
  171. },
  172. onPageScroll({ scrollTop }) {
  173. let backgroundColor = ''
  174. if (Math.abs(scrollTop) >= Math.floor(this.data.navBarHeight / 2)) {
  175. backgroundColor = 'rgba(106, 134, 177)'
  176. }
  177. if (backgroundColor === this.data.navBarConfig.backgroundColor) {
  178. return
  179. }
  180. this.setData({
  181. 'navBarConfig.backgroundColor': backgroundColor
  182. })
  183. },
  184. // 获取定位权限、定位权限授权
  185. async handleGetSetting() {
  186. const that = this
  187. if (that.tempFormKey === '') {
  188. // 清除缓存定位数据
  189. wx.removeStorageSync(app.globalData.homeMyCityKeyOfStorage)
  190. }
  191. try {
  192. const { errMsg, authSetting } = await wx.getSetting()
  193. if (errMsg === 'getSetting:ok') {
  194. // 有定位授权
  195. if (authSetting['scope.userLocation']) {
  196. if (that.tempFormKey === '') {
  197. } else {
  198. await that.chooseLocationBridge()
  199. }
  200. return
  201. }
  202. }
  203. } catch (err) {}
  204. try {
  205. await wx.authorize({ scope: 'scope.userLocation' })
  206. if (that.tempFormKey === '') {
  207. } else {
  208. await that.chooseLocationBridge()
  209. }
  210. } catch (err) {
  211. wx.showModal({
  212. title: '提示',
  213. content: '未开启获取地理位置权限,去设置中打开',
  214. success(res) {
  215. if (res.confirm) {
  216. that.openSetting()
  217. }
  218. }
  219. })
  220. }
  221. },
  222. // 去小程序自带设置页:返回
  223. async openSetting() {
  224. try {
  225. const openSettingData = await wx.openSetting()
  226. // 开启了定位权限
  227. if (openSettingData.authSetting['scope.userLocation']) {
  228. if (this.tempFormKey === '') {
  229. } else {
  230. await this.chooseLocationBridge()
  231. }
  232. }
  233. } catch (err) {}
  234. },
  235. // 经纬度解析为地址
  236. async getCity() {
  237. const { latitude, longitude } = this.data.originLocation
  238. let city = '定位失败'
  239. try {
  240. const { status, data } = await postGeoCoder(latitude, longitude)
  241. if (status) {
  242. const { address_component } = data
  243. city = address_component.city
  244. }
  245. } catch (err) {}
  246. this.setData({
  247. 'navBarConfig.label': city
  248. })
  249. wx.setStorage({
  250. key: app.globalData.homeMyCityKeyOfStorage,
  251. data: city
  252. })
  253. },
  254. async handleChooseLocation(e) {
  255. const { lat, lon, formkey } = e.currentTarget.dataset
  256. this.tempPostData = lat && lon ? {
  257. latitude: lat,
  258. longitude: lon
  259. } : {}
  260. this.tempFormKey = /^order_pass_location/.test(formkey) ? 'form.order_pass_location[' +
  261. formkey.split('-')[1] * 1 + ']' : 'form.' + formkey
  262. await this.handleGetSetting()
  263. },
  264. async chooseLocationBridge() {
  265. if (!this.tempFormKey) {
  266. return
  267. }
  268. try {
  269. const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(
  270. this.tempPostData)
  271. if (errMsg === 'chooseLocation:ok' && address && name) {
  272. const pointInfo = {
  273. address_name: address,
  274. name,
  275. latitude,
  276. longitude
  277. }
  278. if (this.checkSimilarPoint(this.tempFormKey, pointInfo)) {
  279. wx.showModal({
  280. title: '提示',
  281. content: '起点、途经点与终点不能相同~',
  282. showCancel: false
  283. })
  284. return
  285. }
  286. this.setData({
  287. [this.tempFormKey]: pointInfo
  288. }, () => {
  289. this.getDistance()
  290. })
  291. }
  292. } catch (err) {
  293. // 点击了取消按钮
  294. if (err.errMsg === 'chooseLocation:fail cancel') {
  295. }
  296. // 未开启授权
  297. if (err.errMsg === 'chooseLocation:fail auth deny') {
  298. }
  299. }
  300. },
  301. // 检查起点、途经点、终点是否相同了:判断依据相邻的2个点不同就可以了
  302. checkSimilarPoint(formKey, value) {
  303. const { start_point, order_pass_location, end_point } = this.data.form
  304. let start = JSON.parse(JSON.stringify(start_point))
  305. let passLocation = JSON.parse(JSON.stringify(order_pass_location))
  306. let end = JSON.parse(JSON.stringify(end_point))
  307. let arr = []
  308. let isSimilar = false
  309. if (formKey.indexOf('start_point') > -1) {
  310. start = value
  311. }
  312. if (formKey.indexOf('end_point') > -1) {
  313. end = value
  314. }
  315. // formKey:form.order_pass_location[0]
  316. if (formKey.indexOf('order_pass_location') > -1) {
  317. const index = formKey.replace(/.*\[|]$/g, '')
  318. passLocation[index * 1] = value
  319. }
  320. arr = [].concat(start, ...passLocation, end)
  321. arr = arr.filter(item => item.address_name)
  322. if (arr.length > 1) {
  323. for (let i = 1; i < arr.length; i++) {
  324. if (arr[i].address_name === arr[i - 1].address_name) {
  325. isSimilar = true
  326. break
  327. }
  328. }
  329. }
  330. return isSimilar
  331. },
  332. addWay() {
  333. let temp = JSON.parse(JSON.stringify(this.data.form.order_pass_location))
  334. if (this.data.form.order_pass_location.length > 4) {
  335. wx.showToast({
  336. title: '最多可添加5个途经点',
  337. icon: 'none'
  338. })
  339. return
  340. }
  341. temp.push({ address_name: '', name: '', latitude: 0, longitude: 0 })
  342. this.setData({
  343. 'form.order_pass_location': temp
  344. })
  345. },
  346. delWay(e) {
  347. const { index } = e.currentTarget.dataset
  348. let temp = JSON.parse(JSON.stringify(this.data.form.order_pass_location))
  349. temp.splice(index, 1)
  350. this.setData({
  351. 'form.order_pass_location': temp
  352. })
  353. },
  354. async navBarEvent() {
  355. wx.navigateTo({
  356. url: '/pages/selectCity/selectCity'
  357. })
  358. },
  359. onClose() {
  360. this.setData({
  361. travelerPopup: false
  362. })
  363. },
  364. showTravelerPopup() {
  365. this.setData({
  366. travelerPopup: true
  367. })
  368. setTimeout(() => {
  369. this.setData({
  370. isAutoFocus: true
  371. })
  372. }, 500)
  373. },
  374. bindInput(e) {
  375. return {
  376. value: e.detail.value.replace(/[^\d$]/, '').replace(/^0/, '')
  377. }
  378. },
  379. initColumnData(pickerType, val0 = {}, val1 = {}) {
  380. const date = new Date()
  381. const curMS = date.getTime()
  382. const minMS = curMS + 2 * 60 * 60 * 1000
  383. // 每一天的23:50:00必须大于当前时间加2小时
  384. const firstColumn = one.filter(item => {
  385. const temp = platform().ios ? item._text.replace(/-/g, '/') : item._text
  386. return new Date(`${temp} 23:50:00`).getTime() > minMS
  387. })
  388. if (pickerType === 'end_point') {
  389. firstColumn.unshift({
  390. value: '10',
  391. text: '无返程',
  392. _text: '无返程'
  393. })
  394. }
  395. let YYYYIndex = firstColumn.findIndex(item => item.value === val0.value)
  396. if (YYYYIndex === -1) {
  397. YYYYIndex = 0
  398. }
  399. let YYYYMMDD = firstColumn[YYYYIndex]._text
  400. if (/^\d{4}/.test(YYYYMMDD) && platform().ios) {
  401. YYYYMMDD = YYYYMMDD.replace(/-/g, '/')
  402. }
  403. const secondColumn = /^\d{4}/.test(YYYYMMDD) ? two.filter(item => {
  404. return new Date(`${YYYYMMDD} ${item._text}:50:00`).getTime() > minMS
  405. }) : []
  406. let HHIndex = secondColumn.findIndex(item => item.value === val1.value)
  407. if (HHIndex === -1) {
  408. HHIndex = 0
  409. }
  410. const thirdColumn = /^\d{4}/.test(YYYYMMDD) ? three.filter(item => {
  411. const str = `${YYYYMMDD} ${secondColumn[HHIndex]._text}:${item._text}:00`
  412. return new Date(str).getTime() > minMS
  413. }) : []
  414. return {
  415. firstColumn,
  416. secondColumn,
  417. thirdColumn
  418. }
  419. },
  420. setStartColumnsDefaultIndex(value) {
  421. let startOptions = {}
  422. let index0 = 0
  423. let index1 = 0
  424. let index2 = 0
  425. if (Object.prototype.toString.call(value) === '[object Number]' && value > 0) {
  426. const { YYYY, MM, DD, HH, mm } = formatTs(value)
  427. const YYYYMMDD = `${YYYY}-${MM}-${DD}`
  428. const val0 = one.filter(item => item._text === YYYYMMDD)
  429. const val1 = two.filter(item => item._text === HH)
  430. startOptions = this.initColumnData('start_point', val0[0], val1[0])
  431. index0 = startOptions.firstColumn.findIndex(item => item._text === YYYYMMDD)
  432. index1 = startOptions.secondColumn.findIndex(item => item._text === HH)
  433. index2 = startOptions.thirdColumn.findIndex(item => item._text === mm)
  434. } else {
  435. startOptions = this.initColumnData('start_point')
  436. }
  437. if (index0 === -1) {
  438. index0 = 0
  439. }
  440. if (index1 === -1) {
  441. index1 = 0
  442. }
  443. if (index2 === -1) {
  444. index2 = 0
  445. }
  446. this.tempStartOptions = {
  447. firstColumn: startOptions.firstColumn,
  448. secondColumn: startOptions.secondColumn,
  449. thirdColumn: startOptions.thirdColumn
  450. }
  451. this.setData({
  452. multiIndexGoTimePicker: [index0, index1, index2],
  453. 'startColumns[0].values': startOptions.firstColumn,
  454. 'startColumns[0].defaultIndex': index0,
  455. 'startColumns[1].values': startOptions.secondColumn,
  456. 'startColumns[1].defaultIndex': index1,
  457. 'startColumns[2].values': startOptions.thirdColumn,
  458. 'startColumns[2].defaultIndex': index2
  459. }, () => {
  460. const instance = this.selectComponent('#van-picker-start_time')
  461. instance && instance.setIndexes && instance.setIndexes(this.data.multiIndexGoTimePicker)
  462. })
  463. },
  464. setEndColumnsDefaultIndex(value) {
  465. let endOptions = {}
  466. let index0 = 0
  467. let index1 = 0
  468. let index2 = 0
  469. if (Object.prototype.toString.call(value) === '[object Number]' && value > 0) {
  470. const { YYYY, MM, DD, HH, mm } = formatTs(value)
  471. const YYYYMMDD = `${YYYY}-${MM}-${DD}`
  472. const val0 = one.filter(item => item._text === YYYYMMDD)
  473. const val1 = two.filter(item => item._text === HH)
  474. endOptions = this.initColumnData('end_point', val0[0], val1[0])
  475. index0 = endOptions.firstColumn.findIndex(item => item._text === YYYYMMDD)
  476. index1 = endOptions.secondColumn.findIndex(item => item._text === HH)
  477. index2 = endOptions.thirdColumn.findIndex(item => item._text === mm)
  478. } else {
  479. endOptions = this.initColumnData('end_point')
  480. }
  481. if (index0 === -1) {
  482. index0 = 0
  483. }
  484. if (index1 === -1) {
  485. index1 = 0
  486. }
  487. if (index2 === -1) {
  488. index2 = 0
  489. }
  490. this.tempEndOptions = {
  491. firstColumn: endOptions.firstColumn,
  492. secondColumn: endOptions.secondColumn,
  493. thirdColumn: endOptions.thirdColumn
  494. }
  495. this.setData({
  496. multiIndexBackTimePicker: [index0, index1, index2],
  497. 'endColumns[0].values': endOptions.firstColumn,
  498. 'endColumns[0].defaultIndex': index0,
  499. 'endColumns[1].values': endOptions.secondColumn,
  500. 'endColumns[1].defaultIndex': index1,
  501. 'endColumns[2].values': endOptions.thirdColumn,
  502. 'endColumns[2].defaultIndex': index2
  503. }, () => {
  504. const instance = this.selectComponent('#van-picker-back_time')
  505. instance && instance.setIndexes && instance.setIndexes(this.data.multiIndexBackTimePicker)
  506. })
  507. },
  508. showGoTimePicker() {
  509. this.setData({ booGoTimePicker: true })
  510. },
  511. bindChangeGoTimePicker(e) {
  512. const { picker, value, index } = e.detail
  513. const options = this.initColumnData('start_point', value[0], value[1])
  514. if (index === 0) {
  515. picker.setColumnValues(1, options.secondColumn)
  516. picker.setColumnValues(2, options.thirdColumn)
  517. } else if (index === 1) {
  518. picker.setColumnValues(2, options.thirdColumn)
  519. } else {
  520. picker.setColumnValues(2, options.thirdColumn)
  521. }
  522. },
  523. getValueGoTimePicker() {
  524. const instance = this.selectComponent('#van-picker-start_time')
  525. const indexes = instance.getIndexes()
  526. const values = instance.getValues()
  527. const HH = values.length >= 2 ? values[1]._text : ''
  528. const mm = values.length === 3 ? values[2]._text : ''
  529. let YYYYMMDD = values.length >= 1 ? values[0]._text : ''
  530. if (platform().ios) {
  531. YYYYMMDD = YYYYMMDD.replace(/-/g, '/')
  532. }
  533. if (['02', '03', '04'].findIndex(item => item === HH) > -1 || (HH === '05' && mm === '00')) {
  534. wx.showModal({
  535. title: '提示',
  536. content: '凌晨2点~5点高速不通行,请选择其它时间~',
  537. showCancel: false
  538. })
  539. return
  540. }
  541. this.tempStartOptions = {
  542. firstColumn: instance.getColumnValues(0),
  543. secondColumn: instance.getColumnValues(1),
  544. thirdColumn: instance.getColumnValues(2)
  545. }
  546. this.setData({
  547. 'form.order_start_time': new Date(`${YYYYMMDD} ${HH}:${mm}:00`).getTime(),
  548. multiIndexGoTimePicker: indexes
  549. }, () => {
  550. this.onCloseGoTimePicker()
  551. })
  552. },
  553. onCloseGoTimePicker() {
  554. const startOptions = this.tempStartOptions
  555. this.setData({
  556. booGoTimePicker: false,
  557. 'startColumns[0].values': startOptions.firstColumn,
  558. 'startColumns[1].values': startOptions.secondColumn,
  559. 'startColumns[2].values': startOptions.thirdColumn
  560. }, () => {
  561. const instance = this.selectComponent('#van-picker-start_time')
  562. instance.setIndexes(this.data.multiIndexGoTimePicker)
  563. })
  564. },
  565. showBackTimePicker() {
  566. if (!(this.data.form.start_point.address_name) || !(this.data.form.end_point.address_name)) {
  567. wx.showToast({
  568. title: '请选择出发地与目的地',
  569. icon: 'none'
  570. })
  571. return
  572. }
  573. if (!(this.data.form.order_start_time)) {
  574. wx.showToast({
  575. title: '请选择出发时间',
  576. icon: 'none'
  577. })
  578. return
  579. }
  580. this.setData({
  581. booBackTimePicker: true
  582. })
  583. },
  584. bindChangeBackTimePicker(e) {
  585. const { picker, value, index } = e.detail
  586. const options = this.initColumnData('end_point', value[0], value[1])
  587. if (index === 0) {
  588. picker.setColumnValues(1, options.secondColumn)
  589. picker.setColumnValues(2, options.thirdColumn)
  590. } else if (index === 1) {
  591. picker.setColumnValues(2, options.thirdColumn)
  592. } else {
  593. picker.setColumnValues(2, options.thirdColumn)
  594. }
  595. },
  596. getValueBackTimePicker() {
  597. const orderStartTime = this.data.form.order_start_time
  598. // 单程耗时
  599. const orderDuration = this.data.form.order_time * 60 * 1000
  600. const instance = this.selectComponent('#van-picker-back_time')
  601. const indexes = instance.getIndexes()
  602. const values = instance.getValues()
  603. let YYYYMMDD = values.length >= 1 ? values[0]._text : ''
  604. const HH = /^\d{4}/.test(YYYYMMDD) && values.length >= 2 ? values[1]._text : ''
  605. const mm = /^\d{4}/.test(YYYYMMDD) && values.length === 3 ? values[2]._text : ''
  606. let formTime = YYYYMMDD
  607. if (orderDuration <= 0) {
  608. wx.showToast({
  609. title: '正在规划路线,请稍后选择返程时间~',
  610. icon: 'none'
  611. })
  612. return
  613. }
  614. if (/^\d{4}/.test(YYYYMMDD) && platform().ios) {
  615. YYYYMMDD = YYYYMMDD.replace(/-/g, '/')
  616. }
  617. if (['02', '03', '04'].findIndex(item => item === HH) > -1 || (HH === '05' && mm === '00')) {
  618. wx.showModal({
  619. title: '提示',
  620. content: '凌晨2点~5点高速不通行,请选择其它时间~',
  621. showCancel: false
  622. })
  623. return
  624. }
  625. if (/^\d{4}/.test(YYYYMMDD)) {
  626. formTime = new Date(`${YYYYMMDD} ${HH}:${mm}:00`).getTime()
  627. const dayList = getDayList(orderStartTime, orderDuration)
  628. const endTs = dayList.length > 0 ? dayList[dayList.length - 1].endTs : orderStartTime
  629. const duration = endTs - orderStartTime
  630. if (formTime < endTs) {
  631. wx.showModal({
  632. title: '提示',
  633. content: `当前时间可能无法返程:去程耗时约${cutDownTime(duration / 1000, 'hh:mm zh')}~`,
  634. showCancel: false
  635. })
  636. return
  637. }
  638. }
  639. this.tempEndOptions = {
  640. firstColumn: instance.getColumnValues(0),
  641. secondColumn: instance.getColumnValues(1),
  642. thirdColumn: instance.getColumnValues(2)
  643. }
  644. this.setData({
  645. 'form.order_end_time': formTime,
  646. multiIndexBackTimePicker: indexes
  647. }, () => {
  648. this.onCloseBackTimePicker()
  649. })
  650. },
  651. onCloseBackTimePicker() {
  652. const endOptions = this.tempEndOptions
  653. this.setData({
  654. booBackTimePicker: false,
  655. 'endColumns[0].values': endOptions.firstColumn,
  656. 'endColumns[1].values': endOptions.secondColumn,
  657. 'endColumns[2].values': endOptions.thirdColumn
  658. }, () => {
  659. const instance = this.selectComponent('#van-picker-back_time')
  660. instance.setIndexes(this.data.multiIndexBackTimePicker)
  661. })
  662. },
  663. bindSubmit(e) {
  664. const {
  665. number
  666. } = e.detail.value
  667. if (!number) {
  668. wx.showToast({
  669. title: '请输入乘车人数',
  670. icon: 'none'
  671. })
  672. return
  673. }
  674. this.setData({
  675. 'form.order_user_num': number,
  676. travelerPopup: false
  677. })
  678. },
  679. async getDistance() {
  680. const that = this
  681. const { start_point, order_pass_location, end_point } = that.data.form
  682. // 去除选择的同一个地点
  683. const orderPassLocation = order_pass_location.filter(item => item.address_name)
  684. const formData = {
  685. start_point,
  686. end_point,
  687. order_pass_location: orderPassLocation
  688. }
  689. that.setData({
  690. 'form.gaosu_distance': 0,
  691. 'form.order_time': 0,
  692. 'form.order_distance': 0,
  693. 'form.gaosu_distance_back': 0,
  694. 'form.order_time_back': 0,
  695. 'form.order_distance_back': 0
  696. })
  697. if (!start_point.address_name) {
  698. return
  699. }
  700. if (!end_point.address_name) {
  701. return
  702. }
  703. try {
  704. const { status, data, msg } = await fetchDistanceInfo(formData)
  705. if (status) {
  706. const {
  707. gaosu_distance,
  708. order_time,
  709. order_distance,
  710. gaosu_distance_back,
  711. order_time_back,
  712. order_distance_back
  713. } = data
  714. that.setData({
  715. 'form.gaosu_distance': gaosu_distance,
  716. 'form.order_time': order_time,
  717. 'form.order_distance': order_distance,
  718. 'form.gaosu_distance_back': gaosu_distance_back,
  719. 'form.order_time_back': order_time_back,
  720. 'form.order_distance_back': order_distance_back
  721. })
  722. } else {
  723. wx.showToast({
  724. title: msg,
  725. icon: 'none'
  726. })
  727. }
  728. } catch (err) {}
  729. },
  730. verify() {
  731. const orderStartTime = this.data.form.order_start_time
  732. const orderDuration = this.data.form.order_time * 60 * 1000
  733. const dayList = getDayList(orderStartTime, orderDuration)
  734. const endTs = dayList.length > 0 ? dayList[dayList.length - 1].endTs : orderStartTime
  735. let errorList = []
  736. if (!(this.data.form.start_point.address_name)) {
  737. errorList.push({ errMsg: '请选择出发地' })
  738. }
  739. if (!(this.data.form.end_point.address_name)) {
  740. errorList.push({ errMsg: '请选择目的地' })
  741. }
  742. if (this.data.form.order_distance <= 1 || this.data.form.order_time <= 1) {
  743. errorList.push({ errMsg: '正在规划路线,请稍后点击下一步~' })
  744. }
  745. if (!(this.data.form.order_user_num)) {
  746. errorList.push({ key: 'order_user_num' })
  747. }
  748. if (!orderStartTime) {
  749. errorList.push({ key: 'order_start_time' })
  750. }
  751. // 返程时间限制:返程时间要大于单程到达时间
  752. if (this.data.form.order_end_time !== '无返程' && this.data.form.order_end_time < endTs) {
  753. errorList.push({ key: 'order_end_time' })
  754. }
  755. return errorList
  756. },
  757. async onsubmit() {
  758. const verifyList = this.verify()
  759. if (verifyList.length) {
  760. const { errMsg, key } = verifyList[0]
  761. if (errMsg) {
  762. wx.showToast({
  763. title: errMsg,
  764. icon: 'none'
  765. })
  766. } else if (key) {
  767. switch (key) {
  768. case 'order_user_num':
  769. this.showTravelerPopup()
  770. break
  771. case 'order_start_time':
  772. this.showGoTimePicker()
  773. break
  774. case 'order_end_time':
  775. this.showBackTimePicker()
  776. break
  777. default:
  778. }
  779. }
  780. return
  781. }
  782. // 去除途径地未完善信息的
  783. // 创建订单数据:存入本地
  784. wx.setStorage({
  785. key: app.globalData.homeCreateOrderKeyOfStorage,
  786. data: JSON.stringify({
  787. ...this.data.form,
  788. 'order_pass_location': this.data.form.order_pass_location.filter(item => item.address_name),
  789. 'order_start_time': Math.round(this.data.form.order_start_time / 1000), // 后端要求时间戳为秒
  790. 'order_end_time': this.data.form.order_end_time === '无返程' ? '' : Math.round(
  791. this.data.form.order_end_time / 1000),
  792. 'is_return_back': this.data.form.order_end_time === '无返程' ? 0 : 1 // 是否返程(0否1是)
  793. })
  794. })
  795. wx.navigateTo({
  796. url: !this.data.sessionId ? '/pages/login/login' : '/pages/createOrder/createOrder'
  797. })
  798. },
  799. jumpInvoice() {
  800. wx.navigateTo({
  801. url: '/pages/invoice/invoice'
  802. })
  803. },
  804. jumpHelp() {
  805. wx.navigateTo({
  806. url: '/pages/help/help'
  807. })
  808. },
  809. jumpFeedback() {
  810. wx.navigateTo({
  811. url: '/pages/feedback/feedback'
  812. })
  813. },
  814. // 设置form的初始值
  815. async setForm() {
  816. const date = new Date()
  817. const curMS = date.getTime()
  818. const minMS = curMS + 2 * 60 * 60 * 1000
  819. const key = app.globalData.homeCreateOrderKeyOfStorage
  820. try {
  821. const { data, errMsg } = await wx.getStorage({ key })
  822. if (errMsg === 'getStorage:ok' && /^{/.test(data)) {
  823. const temp = JSON.parse(data)
  824. for (let key in temp) {
  825. let value = temp[key]
  826. if (this.data.form.propertyIsEnumerable(key)) {
  827. // 检查出发时间是否大于当前时间2小时
  828. if (key === 'order_start_time') {
  829. if (value && value * 1000 > minMS) {
  830. value = value * 1000
  831. } else {
  832. value = ''
  833. }
  834. }
  835. if (key === 'order_end_time') {
  836. if (value && value * 1000 > minMS) {
  837. value = value * 1000
  838. } else {
  839. value = '无返程'
  840. }
  841. }
  842. this.setData({
  843. ['form.' + key]: value
  844. })
  845. }
  846. }
  847. }
  848. } catch (err) {}
  849. const { order_start_time, order_end_time } = this.data.form
  850. this.setStartColumnsDefaultIndex(order_start_time)
  851. this.setEndColumnsDefaultIndex(order_end_time)
  852. },
  853. async getOrderCount() {
  854. try {
  855. const { status, data } = await fetchOrderCount()
  856. if (status) {
  857. this.setData({
  858. objOrderCount: data
  859. })
  860. }
  861. } catch (err) {}
  862. },
  863. jumpOrder() {
  864. const { count, id } = this.data.objOrderCount
  865. if (count === 1) {
  866. wx.navigateTo({
  867. url: '/pages/orderDetail/orderDetail?orderId=' + id
  868. })
  869. return
  870. }
  871. wx.navigateTo({
  872. url: '/pages/order/order'
  873. })
  874. },
  875. jumpCompany() {
  876. wx.navigateTo({
  877. url: '/pages/company/company'
  878. })
  879. }
  880. })