// pages/order/order-info/index.js const request = require('../../../utils/request'); //导入模块 const util = require('../../../utils/util'); const float = require('../../../utils/floatObj'); const event = require('../../../utils/event.js') const app = getApp() Page({ /** * 页面的初始数据 */ data: { token: app.globalData.token, backStr: '返回', orderInfo: null, matchTag: null, sukList: [], imgList: [], form: { productId: null, skuId: null, number: 1, unitPrice: 0 }, amount: null, isIPhoneX: false, status: 0, controls: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (getCurrentPages().length == 1) { this.setData({ backStr: '首页' }) } this.setData({ token: app.globalData.token, isIPhoneX: app.globalData.isIPhoneX }) event.on('EventMessage', this, this.onEvent) if (options.id) { wx.showLoading({ title: '加载中', mask: true }) // /product/{id}产品详情 request.get('/bxe-mall/product/' + options.id).then(result => { //成功回调 wx.hideLoading() var vindex = -1 var vurl = null var imgList = [] for (let index = 0; index < result.data.imgList.length; index++) { const element = result.data.imgList[index] if(this.isVideoUrl(element)){ vindex = index vurl = element break } } if(vindex >= 1){ result.data.imgList.splice(vindex, 1) imgList = result.data.imgList imgList.unshift(vurl) this.videoContext = wx.createVideoContext('video') } else { imgList = result.data.imgList } this.setData({ ['form.productId']: options.id, orderInfo: result.data, imgList: imgList }) }).catch(err => { //异常回调 wx.hideLoading() util.showToast(err) }) } }, swapArray: function (arr, index1, index2) { arr[index1] = arr.splice(index2, 1, arr[index1])[0] return arr }, isVideoUrl: function (url) { if (url && url.indexOf('.mp4') >= 0) { return true } return false }, // 事件处理 onEvent: function (message) { console.log('mall>>order>>onEvent', message) if (message.what == 1 && util.isEmpty(this.data.token)) { this.setData({ token: app.globalData.token }) } else if (message.what == 10) { wx.navigateBack() } }, bindchange: function (event) { if (this.videoContext && this.data.status == 1) { this.videoContext.pause() } }, videoTouch: function () { console.log('videoTouch>>>') if (this.timeout) { return } if (!this.data.controls && this.data.status == 1) { this.setData({ controls: true }) this.timeout = setTimeout(() => { this.setData({ controls: false }) this.timeout = null }, 1000) } }, videoTap: function () { console.log('videoTap>>>') if (this.videoContext) { if (this.data.status == 0) { this.videoContext.play() } else if (this.data.status == 1) { this.videoContext.pause() } } }, bindplay: function () { this.setData({ status: 1 }) this.timeout = setTimeout(() => { this.setData({ controls: false }) this.timeout = null }, 1500) }, bindpause: function () { this.setData({ status: 0, controls: true }) if (this.timeout) { clearTimeout(this.timeout) this.timeout = null } }, bindended: function () { this.setData({ status: 0 }) }, // phone: function (e) { // var mobile = e.currentTarget.dataset.index // if (util.checkPhone(mobile)) { // wx.makePhoneCall({ // phoneNumber: mobile // }) // } // }, // 图片查看 viewImage: function (e) { if (e.currentTarget.dataset.url.indexOf('.mp4') >= 0) { return } var imgList = [] for (let index = 0; index < this.data.orderInfo.imgList.length; index++) { if (this.data.orderInfo.imgList[index].indexOf('.mp4') >= 0) { continue } imgList.push(this.data.orderInfo.imgList[index]) } wx.previewImage({ urls: imgList, current: e.currentTarget.dataset.url }); }, // 根据 checkDisableTag: function (sukId, match, element) { match[sukId] = element.id for (let index = 0; index < this.data.orderInfo.skuList.length; index++) { const item = this.data.orderInfo.skuList[index]; if (this.checkSkuPrice(match, item)) { delete match[sukId] return false } } delete match[sukId] return true }, // tag 点击事件 tagClick: function (e) { this.data.matchTag = null var sukId = e.currentTarget.dataset.suk // 对应的是一级列表的Id var id = e.currentTarget.dataset.index // 对应的是二级列表的Id,相应是有价格计算的 //------------------------------sukList------------------------------------------- // 检查是否需要跟新,或者修改,跟新sukList; var push_flag = true for (let index = 0; index < this.data.sukList.length; index++) { const element = this.data.sukList[index]; if (element.skuId == sukId) { this.data.sukList[index].id = id push_flag = false break } } if (push_flag) { this.data.sukList.push({ skuId: sukId, id: id }) } //------------------------------attributeList------------------------------------------- var match = {} for (let index = 0; index < this.data.sukList.length; index++) { const element = this.data.sukList[index]; match[element.skuId] = element.id } var attributeList = this.data.orderInfo.attributeList for (let i = 0; i < attributeList.length; i++) { for (let k = 0; k < attributeList[i].values.length; k++) { const element = attributeList[i].values[k]; // 点击的列判断 对应的是一级列表,肯定是可以点击的 if (attributeList[i].id == sukId) { element.select = false if (element.id == id) { element.select = true } } else { // 如果其余的规格没有价格的,进行处理;无法点击 element.disabled = this.checkDisableTag(attributeList[i].id, match, element) } } } this.setData({ ['orderInfo.attributeList']: attributeList, amount: null }) //------------------------------matchTag------------------------------------------- if (this.data.sukList.length == this.data.orderInfo.attributeList.length) { this.data.matchTag = {} for (let index = 0; index < this.data.sukList.length; index++) { const element = this.data.sukList[index]; this.data.matchTag[element.skuId] = element.id } this.onNumChange(null) } }, // 根据所有选择的规格校验获取价格 checkSkuPrice: function (tag, element) { if (!tag) { return false } for (var key in tag) { if (element.price <= 0 || tag[key] != element[key]) { return false } } return true }, // 数量变化的函数监听 onNumChange: function (e) { if (e) { this.setData({ ['form.number']: e.detail.value }) // 如果查询到了价格,进行价格计算 if (this.data.form.unitPrice) { this.setData({ amount: float.accMul(this.data.form.unitPrice, this.data.form.number) }) } } else if (this.data.sukList.length == this.data.orderInfo.attributeList.length) { // 根据规格,克重,以及其他筛选价格 for (let index = 0; index < this.data.orderInfo.skuList.length; index++) { const element = this.data.orderInfo.skuList[index]; if (this.checkSkuPrice(this.data.matchTag, element)) { this.data.form.unitPrice = Number(element.price) this.data.form.monthPrice = Number(element.groupMonthPrice) this.data.form.stockNumber = Number(element.stockNumber) // 如果当前的数量大于所选规格的库存数量,那么就设置成库存数量; if (this.data.form.number > this.data.form.stockNumber) { this.data.form.number = Number(element.stockNumber) } this.data.form.dividePrice = Number(element.dividePrice) this.data.form.skuId = element.id // console.log(element) break } } // 如果查询到了价格,进行价格计算 if (this.data.form.unitPrice) { this.setData({ ['form.number']: this.data.form.number, ['form.unitPrice']: this.data.form.unitPrice, ['form.stockNumber']: this.data.form.stockNumber, amount: float.accMul(this.data.form.unitPrice, this.data.form.number) }) } } }, // 进入支付流程 offerProject: function (e) { var attributeList = this.data.orderInfo.attributeList for (let i = 0; i < attributeList.length; i++) { var toast = '请选择' + attributeList[i].name for (let k = 0; k < attributeList[i].values.length; k++) { const element = attributeList[i].values[k]; if (element.select) { toast = '' break } } if (!util.isEmpty(toast)) { util.showToast(toast) return } } if (this.data.form.number <= 0) { util.showToast('请输入购买数量,必选大于0') return } if (this.data.form.stockNumber <= 0) { util.showToast('暂无库存,请选择其他规格类型') return } this.data.form.summary = this.data.orderInfo.summary this.data.form.sellMode = this.data.orderInfo.sellMode this.data.form.name = this.data.orderInfo.name this.data.form.outline = this.data.orderInfo.outline this.data.form.litimgUrl = this.data.orderInfo.litimgUrl this.data.form.unit = this.data.orderInfo.unit wx.navigateTo({ url: '/pages/mall/order-offer/index?json=' + JSON.stringify(this.data.form) }) }, // 就行登录流程;然后进入下一步的支付操作 onGotPhoneNumber: function (e) { request.loginWechat(e.detail).then(data => { this.offerProject() }) }, onShareAppMessage: function () { return { title: this.data.orderInfo.name, path: '/pages/mall/order-info/index?id=' + this.data.orderInfo.id, imageUrl: this.data.orderInfo.litimgUrl, success: function (res) {} } }, // event的unregister onUnload: function () { event.remove('EventMessage', this) if (getCurrentPages().length == 1) { this.toHome() } }, // 返回到主页 toHome() { wx.reLaunch({ url: '/pages/mall/shops/index', }) } })