order-result.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // pages/orders/order-detail.ts
  2. import drawQrcode from "../../utils/weapp.qrcode.esm";
  3. import { api_userPaymentOrderDetail } from "../../api/login";
  4. // 获取应用实例
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. status: 'WAIT_PAY',
  11. statusList: {
  12. WAIT_PAY: {
  13. logo: './images/ing.png',
  14. title: '等待付款',
  15. content: '请尽快完成支付,以便我们为您处理订单'
  16. },
  17. SUCCESS: {
  18. logo: './images/success.png',
  19. title: '交易完成',
  20. content: '登录「音乐数字课堂」APP使用AI学练'
  21. },
  22. CLOSED: {
  23. logo: './images/error.png',
  24. title: '交易取消',
  25. content: '您的交易订单已关闭'
  26. },
  27. WAIT_USE: {
  28. logo: './images/wait.png',
  29. title: '等待使用',
  30. content: '请尽快扫描下方二维码进行激活'
  31. },
  32. REFUNDED: {
  33. logo: './images/wait.png',
  34. title: '退款成功',
  35. content: '您的退款已成功处理,感谢您的理解和支持'
  36. }
  37. },
  38. timerCount: 0,
  39. timer: null as any,
  40. goodsInfo: {} as any,
  41. orderNo: "" as string,
  42. showCanvas: false, // 是否显示二维码
  43. canvasImg: "" as string
  44. },
  45. /**
  46. * 生命周期函数--监听页面加载
  47. */
  48. onLoad(options: any) {
  49. if (options.orderNo) {
  50. this.setData({
  51. orderNo: options.orderNo
  52. }, () => {
  53. this.getDetail(this.onTimeout)
  54. });
  55. }
  56. },
  57. async getDetail(callback?: any) {
  58. try {
  59. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  60. if (data.code == 200) {
  61. const result = data.data || {}
  62. const goodsInfos = result.goodsInfos || []
  63. const tempGoods: any = []
  64. goodsInfos.forEach((item: any) => {
  65. tempGoods.push({
  66. ...item,
  67. shortUrl: item.activationCodeInfo.shortUrl,
  68. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  69. typeName: this.formatPeriod(item.activationCodeInfo?.times || 1, item.activationCodeInfo.type)
  70. })
  71. })
  72. const firstGoods = tempGoods[0]
  73. let refundStyleStr = ''
  74. if(firstGoods?.refundStyle === 'TURN_BACK') {
  75. refundStyleStr = '原路返回'
  76. } else if(firstGoods?.refundStyle === 'OFFLINE') {
  77. refundStyleStr = '线下'
  78. }
  79. const goodsInfo = {
  80. orderNo: result.orderNo,
  81. createTime: result.createTime,
  82. wechatStatus: result.wechatStatus,
  83. goods: tempGoods,
  84. refundTime: firstGoods.refundTime,
  85. refundAmount: firstGoods.refundAmount,
  86. refundStyleStr
  87. }
  88. this.setData({
  89. goodsInfo,
  90. status: result.wechatStatus
  91. }, () => {
  92. callback && typeof callback === 'function' && callback()
  93. })
  94. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  95. const firstGoods = tempGoods[0]
  96. if(firstGoods?.shortUrl) {
  97. this.setData({
  98. showCanvas: true
  99. }, () => {
  100. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  101. })
  102. }
  103. }
  104. }
  105. } catch (error) {
  106. console.log(error, "error");
  107. }
  108. },
  109. // 格式化价格
  110. formatPrice(price: number, type?: string) {
  111. const amountStr = price.toFixed(2)
  112. const [integerPart, decimalPart] = amountStr.split('.');
  113. if(type === 'ALL') {
  114. return amountStr
  115. }
  116. return {
  117. integerPart,
  118. decimalPart
  119. }
  120. },
  121. // 格式化类型
  122. formatPeriod(num: number, type: string) {
  123. const template: any = {
  124. DAY: "天卡",
  125. MONTH: "月卡",
  126. YEAR: "年卡"
  127. }
  128. if(type === "YEAR" && num >= 99) {
  129. return '终生卡'
  130. }
  131. return num + template[type]
  132. },
  133. onSubmit() {
  134. wx.redirectTo({
  135. url: '../index/index'
  136. })
  137. },
  138. setCanvasSize: function () {
  139. var size = {} as any;
  140. try {
  141. const res = wx.getWindowInfo()
  142. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  143. var width = res.windowWidth / scale;
  144. var height = width; //canvas画布为正方形
  145. size.w = width;
  146. size.h = height;
  147. } catch (e) {
  148. // Do something when catch error
  149. console.log("获取设备信息失败" + e);
  150. }
  151. return size;
  152. },
  153. createQrCode(content: any, canvasId: any) {
  154. const size = this.setCanvasSize();
  155. drawQrcode({
  156. width: size.w,
  157. height: size.h,
  158. canvasId: canvasId,
  159. text: content,
  160. callback: () => {
  161. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  162. setTimeout(() => {
  163. wx.canvasToTempFilePath(
  164. {
  165. canvasId: canvasId,
  166. success: (res) => {
  167. this.setData({
  168. canvasImg: res.tempFilePath,
  169. });
  170. },
  171. },
  172. this
  173. );
  174. }, 500);
  175. },
  176. });
  177. },
  178. onTimeout() {
  179. // 轮询10次查询订单状态
  180. const { goodsInfo, timerCount, timer } = this.data
  181. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  182. let count = timerCount
  183. const tempT = setTimeout(async () => {
  184. count += 1
  185. await this.getDetail()
  186. this.setData({
  187. timer: tempT,
  188. timerCount: count
  189. }, () => {
  190. this.onTimeout()
  191. })
  192. }, 3000);
  193. } else {
  194. clearTimeout(timer)
  195. }
  196. }
  197. })