order-result.ts 7.4 KB

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