order-result.ts 8.3 KB

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