Files
sgy-web/pages/mall/details/details.js
2023-06-21 17:14:39 +08:00

315 lines
8.3 KiB
JavaScript

/*
* 手太欠
* 愿这世界都如故事里一样 美好而动人~
*/
Page({
data: {
isFixedTop : 0,
barHeight : getApp().globalData.barHeight, // 状态栏高度
goodsId : '', // 产品id
mallData : '', // 产品信息
mallContent: '', // 产品简介
shareSee : false, // 分享弹出
inviteText : '', // 自己的邀请码
isParent : false, // 绑定邀请码
nameValue : '', // 上级邀请码
},
onLoad(options) {
if(options.invite != undefined) {
getApp().globalData.inviteText = options.invite
}
this.setData({
goodsId: options.id
})
// 初始化画布
wx.createSelectorQuery().select('#coverCanvas').fields({node: true, size: true}).exec(canvasNode => {
const canvas = canvasNode[0].node
canvas.width = 375
canvas.height = 600
this.setData({
canvas
})
})
},
onShow() {
// 获取产品详情
this.mallInfo();
// 获取登录状态
if(wx.getStorageSync("token") != ''){
// 小程序码
this.ShareInfo();
}
},
/**
* 产品详情
*/
mallInfo (){
wx.$api.mall.mallSee(this.data.goodsId).then(res => {
this.setData({
mallData : res.data,
mallContent : res.data.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"')
})
}).catch(err => { })
},
/**
* 确认购买
*/
buyTap() {
// 获取登录状态
if(wx.getStorageSync("token") != ''){
if(this.data.mallData.is_parent == true) {
wx.navigateTo({
url: '/pages/mall/confirm/confirm?goodsid=' + this.data.mallData.goods_id + '&skuid=' + this.data.mallData.skus[0].sku_id + '&qty=1',
})
return
}
// 显示绑定邀请码弹窗
this.setData({
isParent: true
})
}else{
// 去登录
wx.navigateTo({
url: "/pages/login/index"
})
}
},
/**
* 小程序码
*/
ShareInfo() {
wx.$api.user.miniShare({
url: '/pages/index/index'
}).then(res => {
this.setData({
inviteCode: res.data.qrcode
})
}).catch(err => {})
},
/**
* 分享弹出
*/
shareTap() {
// 获取登录状态
if(wx.getStorageSync("token") != ''){
this.setData({
shareSee: !this.data.shareSee
})
}else{
// 去登录
wx.navigateTo({
url: "/pages/login/index"
})
}
},
/**
* 生成海报
*/
onCanvas(){
wx.showLoading({
title: '生成图片中...',
mask : true
})
const canvas = this.data.canvas
const ctx = canvas.getContext('2d')
const codeImgEl = canvas.createImage()
const backBackEl = canvas.createImage()
codeImgEl.src = this.data.inviteCode //二维码
backBackEl.src = this.data.mallData.cover //产品分享图片
const codeImgLoding = new Promise((resolve, reason) => {
codeImgEl.onload = () => {
resolve()
}
})
const backBackLoding = new Promise((resolve, reason) => {
backBackEl.onload = () => {
resolve()
}
})
Promise.all([codeImgLoding, backBackLoding]).then(() => {
// 绘制[二维码-白色背景]
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, 375, 540);
ctx.drawImage(backBackEl, 0, 0, 375, 375)
// 绘制[二维码]
ctx.drawImage(codeImgEl, 270, 440, 80, 80)
// 产品名称
ctx.font = "24px Arial"; //字体大小
ctx.fillStyle = "#000000"; //字体颜色
ctx.fillText(this.data.mallData.name, 25, 420);
// 产品价格
ctx.font = "bold 36px Arial"; //字体大小
ctx.fillStyle = "#3b7cff"; //字体颜色
ctx.fillText(this.data.mallData.price.price, 25, 480);
// 海报文字提示
ctx.font = "18px Arial"; //字体大小
ctx.fillStyle = "#b3b3b3"; //字体颜色
ctx.fillText('识别二维码进行查看', 25, 510);
wx.hideLoading()
wx.canvasToTempFilePath({
canvas: this.data.canvas,
success : res => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: saveRes => {
wx.showToast({
title: '海报已保存至您的相册',
icon : 'none'
})
this.setData({
shareSee: false
})
},
fail: () => {
this.setData({
shareSee: false
})
wx.hideLoading()
wx.showModal({
title: '提示',
content: '暂未授权小程序写入您的相册,无法存储海报',
confirmColor: '#e50d01',
confirmText: '去设置',
success: res => {
if (res.confirm) {
wx.openSetting()
}
}
})
}
})
},
})
}).catch(err => {
wx.showToast({
title: '图片加载失败',
icon : 'none'
})
})
},
/*
邀请码截取
*/
bindinput(e) {
this.setData({
nameValue: e.detail.value
})
},
/**
* 绑定邀请码
*/
nameTrue() {
wx.$api.mall.levelBind({
invite: this.data.nameValue
}).then(res => {
this.setData({
isParent: false
})
wx.navigateTo({
url: '/pages/mall/confirm/confirm?goodsid=' + this.data.mallData.goods_id + '&skuid=' + this.data.mallData.skus[0].sku_id + '&qty=1',
})
}).catch(err => {})
},
/**
* 关闭绑定邀请码弹窗
*/
nameCancel() {
this.setData({
isParent: !this.data.isParent
})
},
/**
* 删除邀请码
*/
colseTap() {
this.setData({
nameValue: ''
})
},
/**
* 返回上一页
*/
returnGo() {
wx.navigateBack({
delta: 1,
fail: err => {
wx.switchTab({
url: '/pages/index/index?invite=' + getApp().globalData.inviteText
})
}
})
},
/**
* 回到首页
*/
returnHome() {
wx.switchTab({
url: '/pages/index/index?invite=' + getApp().globalData.inviteText
})
},
/**
* 监听页面滑动事件
*/
onPageScroll(e) {
this.setData({
isFixedTop: parseInt(e.scrollTop)
});
},
/**
* 微信分享
*/
onShareAppMessage(){
this.setData({
shareSee: false
})
return {
title : this.data.mallData.name,
path : "/pages/mall/details/details?id=" + this.data.goodsId + '&invite=' + wx.getStorageSync("invite"),
imageUrl: this.data.mallData.cover
}
},
/**
* 放大轮播相册图片
*/
opneBanner(e){
let imgs = [],
index = e.currentTarget.dataset.index
for (let img of e.currentTarget.dataset.imgs){
imgs.push(img)
}
wx.previewImage({
urls : imgs,
current : imgs[index]
})
}
})