// pages/creatOrder/creatOrder.js import { shuoAPIClient } from '../../common/api/api.js'; import user from '../../common/user/user.js'; var touchstart = 0 var touchend = 0 Page({ /** * 页面的初始数据 */ data: { dateList: [], curr: "", isMoreDate: false, // 日期框展示 isMask: false, num: 1, choosedData: 1, infoId: null, year: null, month: null, blankDays: [], prices: null, price3: null, selectDate: null, // prodDetail: null, //产品详情 priceTotal: null, //计算后的总价 linkInfo: [], //游客信息 remark: '' }, /** * 下单 */ addOrder() { if (!user.getMyTokenSync()) { this.openToast('尚未登录授权,无法下单', 'none') return false } let travelDate = this.data.selectDate if (!travelDate) { this.openToast('请选择出游日期', 'none') return false } let linkInfo = this.data.linkInfo let peoples = [] for (let info of linkInfo) { if (!info.nameValue) { this.openToast('游客姓名未输入', 'none') return false } if (!info.phoneValue) { this.openToast('手机号码未输入', 'none') return false } if (this.checkIdCard() && !info.idCardValue) { this.openToast('身份证号码未输入', 'none') return false } let people = { linkMan: info.nameValue, linkPhone: info.phoneValue } if (this.checkIdCard()) { people.linkCreditType = 0 people.linkCreditNo = info.idCardValue } peoples.push(people) } // 下单参数 let params = { infoId: this.data.infoId, travelDate, ticketNum: this.data.num, people: peoples, linkMan: peoples[0].linkMan, linkPhone: peoples[0].linkPhone, linkCreditType: peoples[0].linkCreditType, linkCreditNo: peoples[0].linkCreditNo, orderMemo: this.data.remark } let data = { orderCommonRequest: params, treeId: 0 } console.log('请求参数json:', data) wx.showLoading({ title: '提交订单中...', mask: true }); shuoAPIClient.request('/api/minapp/order/order', data, true, 'POST').then(res => { wx.hideLoading() console.log('下单返回', res) if (res.data.state == 2) { // 下单成功,可以立即发起支付 let orderId = res.data.orderId wx.requestPayment({ 'timeStamp': res.data.timestamp, 'nonceStr': res.data.noncestr, 'package': res.data.packages, 'signType': res.data.signtype, 'paySign': res.data.paysign, 'complete': function(res) { console.log(res, '支付信息') let isPay = res.errMsg.indexOf('fail') > -1 ? 2 : 1; // 支付失败或取消支付:2, 支付成功:1; wx.redirectTo({ url: `/pages/orderDetail/orderDetail?orderId=${orderId}&isPay=${isPay}` }); } }); } else if (res.data.state == 1) { let orderId = res.data.orderId wx.redirectTo({ url: `/pages/orderDetail/orderDetail?orderId=${orderId}` }); } else { wx.showModal({ title: '提示', content: res.data.msg, showCancel: false }); } }) }, /** * 弹框提示 */ openToast(title, icon) { wx.showToast({ title: title, icon: icon, duration: 2000 }) }, touchstart: function(e) { console.log('滑动') console.log(e.changedTouches[0].clientX) touchstart = e.changedTouches[0].clientX }, touchend: function(e) { console.log('停止') console.log(e) console.log(e.changedTouches[0].clientX) touchend = e.changedTouches[0].clientX if (touchstart - touchend > 60) { console.log('左滑了') this.nextMonth() } if (touchstart - touchend < -60) { let today = new Date(); let amonth = today.getMonth() + 1 if (this.data.month > amonth) { this.lastMonth() } } }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { console.log('options', options) let today = new Date(); console.log('today.getMonth()', today.getMonth()) this.setData({ infoId: options.infoId, month: today.getMonth() + 1, year: today.getFullYear() }) this.initDate(this.data.year, this.data.month) this.blankDays(this.data.year, this.data.month) this.getProdDetail() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, onShow: function() { console.log('creatOrder') this.getPrice() }, onUnload: function() { console.log('onUnload') if (this.data.linkInfo[0].nameValue || this.data.linkInfo[0].phoneValue || this.data.linkInfo[0].idCardValue) { wx.showToast({ title: '你曾经说,最大的愿望,就是去旅行', icon: "none", duration: 2000 }) } }, /** * 初始化日历 */ initDate(year, month) { let date = new Date(year, month, 0) let days = date.getDate() var dateList = [] console.log(this.data.dateList) var today = new Date() let day = today.getDate() console.log(day) let week = today.getDay() console.log(today.getDay()) for (var i = 1; i <= days; i++) { // if (day == i) { // dateList.push('今天') // } else if (day + 1 == i) { // dateList.push('明天') // } else { dateList.push(i) // } } this.setData({ dateList: dateList, day: day, week: week, curr: '今天' }) }, /** * 当前月第一日前面的空白格 */ blankDays(year, month) { let date = new Date(year, month - 1, 1) console.log('date', date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate()); let week = date.getDay(); let blankDays = [] for (let i = 0; i < week; i++) { blankDays.push(""); } this.setData({ blankDays: blankDays }) }, /** * 登录回调 */ loginCallBack: function(e) { // console.log('loginCallBack') // if (getCurrentPages().length != 0) { // //刷新当前页面的数据 // getCurrentPages()[getCurrentPages().length - 1].onLoad() // } }, /** * 非日历内的选中日期事件 */ chooseDate: function(e) { console.log(e.currentTarget.dataset.traveldate) this.setData({ selectDate: e.currentTarget.dataset.traveldate }) this.priceCalculation() }, /** * 数量减少 */ increaseNum: function() { let num = this.data.num if (num > 1) { num-- this.setData({ num: num }) this.priceCalculation() this.changeLinkInfo() } }, /** * 数量增加 */ addNum: function() { let num = this.data.num num++ this.setData({ num: num }) this.priceCalculation() this.changeLinkInfo() }, /** * 价格计算 */ priceCalculation: function() { let num = this.data.num let selectDate = this.data.selectDate if (!selectDate) { return false } let prices = this.data.prices let priceTotal = prices[selectDate].salePrice / 100 * num this.setData({ priceTotal: priceTotal }) }, /** * 上个月 */ lastMonth: function() { let today = new Date(); let amonth = today.getMonth() + 1 if (this.data.month > amonth) { let year = this.data.year let month = this.data.month if (month == 1) { year = year - 1 month = 12 } else { month-- } this.setData({ year: year, month: month }) this.initDate(year, month) this.blankDays(year, month) this.getPrice() } }, /** * 下个月 */ nextMonth: function() { let year = this.data.year let month = this.data.month if (month == 12) { year = year + 1 month = 1 } else { month++ } this.setData({ year: year, month: month }) this.initDate(year, month) this.blankDays(year, month) this.getPrice() }, /** * 价格日历内选择日期 */ select: function(e) { console.log('select', e.currentTarget.dataset.day) let select = e.currentTarget.dataset.day let price = this.data.prices[this.data.year + '-' + (this.data.month < 10 ? '0' + this.data.month : this.data.month) + '-' + (select < 10 ? '0' + select : select)] if (!price) { return false; } this.setData({ curr: select, selectDate: price.travelDate }) this.priceCalculation() let week = new Date().getDay() let day = select if (day == '今天') { day = this.data.day } else if (day == '明天') { day = this.data.day + 1 } week = (week + (day - this.data.day)) % 7 console.log('week' + week) this.setData({ week: week }) this.close() }, /** * 价格日历开启 */ tapMoreDate: function() { this.setData({ isMoreDate: true, isMask: true }) }, /** * 价格日历关闭 */ close: function() { this.setData({ isMoreDate: false, isMask: false }) }, /** * 获取价格 */ getPrice() { // wx.showLoading({ // title: '加载中...', // mask: true // }); shuoAPIClient.request('/api/minapp/ticket/productPrices', { year: this.data.year, month: this.data.month, infoId: this.data.infoId }, false).then(res => { // wx.hideLoading() let prices = {} console.log('价格', res) if (res.data && res.data) { for (let data of res.data) { prices[data.travelDate] = data } if (!this.data.price3) { //如果前三日价格不存在则加载,存在就不变 let price3 = [] for (let i = 0; i < 3; i++) { if (res.data[i]) { price3.push(res.data[i]) } } this.setData({ price3: price3 }) } this.setData({ prices: prices, }) console.log('prices', this.data.prices) } }) }, /** * 产品详情 */ getProdDetail() { let self = this shuoAPIClient.request('/api/minapp/common/product/' + self.data.infoId, { isOrder: 1 }, false).then(res => { console.log('产品详情', res) if (res && res.data) { self.setData({ prodDetail: res.data }) } this.initLinkInfo() }) }, /** * 初始取票人信息 */ initLinkInfo() { let linkInfo = [] let linkDetail = { name: "姓名", nameValue: '', phone: "手机号", phoneValue: '', } if (this.checkIdCard()) { linkDetail.idCard = '身份证' linkDetail.idCardValue = '' } linkInfo.push(linkDetail) this.setData({ linkInfo: linkInfo }) }, /** * 校验是否必填身份证 */ checkIdCard() { let prodDetail = this.data.prodDetail if (!prodDetail || !prodDetail.custField || prodDetail.custField.indexOf('link_credit_no') == -1) { return false } return true }, /** * 取票人信息变更处理 */ changeLinkInfo() { let isSingle = this.data.prodDetail.isSingle let num = this.data.num let linkInfo = this.data.linkInfo if (isSingle == 1) { let linkLength = linkInfo.length if (linkLength > num) { for (let i = 0; i < linkLength - num; i++) { linkInfo.splice(linkInfo.length - 1, 1) } } else if (linkLength < num) { let linkDetail = { name: "姓名", nameValue: '', phone: "手机号", phoneValue: '', } if (this.checkIdCard()) { linkDetail.idCard = '身份证' linkDetail.idCardValue = '' } for (let i = 0; i < num - linkLength; i++) { linkInfo.push(linkDetail) } } this.setData({ linkInfo: linkInfo }) } }, /** * 输入姓名 */ inputName(e) { console.log('输入姓名', e) let index = e.currentTarget.dataset.index let value = e.detail.value let linkInfo = this.data.linkInfo if (linkInfo[index]) { linkInfo[index].nameValue = value this.setData({ linkInfo: linkInfo }) } console.log('linkInfo', this.data.linkInfo) }, /** * 输入电话 */ inputPhone(e) { console.log('输入电话', e) let index = e.currentTarget.dataset.index let value = e.detail.value let linkInfo = this.data.linkInfo if (linkInfo[index]) { linkInfo[index].phoneValue = value this.setData({ linkInfo: linkInfo }) } console.log('linkInfo', this.data.linkInfo) }, /** * 输入身份证 */ inputIdCard(e) { console.log('输入身份证', e) let index = e.currentTarget.dataset.index let value = e.detail.value let linkInfo = this.data.linkInfo if (linkInfo[index]) { linkInfo[index].idCardValue = value this.setData({ linkInfo: linkInfo }) } console.log('linkInfo', this.data.linkInfo) }, /** * 留言 */ inputRemark(e) { console.log('输入留言', e) this.setData({ remark: e.detail.value }) } })