order-result.ts 8.9 KB

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