order-result.ts 7.4 KB

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