order-result.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. goodsInfo: {} as any,
  39. orderNo: "" as string,
  40. showCanvas: false, // 是否显示二维码
  41. canvasImg: "" as string
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad(options: any) {
  47. if (options.orderNo) {
  48. console.log(options, 'options')
  49. this.setData({
  50. orderNo: options.orderNo
  51. }, () => {
  52. this.getDetail()
  53. });
  54. }
  55. },
  56. async getDetail() {
  57. try {
  58. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  59. if (data.code == 200) {
  60. const result = data.data || {}
  61. const goodsInfos = result.goodsInfos || []
  62. const tempGoods: any = []
  63. goodsInfos.forEach((item: any) => {
  64. tempGoods.push({
  65. ...item,
  66. shortUrl: item.activationCodeInfo.shortUrl,
  67. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  68. typeName: this.formatPeriod(item.goodsNum, item.activationCodeInfo.type)
  69. })
  70. })
  71. const firstGoods = tempGoods[0]
  72. let refundStyleStr = ''
  73. if(firstGoods?.refundStyle === 'TURN_BACK') {
  74. refundStyleStr = '原路返回'
  75. } else if(firstGoods?.refundStyle === 'OFFLINE') {
  76. refundStyleStr = '线下'
  77. }
  78. const goodsInfo = {
  79. orderNo: result.orderNo,
  80. createTime: result.createTime,
  81. wechatStatus: result.wechatStatus,
  82. goods: tempGoods,
  83. refundTime: firstGoods.refundTime,
  84. refundAmount: firstGoods.refundAmount,
  85. refundStyleStr
  86. }
  87. this.setData({
  88. goodsInfo,
  89. status: result.wechatStatus
  90. })
  91. if(result.wechatStatus != 'CLOSED') {
  92. const firstGoods = tempGoods[0]
  93. if(firstGoods?.shortUrl) {
  94. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  95. }
  96. }
  97. }
  98. } catch (error) {
  99. console.log(error, "error");
  100. }
  101. },
  102. // 格式化价格
  103. formatPrice(price: number, type?: string) {
  104. const amountStr = price.toFixed(2)
  105. const [integerPart, decimalPart] = amountStr.split('.');
  106. if(type === 'ALL') {
  107. return amountStr
  108. }
  109. return {
  110. integerPart,
  111. decimalPart
  112. }
  113. },
  114. // 格式化类型
  115. formatPeriod(num: number, type: string) {
  116. const template: any = {
  117. DAY: "天卡",
  118. MONTH: "月卡",
  119. YEAR: "年卡"
  120. }
  121. if(type === "YEAR" && num >= 99) {
  122. return '终生卡'
  123. }
  124. return num + template[type]
  125. },
  126. onSubmit() {
  127. wx.redirectTo({
  128. url: '../index/index'
  129. })
  130. },
  131. setCanvasSize: function () {
  132. var size = {} as any;
  133. try {
  134. const res = wx.getWindowInfo()
  135. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  136. var width = res.windowWidth / scale;
  137. var height = width; //canvas画布为正方形
  138. size.w = width;
  139. size.h = height;
  140. } catch (e) {
  141. // Do something when catch error
  142. console.log("获取设备信息失败" + e);
  143. }
  144. return size;
  145. },
  146. createQrCode(content: any, canvasId: any) {
  147. const size = this.setCanvasSize();
  148. drawQrcode({
  149. width: size.w,
  150. height: size.h,
  151. canvasId: canvasId,
  152. text: content,
  153. callback: () => {
  154. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  155. setTimeout(() => {
  156. wx.canvasToTempFilePath(
  157. {
  158. canvasId: canvasId,
  159. success: (res) => {
  160. this.setData({
  161. showCanvas: true,
  162. canvasImg: res.tempFilePath,
  163. });
  164. },
  165. },
  166. this
  167. );
  168. }, 500);
  169. },
  170. });
  171. },
  172. })