// pages/shop/goodsDetail/goodsDetail.js import Shop from '../../../net/api/shop' import { sImgPrefix, } from '../../../net/mainUrl' const app = getApp() const deviceInfo = wx.getDeviceInfo() const screenInfo = wx.getWindowInfo(); const statusBarHeight = screenInfo.statusBarHeight; // 状态栏高度 const navBarHeight = deviceInfo.platform == 'IOS' ? 48 : 50; // 导航栏高度(iOS 为 44px,Android 为 48px) Page({ /** * 页面的初始数据 */ data: { animationClass: '', statusBarHeight: statusBarHeight, totalHeight: navBarHeight, id: '', msgType: 'info', msgHint: '', msgShow: false, goods: null, isagree: false }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { wx.setNavigationBarTitle({ title: '我要买', }) wx.setNavigationBarColor({ frontColor: '#FFFFFF', // 必写项,字体颜色仅支持#ffffff和#000000 backgroundColor: '#000000', // 传递的颜色值,仅支持十六进制颜色 }) const id = options.id if (id != '') { this.setData({ id: id }) this.doGetDetail() } else { this.setData({ msgType: 'error', msgHint: '数据有误,请刷新重试', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1200); } }, onShow() { this.setData({ animationClass: 'fade-in' }) }, onHide() { this.setData({ animationClass: 'fade-out' }) }, doBack() { wx.navigateBack() }, doPreImg(e) { const url = e.currentTarget.dataset.url wx.previewImage({ urls: [url], }) }, doChangeDeal() { this.setData({ isagree: !this.data.isagree }) }, //显示购买协议 doShowDeal() { wx.navigateTo({ url: '/pages/treaty/rule/rule?id=db805bf2-eef9-47d5-941b-cdc3b83ec316&needToken=true', }) }, //提交购买 doConfirm() { if (this.data.isagree) { this.doSaveOrder() } else { this.setData({ msgHint: '请阅读并同意购买协议', msgType: 'info', msgShow: true }) } }, async doSaveOrder() { try { wx.showLoading({ title: '购买中..', }); const res = await Shop.doSaveOrder(this.data.id); wx.hideLoading(); if (res && res.data != '') { await this.doConfirmOrder(res.data); } else { this.setData({ msgHint: '网络错误,请稍后重试', msgType: 'error', msgShow: true }); } } catch (err) { wx.hideLoading(); this.setData({ msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgType: 'error', msgShow: true }); } }, async doConfirmOrder(id) { try { wx.showLoading({ title: '购买中...', }); await Shop.doConfirmOrder(id); wx.hideLoading(); this.setData({ msgHint: '购买成功', msgType: 'success', msgShow: true }); setTimeout(() => { wx.navigateBack(); }, 1500); } catch (err) { wx.hideLoading(); this.setData({ msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgType: 'error', msgShow: true }); } }, doGetDetail() { const that = this wx.showLoading({ title: '加载中...', }) Shop.doGetGoodsDetail(that.data.id) .then(res => { wx.hideLoading() console.log(res) if (res.goodsPhoto && res.goodsPhoto != '') { res.goodsPhoto = sImgPrefix + res.goodsPhoto that.setData({ goods: res }) } else { that.setData({ msgHint: '网络错误,请稍后重试', msgType: 'error', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1200); } }) .catch(err => { wx.hideLoading() that.setData({ msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgType: 'error', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1200); }) } })