order-result.ts 8.2 KB

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