creatOrder.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // pages/creatOrder/creatOrder.js
  2. import {
  3. shuoAPIClient
  4. } from '../../common/api/api.js';
  5. import user from '../../common/user/user.js';
  6. var touchstart = 0
  7. var touchend = 0
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. dateList: [],
  14. curr: "",
  15. isMoreDate: false, // 日期框展示
  16. isMask: false,
  17. num: 1,
  18. choosedData: 1,
  19. infoId: null,
  20. year: null,
  21. month: null,
  22. blankDays: [],
  23. prices: null,
  24. price3: null,
  25. selectDate: null, //
  26. prodDetail: null, //产品详情
  27. priceTotal: null, //计算后的总价
  28. linkInfo: [], //游客信息
  29. remark: ''
  30. },
  31. /**
  32. * 下单
  33. */
  34. addOrder() {
  35. if (!user.getMyTokenSync()) {
  36. this.openToast('尚未登录授权,无法下单', 'none')
  37. return false
  38. }
  39. let travelDate = this.data.selectDate
  40. if (!travelDate) {
  41. this.openToast('请选择出游日期', 'none')
  42. return false
  43. }
  44. let linkInfo = this.data.linkInfo
  45. let peoples = []
  46. for (let info of linkInfo) {
  47. if (!info.nameValue) {
  48. this.openToast('游客姓名未输入', 'none')
  49. return false
  50. }
  51. if (!info.phoneValue) {
  52. this.openToast('手机号码未输入', 'none')
  53. return false
  54. }
  55. if (this.checkIdCard() && !info.idCardValue) {
  56. this.openToast('身份证号码未输入', 'none')
  57. return false
  58. }
  59. let people = {
  60. linkMan: info.nameValue,
  61. linkPhone: info.phoneValue
  62. }
  63. if (this.checkIdCard()) {
  64. people.linkCreditType = 0
  65. people.linkCreditNo = info.idCardValue
  66. }
  67. peoples.push(people)
  68. }
  69. // 下单参数
  70. let params = {
  71. infoId: this.data.infoId,
  72. travelDate,
  73. ticketNum: this.data.num,
  74. people: peoples,
  75. linkMan: peoples[0].linkMan,
  76. linkPhone: peoples[0].linkPhone,
  77. linkCreditType: peoples[0].linkCreditType,
  78. linkCreditNo: peoples[0].linkCreditNo,
  79. orderMemo: this.data.remark
  80. }
  81. let data = {
  82. orderCommonRequest: params,
  83. treeId: 0
  84. }
  85. console.log('请求参数json:', data)
  86. wx.showLoading({
  87. title: '提交订单中...',
  88. mask: true
  89. });
  90. shuoAPIClient.request('/api/minapp/order/order',
  91. data, true, 'POST').then(res => {
  92. wx.hideLoading()
  93. console.log('下单返回', res)
  94. if (res.data.state == 2) { // 下单成功,可以立即发起支付
  95. let orderId = res.data.orderId
  96. wx.requestPayment({
  97. 'timeStamp': res.data.timestamp,
  98. 'nonceStr': res.data.noncestr,
  99. 'package': res.data.packages,
  100. 'signType': res.data.signtype,
  101. 'paySign': res.data.paysign,
  102. 'complete': function(res) {
  103. console.log(res, '支付信息')
  104. let isPay = res.errMsg.indexOf('fail') > -1 ? 2 : 1; // 支付失败或取消支付:2, 支付成功:1;
  105. wx.redirectTo({
  106. url: `/pages/orderDetail/orderDetail?orderId=${orderId}&isPay=${isPay}`
  107. });
  108. }
  109. });
  110. } else if (res.data.state == 1) {
  111. let orderId = res.data.orderId
  112. wx.redirectTo({
  113. url: `/pages/orderDetail/orderDetail?orderId=${orderId}`
  114. });
  115. } else {
  116. wx.showModal({
  117. title: '提示',
  118. content: res.data.msg,
  119. showCancel: false
  120. });
  121. }
  122. })
  123. },
  124. /**
  125. * 弹框提示
  126. */
  127. openToast(title, icon) {
  128. wx.showToast({
  129. title: title,
  130. icon: icon,
  131. duration: 2000
  132. })
  133. },
  134. touchstart: function(e) {
  135. console.log('滑动')
  136. console.log(e.changedTouches[0].clientX)
  137. touchstart = e.changedTouches[0].clientX
  138. },
  139. touchend: function(e) {
  140. console.log('停止')
  141. console.log(e)
  142. console.log(e.changedTouches[0].clientX)
  143. touchend = e.changedTouches[0].clientX
  144. if (touchstart - touchend > 60) {
  145. console.log('左滑了')
  146. this.nextMonth()
  147. }
  148. if (touchstart - touchend < -60) {
  149. let today = new Date();
  150. let amonth = today.getMonth() + 1
  151. if (this.data.month > amonth) {
  152. this.lastMonth()
  153. }
  154. }
  155. },
  156. /**
  157. * 生命周期函数--监听页面加载
  158. */
  159. onLoad: function(options) {
  160. console.log('options', options)
  161. let today = new Date();
  162. console.log('today.getMonth()', today.getMonth())
  163. this.setData({
  164. infoId: options.infoId,
  165. month: today.getMonth() + 1,
  166. year: today.getFullYear()
  167. })
  168. this.initDate(this.data.year, this.data.month)
  169. this.blankDays(this.data.year, this.data.month)
  170. this.getProdDetail()
  171. },
  172. /**
  173. * 生命周期函数--监听页面初次渲染完成
  174. */
  175. onReady: function() {
  176. },
  177. onShow: function() {
  178. console.log('creatOrder')
  179. this.getPrice()
  180. },
  181. onUnload: function() {
  182. console.log('onUnload')
  183. if (this.data.linkInfo[0].nameValue || this.data.linkInfo[0].phoneValue || this.data.linkInfo[0].idCardValue) {
  184. wx.showToast({
  185. title: '你曾经说,最大的愿望,就是去旅行',
  186. icon: "none",
  187. duration: 2000
  188. })
  189. }
  190. },
  191. /**
  192. * 初始化日历
  193. */
  194. initDate(year, month) {
  195. let date = new Date(year, month, 0)
  196. let days = date.getDate()
  197. var dateList = []
  198. console.log(this.data.dateList)
  199. var today = new Date()
  200. let day = today.getDate()
  201. console.log(day)
  202. let week = today.getDay()
  203. console.log(today.getDay())
  204. for (var i = 1; i <= days; i++) {
  205. // if (day == i) {
  206. // dateList.push('今天')
  207. // } else if (day + 1 == i) {
  208. // dateList.push('明天')
  209. // } else {
  210. dateList.push(i)
  211. // }
  212. }
  213. this.setData({
  214. dateList: dateList,
  215. day: day,
  216. week: week,
  217. curr: '今天'
  218. })
  219. },
  220. /**
  221. * 当前月第一日前面的空白格
  222. */
  223. blankDays(year, month) {
  224. let date = new Date(year, month - 1, 1)
  225. console.log('date', date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate());
  226. let week = date.getDay();
  227. let blankDays = []
  228. for (let i = 0; i < week; i++) {
  229. blankDays.push("");
  230. }
  231. this.setData({
  232. blankDays: blankDays
  233. })
  234. },
  235. /**
  236. * 登录回调
  237. */
  238. loginCallBack: function(e) {
  239. // console.log('loginCallBack')
  240. // if (getCurrentPages().length != 0) {
  241. // //刷新当前页面的数据
  242. // getCurrentPages()[getCurrentPages().length - 1].onLoad()
  243. // }
  244. },
  245. /**
  246. * 非日历内的选中日期事件
  247. */
  248. chooseDate: function(e) {
  249. console.log(e.currentTarget.dataset.traveldate)
  250. this.setData({
  251. selectDate: e.currentTarget.dataset.traveldate
  252. })
  253. this.priceCalculation()
  254. },
  255. /**
  256. * 数量减少
  257. */
  258. increaseNum: function() {
  259. let num = this.data.num
  260. if (num > 1) {
  261. num--
  262. this.setData({
  263. num: num
  264. })
  265. this.priceCalculation()
  266. this.changeLinkInfo()
  267. }
  268. },
  269. /**
  270. * 数量增加
  271. */
  272. addNum: function() {
  273. let num = this.data.num
  274. num++
  275. this.setData({
  276. num: num
  277. })
  278. this.priceCalculation()
  279. this.changeLinkInfo()
  280. },
  281. /**
  282. * 价格计算
  283. */
  284. priceCalculation: function() {
  285. let num = this.data.num
  286. let selectDate = this.data.selectDate
  287. if (!selectDate) {
  288. return false
  289. }
  290. let prices = this.data.prices
  291. let priceTotal = prices[selectDate].salePrice / 100 * num
  292. this.setData({
  293. priceTotal: priceTotal
  294. })
  295. },
  296. /**
  297. * 上个月
  298. */
  299. lastMonth: function() {
  300. let today = new Date();
  301. let amonth = today.getMonth() + 1
  302. if (this.data.month > amonth) {
  303. let year = this.data.year
  304. let month = this.data.month
  305. if (month == 1) {
  306. year = year - 1
  307. month = 12
  308. } else {
  309. month--
  310. }
  311. this.setData({
  312. year: year,
  313. month: month
  314. })
  315. this.initDate(year, month)
  316. this.blankDays(year, month)
  317. this.getPrice()
  318. }
  319. },
  320. /**
  321. * 下个月
  322. */
  323. nextMonth: function() {
  324. let year = this.data.year
  325. let month = this.data.month
  326. if (month == 12) {
  327. year = year + 1
  328. month = 1
  329. } else {
  330. month++
  331. }
  332. this.setData({
  333. year: year,
  334. month: month
  335. })
  336. this.initDate(year, month)
  337. this.blankDays(year, month)
  338. this.getPrice()
  339. },
  340. /**
  341. * 价格日历内选择日期
  342. */
  343. select: function(e) {
  344. console.log('select', e.currentTarget.dataset.day)
  345. let select = e.currentTarget.dataset.day
  346. let price = this.data.prices[this.data.year + '-' + (this.data.month < 10 ? '0' + this.data.month : this.data.month) + '-' + (select < 10 ? '0' + select : select)]
  347. if (!price) {
  348. return false;
  349. }
  350. this.setData({
  351. curr: select,
  352. selectDate: price.travelDate
  353. })
  354. this.priceCalculation()
  355. let week = new Date().getDay()
  356. let day = select
  357. if (day == '今天') {
  358. day = this.data.day
  359. } else if (day == '明天') {
  360. day = this.data.day + 1
  361. }
  362. week = (week + (day - this.data.day)) % 7
  363. console.log('week' + week)
  364. this.setData({
  365. week: week
  366. })
  367. this.close()
  368. },
  369. /**
  370. * 价格日历开启
  371. */
  372. tapMoreDate: function() {
  373. this.setData({
  374. isMoreDate: true,
  375. isMask: true
  376. })
  377. },
  378. /**
  379. * 价格日历关闭
  380. */
  381. close: function() {
  382. this.setData({
  383. isMoreDate: false,
  384. isMask: false
  385. })
  386. },
  387. /**
  388. * 获取价格
  389. */
  390. getPrice() {
  391. // wx.showLoading({
  392. // title: '加载中...',
  393. // mask: true
  394. // });
  395. shuoAPIClient.request('/api/minapp/ticket/productPrices', {
  396. year: this.data.year,
  397. month: this.data.month,
  398. infoId: this.data.infoId
  399. }, false).then(res => {
  400. // wx.hideLoading()
  401. let prices = {}
  402. console.log('价格', res)
  403. if (res.data && res.data) {
  404. for (let data of res.data) {
  405. prices[data.travelDate] = data
  406. }
  407. if (!this.data.price3) { //如果前三日价格不存在则加载,存在就不变
  408. let price3 = []
  409. for (let i = 0; i < 3; i++) {
  410. if (res.data[i]) {
  411. price3.push(res.data[i])
  412. }
  413. }
  414. this.setData({
  415. price3: price3
  416. })
  417. }
  418. this.setData({
  419. prices: prices,
  420. })
  421. console.log('prices', this.data.prices)
  422. }
  423. })
  424. },
  425. /**
  426. * 产品详情
  427. */
  428. getProdDetail() {
  429. let self = this
  430. shuoAPIClient.request('/api/minapp/common/product/' + self.data.infoId, {
  431. isOrder: 1
  432. }, false).then(res => {
  433. console.log('产品详情', res)
  434. if (res && res.data) {
  435. self.setData({
  436. prodDetail: res.data
  437. })
  438. }
  439. this.initLinkInfo()
  440. })
  441. },
  442. /**
  443. * 初始取票人信息
  444. */
  445. initLinkInfo() {
  446. let linkInfo = []
  447. let linkDetail = {
  448. name: "姓名",
  449. nameValue: '',
  450. phone: "手机号",
  451. phoneValue: '',
  452. }
  453. if (this.checkIdCard()) {
  454. linkDetail.idCard = '身份证'
  455. linkDetail.idCardValue = ''
  456. }
  457. linkInfo.push(linkDetail)
  458. this.setData({
  459. linkInfo: linkInfo
  460. })
  461. },
  462. /**
  463. * 校验是否必填身份证
  464. */
  465. checkIdCard() {
  466. let prodDetail = this.data.prodDetail
  467. if (!prodDetail || !prodDetail.custField || prodDetail.custField.indexOf('link_credit_no') == -1) {
  468. return false
  469. }
  470. return true
  471. },
  472. /**
  473. * 取票人信息变更处理
  474. */
  475. changeLinkInfo() {
  476. let isSingle = this.data.prodDetail.isSingle
  477. let num = this.data.num
  478. let linkInfo = this.data.linkInfo
  479. if (isSingle == 1) {
  480. let linkLength = linkInfo.length
  481. if (linkLength > num) {
  482. for (let i = 0; i < linkLength - num; i++) {
  483. linkInfo.splice(linkInfo.length - 1, 1)
  484. }
  485. } else if (linkLength < num) {
  486. let linkDetail = {
  487. name: "姓名",
  488. nameValue: '',
  489. phone: "手机号",
  490. phoneValue: '',
  491. }
  492. if (this.checkIdCard()) {
  493. linkDetail.idCard = '身份证'
  494. linkDetail.idCardValue = ''
  495. }
  496. for (let i = 0; i < num - linkLength; i++) {
  497. linkInfo.push(linkDetail)
  498. }
  499. }
  500. this.setData({
  501. linkInfo: linkInfo
  502. })
  503. }
  504. },
  505. /**
  506. * 输入姓名
  507. */
  508. inputName(e) {
  509. console.log('输入姓名', e)
  510. let index = e.currentTarget.dataset.index
  511. let value = e.detail.value
  512. let linkInfo = this.data.linkInfo
  513. if (linkInfo[index]) {
  514. linkInfo[index].nameValue = value
  515. this.setData({
  516. linkInfo: linkInfo
  517. })
  518. }
  519. console.log('linkInfo', this.data.linkInfo)
  520. },
  521. /**
  522. * 输入电话
  523. */
  524. inputPhone(e) {
  525. console.log('输入电话', e)
  526. let index = e.currentTarget.dataset.index
  527. let value = e.detail.value
  528. let linkInfo = this.data.linkInfo
  529. if (linkInfo[index]) {
  530. linkInfo[index].phoneValue = value
  531. this.setData({
  532. linkInfo: linkInfo
  533. })
  534. }
  535. console.log('linkInfo', this.data.linkInfo)
  536. },
  537. /**
  538. * 输入身份证
  539. */
  540. inputIdCard(e) {
  541. console.log('输入身份证', e)
  542. let index = e.currentTarget.dataset.index
  543. let value = e.detail.value
  544. let linkInfo = this.data.linkInfo
  545. if (linkInfo[index]) {
  546. linkInfo[index].idCardValue = value
  547. this.setData({
  548. linkInfo: linkInfo
  549. })
  550. }
  551. console.log('linkInfo', this.data.linkInfo)
  552. },
  553. /**
  554. * 留言
  555. */
  556. inputRemark(e) {
  557. console.log('输入留言', e)
  558. this.setData({
  559. remark: e.detail.value
  560. })
  561. }
  562. })