order-result.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // pages/orders/order-detail.ts
  2. import drawQrcode from "../../utils/weapp.qrcode.esm";
  3. import { api_userPaymentCancelRefund, api_userPaymentOrderDetail } from "../../api/login";
  4. // 获取应用实例
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. serviceShow: true,
  11. status: 'WAIT_PAY',
  12. statusList: {
  13. WAIT_PAY: {
  14. logo: './images/ing.png',
  15. title: '待付款',
  16. content: '请尽快完成支付,以便我们为您处理订单'
  17. },
  18. PAID: {
  19. logo: './images/success.png',
  20. title: '已完成',
  21. content: '登录「音乐数字课堂」APP使用AI学练'
  22. },
  23. CLOSED: {
  24. logo: './images/error.png',
  25. title: '已取消',
  26. content: '您的交易订单已关闭'
  27. },
  28. WAIT_USE: {
  29. logo: './images/wait.png',
  30. title: '待使用',
  31. content: '请尽快扫描下方二维码进行激活'
  32. },
  33. REFUNDING: {
  34. logo: './images/refounding.png',
  35. title: '退款中',
  36. content: '您的退款申请正在处理,预计7个工作日内完成审核'
  37. },
  38. REFUNDED: {
  39. logo: './images/refounded.png',
  40. title: '退款成功',
  41. content: '您的退款已成功处理,感谢您的理解和支持'
  42. }
  43. },
  44. timerCount: 0,
  45. timer: null as any,
  46. goodsInfo: {} as any,
  47. tabIdx: 0, // 当前是从哪个tab来的
  48. orderNo: "" as string,
  49. showCanvas: false, // 是否显示二维码
  50. canvasImg: "" as string,
  51. refoundStatus: false,
  52. },
  53. /**
  54. * 生命周期函数--监听页面加载
  55. */
  56. onLoad(options: any) {
  57. if (options.orderNo) {
  58. this.setData({
  59. orderNo: options.orderNo,
  60. tabIdx: options.tabIdx
  61. }, () => {
  62. this.getDetail(this.onTimeout)
  63. });
  64. }
  65. },
  66. onShow() {
  67. this.setData({
  68. serviceShow: true
  69. })
  70. },
  71. onHide() {
  72. this.setData({
  73. serviceShow: false
  74. })
  75. },
  76. async getDetail(callback?: any) {
  77. try {
  78. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  79. if (data.code == 200) {
  80. const result = data.data || {}
  81. const goodsInfos = result.goodsInfos || []
  82. const tempGoods: any = []
  83. goodsInfos.forEach((item: any) => {
  84. tempGoods.push({
  85. ...item,
  86. shortUrl: item.activationCodeInfo.shortUrl,
  87. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  88. typeName: this.formatPeriod(item.activationCodeInfo?.times || 1, item.activationCodeInfo.type)
  89. })
  90. })
  91. let refundStyleStr = ''
  92. if(result.refundStyle === 'TURN_BACK') {
  93. refundStyleStr = '原路返回'
  94. } else if(result.refundStyle === 'OFFLINE') {
  95. refundStyleStr = '线下'
  96. }
  97. const goodsInfo = {
  98. orderNo: result.orderNo,
  99. createTime: result.createTime,
  100. wechatStatus: result.wechatStatus,
  101. goods: tempGoods,
  102. refundOrderId: result.refundOrderId,
  103. refundTime: result.refundTime,
  104. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  105. refundStyleStr
  106. }
  107. this.setData({
  108. goodsInfo,
  109. status: result.wechatStatus
  110. }, () => {
  111. callback && typeof callback === 'function' && callback()
  112. })
  113. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  114. const firstGoods = tempGoods[0]
  115. if(firstGoods?.shortUrl) {
  116. this.setData({
  117. showCanvas: true
  118. }, () => {
  119. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  120. })
  121. }
  122. }
  123. }
  124. } catch (error) {
  125. console.log(error, "error");
  126. }
  127. },
  128. // 格式化价格
  129. formatPrice(price: number, type?: string) {
  130. const amountStr = price.toFixed(2)
  131. const [integerPart, decimalPart] = amountStr.split('.');
  132. if(type === 'ALL') {
  133. return amountStr
  134. }
  135. return {
  136. integerPart,
  137. decimalPart
  138. }
  139. },
  140. // 格式化类型
  141. formatPeriod(num: number, type: string) {
  142. const template: any = {
  143. DAY: "天卡",
  144. MONTH: "月卡",
  145. YEAR: "年卡"
  146. }
  147. if(type === "YEAR" && num >= 99) {
  148. return '永久卡'
  149. }
  150. return num + template[type]
  151. },
  152. onSubmit() {
  153. wx.redirectTo({
  154. url: '../index/index'
  155. })
  156. },
  157. setCanvasSize: function () {
  158. var size = {} as any;
  159. try {
  160. const res = wx.getWindowInfo()
  161. var scale = 750 / 202; //不同屏幕下canvas的适配比例;设计稿是750宽
  162. var width = res.windowWidth / scale;
  163. var height = width; //canvas画布为正方形
  164. size.w = width;
  165. size.h = height;
  166. } catch (e) {
  167. // Do something when catch error
  168. console.log("获取设备信息失败" + e);
  169. }
  170. return size;
  171. },
  172. createQrCode(content: any, canvasId: any) {
  173. const size = this.setCanvasSize();
  174. drawQrcode({
  175. width: size.w,
  176. height: size.h,
  177. canvasId: canvasId,
  178. text: content,
  179. callback: () => {
  180. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  181. setTimeout(() => {
  182. wx.canvasToTempFilePath(
  183. {
  184. canvasId: canvasId,
  185. success: (res) => {
  186. this.setData({
  187. canvasImg: res.tempFilePath,
  188. });
  189. // console.log(res.tempFilePath, 'res.tempFilePath', 'https://oss.dayaedu.com/ktyq/1732174043543.png')
  190. },
  191. },
  192. this
  193. );
  194. }, 300);
  195. },
  196. });
  197. },
  198. onTimeout() {
  199. // 轮询10次查询订单状态
  200. const goodsInfo = this.data.goodsInfo
  201. const timerCount = this.data.timerCount
  202. const timer = this.data.timer
  203. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  204. let count = timerCount
  205. const tempT = setTimeout(async () => {
  206. count += 1
  207. await this.getDetail()
  208. this.setData({
  209. timer: tempT,
  210. timerCount: count
  211. }, () => {
  212. this.onTimeout()
  213. })
  214. }, 3000);
  215. } else {
  216. clearTimeout(timer)
  217. }
  218. },
  219. /** 申请退款 */
  220. async cancelRefound() {
  221. try {
  222. const {data} = await api_userPaymentCancelRefund(this.data.goodsInfo.refundOrderId)
  223. // console.log(data, 'data')
  224. if(data.code == 200) {
  225. wx.showToast({ title: '取消退款成功', icon: 'none' })
  226. // setTimeout(() => {
  227. // wx.navigateBack({
  228. // delta: 1
  229. // })
  230. // }, 1000);
  231. this.getDetail()
  232. } else {
  233. wx.showToast({ title: data.message, icon: 'none' })
  234. }
  235. } catch {}
  236. },
  237. /** 申请退款 */
  238. useRefound() {
  239. this.setData({
  240. refoundStatus: true
  241. })
  242. },
  243. changeRefoundStatus(e: {detail: any}) {
  244. this.setData({
  245. refoundStatus: e.detail
  246. })
  247. },
  248. onRefoundComfirm() {
  249. this.setData({
  250. refoundStatus: false
  251. })
  252. // wx.navigateBack({
  253. // delta: 1
  254. // })
  255. this.getDetail()
  256. },
  257. onCopy(e: { currentTarget: any }) {
  258. wx.setClipboardData({
  259. data: e.currentTarget.dataset.orderno,
  260. success: () => {
  261. wx.showToast({title: '复制成功', icon: 'none'})
  262. },
  263. fail: () => {
  264. wx.showToast({title: '复制失败,请稍后再试', icon: 'none'})
  265. }
  266. })
  267. }
  268. })