index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // pages/downloadPage/index.ts
  2. const app = getApp<IAppOption>();
  3. let apiPay = false;// 是否调用支付成功
  4. let t: any = undefined;
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. },
  11. toDownload() {
  12. if (t) t = clearTimeout(t);
  13. t = setTimeout(() => {
  14. app.getUser({
  15. data: {},
  16. success: (res: any) => {
  17. console.log(res);
  18. if (!res.RESULT) return wx.showToast({
  19. "title": "视频正在生成中,请耐心等待"
  20. })
  21. res.PAY ? this.downloadVideo() : this.toPAY();
  22. }
  23. })
  24. }, 200);
  25. },
  26. downloadVideo() {
  27. if (t) t = clearTimeout(t);
  28. t = setTimeout(() => {
  29. app.getVideo({
  30. success: (r: any) => {
  31. wx.showLoading({
  32. title: ""
  33. })
  34. wx.downloadFile({
  35. url: r.url,
  36. success: (res) => {
  37. wx.showLoading({
  38. title: ""
  39. })
  40. wx.saveVideoToPhotosAlbum({
  41. filePath: res.tempFilePath,
  42. success: () => {
  43. console.log("--保存成功")
  44. wx.showToast({
  45. title: "保存成功",
  46. icon: "none"
  47. })
  48. },
  49. complete: () => {
  50. wx.hideLoading();
  51. }
  52. })
  53. },
  54. complete: () => {
  55. wx.hideLoading();
  56. }
  57. })
  58. },
  59. })
  60. }, 200);
  61. },
  62. toPAY() {
  63. app.createOrder({
  64. success: (res: any) => {
  65. console.log('支付:', res);
  66. apiPay = true;
  67. wx.requestPayment({
  68. timeStamp: res.timeStamp,
  69. nonceStr: res.nonceStr,
  70. package: res.package,
  71. paySign: res.paySign,
  72. signType: res.signType,
  73. success() { },
  74. failL: () => {
  75. wx.showToast({
  76. icon: "none",
  77. title: "支付调用失败"
  78. })
  79. apiPay = false;
  80. }
  81. })
  82. }
  83. })
  84. },
  85. /**
  86. * 生命周期函数--监听页面加载
  87. */
  88. onLoad() {
  89. wx.hideHomeButton({})
  90. },
  91. /**
  92. * 生命周期函数--监听页面初次渲染完成
  93. */
  94. onReady() {
  95. },
  96. /**
  97. * 生命周期函数--监听页面显示
  98. */
  99. onShow() {
  100. console.log("前台");
  101. // 如果支付成功则下载视频
  102. if (apiPay) this.downloadVideo();
  103. },
  104. /**
  105. * 生命周期函数--监听页面隐藏
  106. */
  107. onHide() {
  108. console.log("后台");
  109. },
  110. /**
  111. * 生命周期函数--监听页面卸载
  112. */
  113. onUnload() {
  114. },
  115. /**
  116. * 页面相关事件处理函数--监听用户下拉动作
  117. */
  118. onPullDownRefresh() {
  119. },
  120. /**
  121. * 页面上拉触底事件的处理函数
  122. */
  123. onReachBottom() {
  124. },
  125. /**
  126. * 用户点击右上角分享
  127. */
  128. onShareAppMessage() {
  129. }
  130. })