order-result.ts 7.2 KB

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