order-result.ts 7.2 KB

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