import commonApi from "../../../../utils/https/common" let app = getApp() Component({ properties: { productId: { type: [String, Number], value: '' }, postId: { type: [String, Number], value: '' }, type: { type: String, value: '' } }, data: { addressList: [], addressIndex: -1, address: null, smoothlyList: [], sommthlyIndex: 0, showAddressPopup: false, showItem: null, showSkuPopup: false, content: '' }, lifetimes: { attached() { this.setData({ sommthlyIndex: 0 }) this.getList() // 组件初始化时,通知父组件重置价格为 0 this.triggerEvent('updateTotalPrice', { total: 0 }); } }, observers: { 'type': function(newVal, oldVal) { // 当从邮寄切换到自提时 if (oldVal === 'post' && newVal !== 'post') { // 重置所有商品邮费 const smoothlyList = this.data.smoothlyList.map(item => ({ ...item, postMoney: 0 })) this.setData({ smoothlyList, addressIndex: -1 }) // 重新计算总价 this.calculateTotalPrice() } else if (newVal === 'post') { // 切换到邮寄时重新计算邮费 this.showPostMoney() } }, 'postId': function(newVal, oldVal) { // 只有当当前是邮寄类型时,才重新计算邮费 if (this.data.type === 'post' && newVal !== oldVal) { this.showPostMoney() } } }, onload() { this.couponCom = this.selectAllComponents("#coupon")[0]; }, methods: { // 查看规格信息 showSkuInfo(e) { console.log(e) this.setData({ showItem: e.currentTarget.dataset.item, showSkuPopup: true, content: e.currentTarget.dataset.item.content }) console.log(this.data.showItem) }, closePopup() { this.setData({ showSkuPopup: false }) }, // 是否有选中商品 hasSelectedGoods() { return this.data.smoothlyList.some(item => (item.buyNum || 0) > 0); }, // 规格id emitSkuIds() { let skuIds = [] this.data.smoothlyList.forEach(item => { if (item.buyNum > 0) { skuIds.push(item.sku_id) } }); return skuIds; }, // 下单参数 emitOrder() { let orderList = [] this.data.smoothlyList.forEach(item => { if (item.buyNum > 0) { orderList.push({ type: item.type, product_id: item.id, sku_id: item.sku_id, post: this.data.type == 'post' ? this.data.postId : (this.data.address ? this.data .address.id : ''), product_num: item.buyNum, use_type: 0, is_batch_shipment: 0, }) } }) return orderList }, switchSmoothlyList() { const { smoothlyList, sommthlyIndex } = this.data // 重置当前商品数量 smoothlyList[sommthlyIndex].buyNum = 0 // 更新索引 const newIndex = sommthlyIndex < smoothlyList.length - 1 ? sommthlyIndex + 1 : 0 this.setData({ smoothlyList, sommthlyIndex: newIndex }) this.showPostMoney().then(() => { this.calculateTotalPrice() }) }, setNullCoupons() { if (app.globalData.couponInfo != null) { wx.showToast({ title: '订单价格发生变化,请重新选择优惠券', icon: 'none' }) this.triggerEvent('callOtherComp', { methodName: 'setNullCoupon', // 要调用的子组件B的方法名 }); } }, setAddress(e) { console.log(e.detail); this.setData({ address: e.detail }) this.showPostMoney().then(() => { this.calculateTotalPrice() }) }, showAddress() { const childComp = this.selectComponent('#addressComp'); if (!childComp) { wx.showToast({ title: '组件未加载完成', icon: 'none' }); return; } childComp.showLinkman() }, showImg(img) { if (!img) { return img } if (img.indexOf('https://') != -1 || img.indexOf('http://') != -1) { return img; } else { // return "https://test.api.cloud.sz-trip.com"+img return "https://static.ticket.sz-trip.com" + img; } }, getList() { commonApi.user_post('product/get_convenient_purchase', { page: 1, limit: 100, product_id: this.data.productId }).then(res => { if (res.data) { const smoothlyList = res.data.map(i => ({ ...i, headimg: this.showImg(i.headimg), buyNum: 0, postMoney: 0 })); this.setData({ smoothlyList }); } }) }, showPostMoney() { const { type, postId, addressList, addressIndex, smoothlyList } = this.data if (type == 'post') { if (!postId) return Promise.resolve() } else { if (!this.data.address) return Promise.resolve() } const promises = smoothlyList.map(item => { if (item.buyNum > 0) { return commonApi.user_post("order/get_post_price", { sku_id: item.sku_id, num: item.buyNum, consignee_id: type == 'post' ? postId : (this.data.address ? this.data .address.id : '') }).then(res => { if (res.code == 1) { // 更新对应商品的邮费 const newList = [...smoothlyList] const idx = newList.findIndex(i => i.sku_id === item.sku_id) console.log(res.data.price) if (idx > -1) { newList[idx].postMoney = res.data.price this.setData({ smoothlyList: newList }) } } }) } return Promise.resolve() }) return Promise.all(promises) }, decreaseSkuNum(e) { const item = e.currentTarget.dataset.item const { smoothlyList } = this.data if (item.buyNum > 0) { // 找到对应商品并更新数量 const newList = smoothlyList.map(i => { if (i.sku_id === item.sku_id) { return { ...i, buyNum: i.buyNum - 1 } } return i }) this.setData({ smoothlyList: newList }) this.setNullCoupons() this.showPostMoney().then(() => { this.calculateTotalPrice() }) } }, increaseSkuNum(e) { const item = e.currentTarget.dataset.item const { smoothlyList } = this.data // 找到对应商品并更新数量 const newList = smoothlyList.map(i => { if (i.sku_id === item.sku_id) { return { ...i, buyNum: i.buyNum + 1 } } return i }) this.setData({ smoothlyList: newList }) this.showPostMoney().then(() => { this.calculateTotalPrice() }) }, // 计算总价 calculateTotalPrice() { let total = 0; this.data.smoothlyList.forEach(item => { if (item.buyNum > 0) { const itemPrice = parseFloat(item.price) * item.buyNum; total += itemPrice + item.postMoney; } }); this.triggerEvent('updateTotalPrice', { total }); return total; }, } })