order-result.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. REFUNDING: {
  33. logo: './images/refounding.png',
  34. title: '退款中',
  35. content: '您的退款申请正在处理,预计7个工作日内完成审核'
  36. },
  37. REFUNDED: {
  38. logo: './images/refounded.png',
  39. title: '退款成功',
  40. content: '您的退款已成功处理,感谢您的理解和支持'
  41. }
  42. },
  43. timerCount: 0,
  44. timer: null as any,
  45. goodsInfo: {} as any,
  46. orderNo: "" as string,
  47. showCanvas: false, // 是否显示二维码
  48. canvasImg: "" as string,
  49. showService: false,
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad(options: any) {
  55. if (options.orderNo) {
  56. this.setData({
  57. orderNo: options.orderNo
  58. }, () => {
  59. this.getDetail(this.onTimeout)
  60. });
  61. }
  62. },
  63. async getDetail(callback?: any) {
  64. try {
  65. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  66. if (data.code == 200) {
  67. const result = data.data || {}
  68. const goodsInfos = result.goodsInfos || []
  69. const tempGoods: any = []
  70. goodsInfos.forEach((item: any) => {
  71. tempGoods.push({
  72. ...item,
  73. shortUrl: item.activationCodeInfo.shortUrl,
  74. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  75. typeName: this.formatPeriod(item.activationCodeInfo?.times || 1, item.activationCodeInfo.type)
  76. })
  77. })
  78. let refundStyleStr = ''
  79. if(result.refundStyle === 'TURN_BACK') {
  80. refundStyleStr = '原路返回'
  81. } else if(result.refundStyle === 'OFFLINE') {
  82. refundStyleStr = '线下'
  83. }
  84. const goodsInfo = {
  85. orderNo: result.orderNo,
  86. createTime: result.createTime,
  87. wechatStatus: result.wechatStatus,
  88. goods: tempGoods,
  89. refundTime: result.refundTime,
  90. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  91. refundStyleStr
  92. }
  93. this.setData({
  94. goodsInfo,
  95. status: result.wechatStatus
  96. }, () => {
  97. callback && typeof callback === 'function' && callback()
  98. })
  99. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  100. const firstGoods = tempGoods[0]
  101. if(firstGoods?.shortUrl) {
  102. this.setData({
  103. showCanvas: true
  104. }, () => {
  105. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  106. })
  107. }
  108. }
  109. }
  110. } catch (error) {
  111. console.log(error, "error");
  112. }
  113. },
  114. // 格式化价格
  115. formatPrice(price: number, type?: string) {
  116. const amountStr = price.toFixed(2)
  117. const [integerPart, decimalPart] = amountStr.split('.');
  118. if(type === 'ALL') {
  119. return amountStr
  120. }
  121. return {
  122. integerPart,
  123. decimalPart
  124. }
  125. },
  126. // 格式化类型
  127. formatPeriod(num: number, type: string) {
  128. const template: any = {
  129. DAY: "天卡",
  130. MONTH: "月卡",
  131. YEAR: "年卡"
  132. }
  133. if(type === "YEAR" && num >= 99) {
  134. return '终生卡'
  135. }
  136. return num + template[type]
  137. },
  138. onSubmit() {
  139. wx.redirectTo({
  140. url: '../index/index'
  141. })
  142. },
  143. setCanvasSize: function () {
  144. var size = {} as any;
  145. try {
  146. const res = wx.getWindowInfo()
  147. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  148. var width = res.windowWidth / scale;
  149. var height = width; //canvas画布为正方形
  150. size.w = width;
  151. size.h = height;
  152. } catch (e) {
  153. // Do something when catch error
  154. console.log("获取设备信息失败" + e);
  155. }
  156. return size;
  157. },
  158. createQrCode(content: any, canvasId: any) {
  159. const size = this.setCanvasSize();
  160. drawQrcode({
  161. width: size.w,
  162. height: size.h,
  163. canvasId: canvasId,
  164. text: content,
  165. callback: () => {
  166. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  167. setTimeout(() => {
  168. wx.canvasToTempFilePath(
  169. {
  170. canvasId: canvasId,
  171. success: (res) => {
  172. this.setData({
  173. canvasImg: res.tempFilePath,
  174. });
  175. },
  176. },
  177. this
  178. );
  179. }, 500);
  180. },
  181. });
  182. },
  183. onTimeout() {
  184. // 轮询10次查询订单状态
  185. const { goodsInfo, timerCount, timer } = this.data
  186. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  187. let count = timerCount
  188. const tempT = setTimeout(async () => {
  189. count += 1
  190. await this.getDetail()
  191. this.setData({
  192. timer: tempT,
  193. timerCount: count
  194. }, () => {
  195. this.onTimeout()
  196. })
  197. }, 3000);
  198. } else {
  199. clearTimeout(timer)
  200. }
  201. },
  202. /** 客服 */
  203. onService() {
  204. this.setData({
  205. showService: true
  206. })
  207. },
  208. changePop(event: { detail: any }) {
  209. this.setData({
  210. showService: event.detail
  211. })
  212. },
  213. })