order-result.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. },
  33. goodsInfo: {} as any,
  34. orderNo: "" as string,
  35. canvasImg: "" as string
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad(options: any) {
  41. if (options.orderNo) {
  42. this.setData({
  43. orderNo: options.orderNo
  44. }, () => {
  45. this.getDetail()
  46. });
  47. }
  48. },
  49. async getDetail() {
  50. try {
  51. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  52. if (data.code == 200) {
  53. const result = data.data || {}
  54. const goodsInfos = result.goodsInfos || []
  55. const tempGoods: any = []
  56. goodsInfos.forEach((item: any) => {
  57. tempGoods.push({
  58. ...item,
  59. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  60. typeName: this.formatPeriod(item.goodsNum, item.activationCodeInfo.type)
  61. })
  62. })
  63. const goodsInfo = {
  64. orderNo: result.orderNo,
  65. createTime: result.createTime,
  66. wechatStatus: result.wechatStatus,
  67. goods: tempGoods
  68. }
  69. this.setData({
  70. goodsInfo,
  71. status: result.wechatStatus
  72. })
  73. if(result.wechatStatus != 'CLOSED') {
  74. this.createQrCode(
  75. 'https://www.baidu.com/',
  76. 'canvasCode')
  77. }
  78. }
  79. } catch (error) {
  80. console.log(error, "error");
  81. }
  82. },
  83. // 格式化价格
  84. formatPrice(price: number, type?: string) {
  85. const amountStr = price.toFixed(2)
  86. const [integerPart, decimalPart] = amountStr.split('.');
  87. if(type === 'ALL') {
  88. return amountStr
  89. }
  90. return {
  91. integerPart,
  92. decimalPart
  93. }
  94. },
  95. // 格式化类型
  96. formatPeriod(num: number, type: string) {
  97. const template: any = {
  98. DAY: "天卡",
  99. MONTH: "月卡",
  100. YEAR: "年卡"
  101. }
  102. if(type === "YEAR" && num >= 99) {
  103. return '终生卡'
  104. }
  105. return num + template[type]
  106. },
  107. onSubmit() {
  108. wx.redirectTo({
  109. url: '../index/index'
  110. })
  111. },
  112. setCanvasSize: function () {
  113. var size = {} as any;
  114. try {
  115. const res = wx.getWindowInfo()
  116. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  117. var width = res.windowWidth / scale;
  118. var height = width; //canvas画布为正方形
  119. size.w = width;
  120. size.h = height;
  121. } catch (e) {
  122. // Do something when catch error
  123. console.log("获取设备信息失败" + e);
  124. }
  125. return size;
  126. },
  127. setLogoSize() {
  128. var size = {} as any;
  129. try {
  130. var res = wx.getSystemInfoSync();
  131. var scale = 750 / 48; //不同屏幕下canvas的适配比例;设计稿是750宽
  132. var width = res.windowWidth / scale;
  133. var height = width; //canvas画布为正方形
  134. size.w = width;
  135. size.h = height;
  136. } catch (e) {
  137. // Do something when catch error
  138. console.log("获取设备信息失败" + e);
  139. }
  140. return size;
  141. },
  142. createQrCode(content: any, canvasId: any) {
  143. const size = this.setCanvasSize();
  144. // const logoSize = this.setLogoSize();
  145. drawQrcode({
  146. width: size.w,
  147. height: size.h,
  148. canvasId: canvasId,
  149. // ctx: wx.createCanvasContext('myQrcode'),
  150. text: content,
  151. // v1.0.0+版本支持在二维码上绘制图片
  152. // image: {
  153. // imageResource: "./images/codeLogo.png",
  154. // dx: (size.w - logoSize.w) / 2,
  155. // dy: (size.h - logoSize.h) / 2,
  156. // dWidth: logoSize.w,
  157. // dHeight: logoSize.h,
  158. // },
  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. })