order-result.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. cancelRefoundStatus: false,
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad(options: any) {
  58. if (options.orderNo) {
  59. this.setData({
  60. orderNo: options.orderNo,
  61. tabIdx: options.tabIdx
  62. }, () => {
  63. this.getDetail(this.onTimeout)
  64. });
  65. }
  66. },
  67. onShow() {
  68. this.setData({
  69. serviceShow: true
  70. })
  71. },
  72. onHide() {
  73. this.setData({
  74. serviceShow: false
  75. })
  76. },
  77. async getDetail(callback?: any) {
  78. try {
  79. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  80. if (data.code == 200) {
  81. const result = data.data || {}
  82. const goodsInfos = result.goodsInfos || []
  83. const tempGoods: any = []
  84. goodsInfos.forEach((item: any) => {
  85. tempGoods.push({
  86. ...item,
  87. shortUrl: item.activationCodeInfo.shortUrl,
  88. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  89. typeName: this.formatPeriod(item.activationCodeInfo?.times || 1, item.activationCodeInfo.type)
  90. })
  91. })
  92. let refundStyleStr = ''
  93. if(result.refundStyle === 'TURN_BACK') {
  94. refundStyleStr = '原路返回'
  95. } else if(result.refundStyle === 'OFFLINE') {
  96. refundStyleStr = '线下'
  97. }
  98. const goodsInfo = {
  99. orderNo: result.orderNo,
  100. createTime: result.createTime,
  101. wechatStatus: result.wechatStatus,
  102. goods: tempGoods,
  103. refundOrderId: result.refundOrderId,
  104. refundTime: result.refundTime,
  105. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  106. refundStyleStr
  107. }
  108. this.setData({
  109. goodsInfo,
  110. status: result.wechatStatus
  111. }, () => {
  112. callback && typeof callback === 'function' && callback()
  113. })
  114. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  115. const firstGoods = tempGoods[0]
  116. if(firstGoods?.shortUrl) {
  117. this.setData({
  118. showCanvas: true
  119. }, () => {
  120. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  121. })
  122. }
  123. }
  124. }
  125. } catch (error) {
  126. console.log(error, "error");
  127. }
  128. },
  129. // 格式化价格
  130. formatPrice(price: number, type?: string) {
  131. const amountStr = price.toFixed(2)
  132. const [integerPart, decimalPart] = amountStr.split('.');
  133. if(type === 'ALL') {
  134. return amountStr
  135. }
  136. return {
  137. integerPart,
  138. decimalPart
  139. }
  140. },
  141. // 格式化类型
  142. formatPeriod(num: number, type: string) {
  143. const template: any = {
  144. DAY: "天卡",
  145. MONTH: "月卡",
  146. YEAR: "年卡"
  147. }
  148. if(type === "YEAR" && num >= 99) {
  149. return '永久卡'
  150. }
  151. return num + template[type]
  152. },
  153. onSubmit() {
  154. wx.redirectTo({
  155. url: '../index/index'
  156. })
  157. },
  158. setCanvasSize: function () {
  159. var size = {} as any;
  160. try {
  161. const res = wx.getWindowInfo()
  162. var scale = 750 / 202; //不同屏幕下canvas的适配比例;设计稿是750宽
  163. var width = res.windowWidth / scale;
  164. var height = width; //canvas画布为正方形
  165. size.w = width;
  166. size.h = height;
  167. } catch (e) {
  168. // Do something when catch error
  169. console.log("获取设备信息失败" + e);
  170. }
  171. return size;
  172. },
  173. createQrCode(content: any, canvasId: any) {
  174. const size = this.setCanvasSize();
  175. drawQrcode({
  176. width: size.w,
  177. height: size.h,
  178. canvasId: canvasId,
  179. text: content,
  180. callback: () => {
  181. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  182. setTimeout(() => {
  183. wx.canvasToTempFilePath(
  184. {
  185. canvasId: canvasId,
  186. success: (res) => {
  187. this.setData({
  188. canvasImg: res.tempFilePath,
  189. });
  190. // console.log(res.tempFilePath, 'res.tempFilePath', 'https://oss.dayaedu.com/ktyq/1732174043543.png')
  191. },
  192. },
  193. this
  194. );
  195. }, 300);
  196. },
  197. });
  198. },
  199. onTimeout() {
  200. // 轮询10次查询订单状态
  201. const goodsInfo = this.data.goodsInfo
  202. const timerCount = this.data.timerCount
  203. const timer = this.data.timer
  204. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  205. let count = timerCount
  206. const tempT = setTimeout(async () => {
  207. count += 1
  208. await this.getDetail()
  209. this.setData({
  210. timer: tempT,
  211. timerCount: count
  212. }, () => {
  213. this.onTimeout()
  214. })
  215. }, 3000);
  216. } else {
  217. clearTimeout(timer)
  218. }
  219. },
  220. /** 申请退款 */
  221. async cancelRefound() {
  222. this.setData({
  223. cancelRefoundStatus: true
  224. }, async () => {
  225. try {
  226. const {data} = await api_userPaymentCancelRefund(this.data.goodsInfo.refundOrderId)
  227. // console.log(data, 'data')
  228. if(data.code == 200) {
  229. wx.showToast({ title: '取消退款成功', icon: 'none' })
  230. this.getDetail()
  231. } else {
  232. wx.showToast({ title: data.message, icon: 'none' })
  233. }
  234. setTimeout(() => {
  235. this.setData({
  236. cancelRefoundStatus: false
  237. })
  238. }, 500);
  239. } catch {}
  240. })
  241. },
  242. /** 申请退款 */
  243. useRefound() {
  244. this.setData({
  245. refoundStatus: true
  246. })
  247. },
  248. changeRefoundStatus(e: {detail: any}) {
  249. this.setData({
  250. refoundStatus: e.detail
  251. })
  252. },
  253. onRefoundComfirm() {
  254. this.setData({
  255. refoundStatus: false
  256. })
  257. // wx.navigateBack({
  258. // delta: 1
  259. // })
  260. this.getDetail()
  261. },
  262. onCopy(e: { currentTarget: any }) {
  263. wx.setClipboardData({
  264. data: e.currentTarget.dataset.orderno,
  265. success: () => {
  266. wx.showToast({title: '复制成功', icon: 'none'})
  267. },
  268. fail: () => {
  269. wx.showToast({title: '复制失败,请稍后再试', icon: 'none'})
  270. }
  271. })
  272. }
  273. })