order-result.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. PAID: {
  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. let refundStyleStr = ''
  73. if(result.refundStyle === 'TURN_BACK') {
  74. refundStyleStr = '原路返回'
  75. } else if(result.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: result.refundTime,
  84. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  85. refundStyleStr
  86. }
  87. this.setData({
  88. goodsInfo,
  89. status: result.wechatStatus
  90. }, () => {
  91. callback && typeof callback === 'function' && callback()
  92. })
  93. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  94. const firstGoods = tempGoods[0]
  95. if(firstGoods?.shortUrl) {
  96. this.setData({
  97. showCanvas: true
  98. }, () => {
  99. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  100. })
  101. }
  102. }
  103. }
  104. } catch (error) {
  105. console.log(error, "error");
  106. }
  107. },
  108. // 格式化价格
  109. formatPrice(price: number, type?: string) {
  110. const amountStr = price.toFixed(2)
  111. const [integerPart, decimalPart] = amountStr.split('.');
  112. if(type === 'ALL') {
  113. return amountStr
  114. }
  115. return {
  116. integerPart,
  117. decimalPart
  118. }
  119. },
  120. // 格式化类型
  121. formatPeriod(num: number, type: string) {
  122. const template: any = {
  123. DAY: "天卡",
  124. MONTH: "月卡",
  125. YEAR: "年卡"
  126. }
  127. if(type === "YEAR" && num >= 99) {
  128. return '终生卡'
  129. }
  130. return num + template[type]
  131. },
  132. onSubmit() {
  133. wx.redirectTo({
  134. url: '../index/index'
  135. })
  136. },
  137. setCanvasSize: function () {
  138. var size = {} as any;
  139. try {
  140. const res = wx.getWindowInfo()
  141. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  142. var width = res.windowWidth / scale;
  143. var height = width; //canvas画布为正方形
  144. size.w = width;
  145. size.h = height;
  146. } catch (e) {
  147. // Do something when catch error
  148. console.log("获取设备信息失败" + e);
  149. }
  150. return size;
  151. },
  152. createQrCode(content: any, canvasId: any) {
  153. const size = this.setCanvasSize();
  154. drawQrcode({
  155. width: size.w,
  156. height: size.h,
  157. canvasId: canvasId,
  158. text: content,
  159. callback: () => {
  160. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  161. setTimeout(() => {
  162. wx.canvasToTempFilePath(
  163. {
  164. canvasId: canvasId,
  165. success: (res) => {
  166. this.setData({
  167. canvasImg: res.tempFilePath,
  168. });
  169. },
  170. },
  171. this
  172. );
  173. }, 500);
  174. },
  175. });
  176. },
  177. onTimeout() {
  178. // 轮询10次查询订单状态
  179. const { goodsInfo, timerCount, timer } = this.data
  180. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  181. let count = timerCount
  182. const tempT = setTimeout(async () => {
  183. count += 1
  184. await this.getDetail()
  185. this.setData({
  186. timer: tempT,
  187. timerCount: count
  188. }, () => {
  189. this.onTimeout()
  190. })
  191. }, 3000);
  192. } else {
  193. clearTimeout(timer)
  194. }
  195. }
  196. })