order-result.ts 8.5 KB

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