import { getProductInfo } from "../../../../api/ztb" const util = require('../../../../utils/util') const math = require('../../../../utils/math') const event = require('../../../../utils/event') const app = getApp() Page({ /** * 页面的初始数据 */ data: { backStr: '返回', orderInfo: null, matchTag: null, sukList: [], imgList: [], form: { productId: null, skuId: null, number: 1, unitPrice: 0 }, amount: null, safeBottom: false, imageHeight: 0, visible: false, videoUrl: null }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (getCurrentPages().length == 1) { this.setData({ backStr: '首页' }) } this.setData({ imageHeight: math.divide(math.times(750, 9), 16), safeBottom: app.globalData.safeBottom }) event.on('EventMessage', this, this.onEvent) if (options.id) { wx.showLoading({ title: '加载中', mask: true }) getProductInfo(options.id).then(result => { wx.hideLoading() var vurl = null var imgList = [] if (result.data.videoUrl) { vurl = result.data.videoUrl 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, videoUrl: vurl, imgList: imgList }) }).catch(err => { //异常回调 wx.hideLoading() util.showToast(err) }) } }, 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) { } else if (message.what == 10) { wx.navigateBack() } }, videoTap: function () { this.setData({ visible: true, }) if (this.videoContext) { this.videoContext.play() } }, onClose: function () { this.setData({ visible: false, }) if (this.videoContext) { this.videoContext.pause() } }, bindended: function () { this.videoContext.exitFullScreen() this.setData({ visible: false }) }, // 图片查看 viewImage: function (e) { if (e.currentTarget.dataset.url.indexOf('.mp4') >= 0) { return } var imgList = [] for (let index = 0; index < this.data.imgList.length; index++) { if (this.data.imgList[index].indexOf('.mp4') >= 0) { continue } imgList.push(this.data.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) }) }, 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) {} } }, // 分享至朋友圈 onShareTimeline(res) { 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', }) } })