Files
qingcheng-xiaochengxu/pages/course/applyPromotion/applyPromotion.js
2025-06-25 15:45:25 +08:00

160 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/course/applyPromotion/applyPromotion.js
Page({
data: {
showCode: false,
buttonText: '生成推广码',
codeImageUrl: 'https://img.picui.cn/free/2025/06/24/685a8953cae83.png'
},
// 点击“生成推广码”或“查看推荐的用户”
handleGenerate() {
if (!this.data.showCode) {
// 1. 显示带遮罩的 loading防止用户点击其他地方
wx.showLoading({
title: '生成中...',
mask: true
});
// 2. 调用后端接口生成推广码图片
wx.request({
url: 'https://your-backend.com/api/generatePromotionCode', // TODO: 替换成你真实的接口地址
method: 'POST',
data: {
// 如果需要传用户信息或参数,在这里补充
},
success: res => {
if (1) {
// 3. 接口返回正确,更新图片地址并显示
this.setData({
codeImageUrl: res.data.imageUrl,
showCode: true,
buttonText: '查看推荐的用户'
});
} else {
wx.showToast({
title: '生成失败,请重试',
icon: 'none'
});
}
},
fail: () => {
wx.showToast({
title: '网络错误,请检查连接',
icon: 'none'
});
},
complete: () => {
// 4. 隐藏 loading
wx.hideLoading();
}
});
} else {
// 已生成时,跳转到查看推荐用户页(如有)
wx.navigateTo({
url: '/pages/course/recommendUser/recommendUser',
})
}
},
// 保存图片到相册(与之前一致)
saveImage() {
const {
codeImageUrl
} = this.data;
wx.showLoading({
title: '保存中...',
mask: true
});
wx.downloadFile({
url: codeImageUrl,
success: res => {
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
wx.hideLoading();
wx.showToast({
title: '保存成功',
icon: 'success'
});
},
fail: err => {
wx.hideLoading();
wx.showToast({
title: '保存失败',
icon: 'none'
});
console.error('saveImageToPhotosAlbum 失败', err);
}
});
}
},
fail: err => {
wx.hideLoading();
wx.showToast({
title: '下载失败',
icon: 'none'
});
console.error('downloadFile 失败', err);
}
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})