// pages/downloadPage/index.ts const app = getApp(); let apiPay = false;// 是否调用支付成功 let t: any = undefined; Page({ /** * 页面的初始数据 */ data: { }, toDownload() { if (t) t = clearTimeout(t); t = setTimeout(() => { app.getUser({ data: {}, success: (res: any) => { console.log(res); if (!res.RESULT) return wx.showToast({ "title": "视频正在生成中,请耐心等待" }) res.PAY ? this.downloadVideo() : this.toPAY(); } }) }, 200); }, downloadVideo() { if (t) t = clearTimeout(t); t = setTimeout(() => { app.getVideo({ success: (r: any) => { wx.showLoading({ title: "" }) wx.downloadFile({ url: r.url, success: (res) => { wx.showLoading({ title: "" }) wx.saveVideoToPhotosAlbum({ filePath: res.tempFilePath, success: () => { console.log("--保存成功") wx.showToast({ title: "保存成功", icon: "none" }) }, complete: () => { wx.hideLoading(); } }) }, complete: () => { wx.hideLoading(); } }) }, }) }, 200); }, toPAY() { app.createOrder({ success: (res: any) => { console.log('支付:', res); apiPay = true; wx.requestPayment({ timeStamp: res.timeStamp, nonceStr: res.nonceStr, package: res.package, paySign: res.paySign, signType: res.signType, success() { }, failL: () => { wx.showToast({ icon: "none", title: "支付调用失败" }) apiPay = false; } }) } }) }, /** * 生命周期函数--监听页面加载 */ onLoad() { wx.hideHomeButton({}) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { console.log("前台"); // 如果支付成功则下载视频 if (apiPay) this.downloadVideo(); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { console.log("后台"); }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })