order-result.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // pages/orders/order-detail.ts
  2. import drawQrcode from "../../utils/weapp.qrcode.esm";
  3. import { api_userPaymentCancelRefund, api_userPaymentOrderDetail, api_userPaymentOrderRefundPayment } 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. refoundStatus: 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. refundOrderId: result.refundOrderId,
  90. refundTime: result.refundTime,
  91. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  92. refundStyleStr
  93. }
  94. this.setData({
  95. goodsInfo,
  96. status: result.wechatStatus
  97. }, () => {
  98. callback && typeof callback === 'function' && callback()
  99. })
  100. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  101. const firstGoods = tempGoods[0]
  102. if(firstGoods?.shortUrl) {
  103. this.setData({
  104. showCanvas: true
  105. }, () => {
  106. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  107. })
  108. }
  109. }
  110. }
  111. } catch (error) {
  112. console.log(error, "error");
  113. }
  114. },
  115. // 格式化价格
  116. formatPrice(price: number, type?: string) {
  117. const amountStr = price.toFixed(2)
  118. const [integerPart, decimalPart] = amountStr.split('.');
  119. if(type === 'ALL') {
  120. return amountStr
  121. }
  122. return {
  123. integerPart,
  124. decimalPart
  125. }
  126. },
  127. // 格式化类型
  128. formatPeriod(num: number, type: string) {
  129. const template: any = {
  130. DAY: "天卡",
  131. MONTH: "月卡",
  132. YEAR: "年卡"
  133. }
  134. if(type === "YEAR" && num >= 99) {
  135. return '永久卡'
  136. }
  137. return num + template[type]
  138. },
  139. onSubmit() {
  140. wx.redirectTo({
  141. url: '../index/index'
  142. })
  143. },
  144. setCanvasSize: function () {
  145. const size = {} as any;
  146. try {
  147. const res = wx.getWindowInfo()
  148. const scale = 750 / 300; //不同屏幕下canvas的适配比例;设计稿是750宽
  149. const width = res.windowWidth / scale;
  150. const height = width; //canvas画布为正方形
  151. size.w = width;
  152. size.h = height;
  153. } catch (e) {
  154. // Do something when catch error
  155. console.log("获取设备信息失败" + e);
  156. }
  157. return size;
  158. },
  159. createQrCode(content: any, canvasId: any) {
  160. const size = this.setCanvasSize();
  161. drawQrcode({
  162. width: size.w,
  163. height: size.h,
  164. canvasId: canvasId,
  165. text: content,
  166. callback: () => {
  167. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  168. setTimeout(() => {
  169. wx.canvasToTempFilePath(
  170. {
  171. canvasId: canvasId,
  172. success: (res) => {
  173. this.setData({
  174. canvasImg: res.tempFilePath,
  175. });
  176. },
  177. },
  178. this
  179. );
  180. }, 500);
  181. },
  182. });
  183. },
  184. onTimeout() {
  185. // 轮询10次查询订单状态
  186. const goodsInfo = this.data.goodsInfo
  187. const timerCount = this.data.timerCount
  188. const timer = this.data.timer
  189. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  190. let count = timerCount
  191. const tempT = setTimeout(async () => {
  192. count += 1
  193. await this.getDetail()
  194. this.setData({
  195. timer: tempT,
  196. timerCount: count
  197. }, () => {
  198. this.onTimeout()
  199. })
  200. }, 3000);
  201. } else {
  202. clearTimeout(timer)
  203. }
  204. },
  205. openService() {
  206. wx.navigateTo({
  207. url: '../service/service'
  208. })
  209. },
  210. /** 申请退款 */
  211. async cancelRefound() {
  212. try {
  213. const {data} = await api_userPaymentCancelRefund(this.data.goodsInfo.refundOrderId)
  214. // console.log(data, 'data')
  215. if(data.code == 200) {
  216. wx.showToast({ title: '取消退款成功', icon: 'none' })
  217. // setTimeout(() => {
  218. // wx.navigateBack({
  219. // delta: 1
  220. // })
  221. // }, 1000);
  222. this.getDetail()
  223. } else {
  224. wx.showToast({ title: data.message, icon: 'none' })
  225. }
  226. } catch {}
  227. },
  228. /** 申请退款 */
  229. useRefound() {
  230. this.setData({
  231. refoundStatus: true
  232. })
  233. },
  234. changeRefoundStatus(e: {detail: any}) {
  235. this.setData({
  236. refoundStatus: e.detail
  237. })
  238. },
  239. onRefoundComfirm() {
  240. this.setData({
  241. refoundStatus: false
  242. })
  243. // wx.navigateBack({
  244. // delta: 1
  245. // })
  246. this.getDetail()
  247. },
  248. onCopy(e: { currentTarget: any }) {
  249. wx.setClipboardData({
  250. data: e.currentTarget.dataset.orderno,
  251. success: () => {
  252. wx.showToast({title: '复制成功', icon: 'none'})
  253. },
  254. fail: () => {
  255. wx.showToast({title: '复制失败,请稍后再试', icon: 'none'})
  256. }
  257. })
  258. }
  259. })