order-result.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. 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. orderNo: "" as string,
  48. showCanvas: false, // 是否显示二维码
  49. canvasImg: "" as string,
  50. refoundStatus: false,
  51. refoundValue: "", // 退款内容
  52. refoundPhone: ''
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad(options: any) {
  58. if (options.orderNo) {
  59. this.setData({
  60. orderNo: options.orderNo
  61. }, () => {
  62. this.getDetail(this.onTimeout)
  63. });
  64. }
  65. },
  66. onShow() {
  67. this.setData({
  68. serviceShow: true
  69. })
  70. },
  71. onHide() {
  72. this.setData({
  73. serviceShow: false
  74. })
  75. },
  76. async getDetail(callback?: any) {
  77. try {
  78. const { data } = await api_userPaymentOrderDetail(this.data.orderNo);
  79. if (data.code == 200) {
  80. const result = data.data || {}
  81. const goodsInfos = result.goodsInfos || []
  82. const tempGoods: any = []
  83. goodsInfos.forEach((item: any) => {
  84. tempGoods.push({
  85. ...item,
  86. shortUrl: item.activationCodeInfo.shortUrl,
  87. originalPrice: this.formatPrice(item.paymentCashAmount, 'ALL'),
  88. typeName: this.formatPeriod(item.activationCodeInfo?.times || 1, item.activationCodeInfo.type)
  89. })
  90. })
  91. let refundStyleStr = ''
  92. if(result.refundStyle === 'TURN_BACK') {
  93. refundStyleStr = '原路返回'
  94. } else if(result.refundStyle === 'OFFLINE') {
  95. refundStyleStr = '线下'
  96. }
  97. const goodsInfo = {
  98. orderNo: result.orderNo,
  99. createTime: result.createTime,
  100. wechatStatus: result.wechatStatus,
  101. goods: tempGoods,
  102. refundOrderId: result.refundOrderId,
  103. refundTime: result.refundTime,
  104. refundAmount: this.formatPrice(result.refundAmount || 0, 'ALL'),
  105. refundStyleStr
  106. }
  107. this.setData({
  108. goodsInfo,
  109. status: result.wechatStatus
  110. }, () => {
  111. callback && typeof callback === 'function' && callback()
  112. })
  113. if(result.wechatStatus != 'CLOSED' || result.wechatStatus != 'WAIT_PAY') {
  114. const firstGoods = tempGoods[0]
  115. if(firstGoods?.shortUrl) {
  116. this.setData({
  117. showCanvas: true
  118. }, () => {
  119. this.createQrCode(firstGoods?.shortUrl, 'canvasCode')
  120. })
  121. }
  122. }
  123. }
  124. } catch (error) {
  125. console.log(error, "error");
  126. }
  127. },
  128. // 格式化价格
  129. formatPrice(price: number, type?: string) {
  130. const amountStr = price.toFixed(2)
  131. const [integerPart, decimalPart] = amountStr.split('.');
  132. if(type === 'ALL') {
  133. return amountStr
  134. }
  135. return {
  136. integerPart,
  137. decimalPart
  138. }
  139. },
  140. // 格式化类型
  141. formatPeriod(num: number, type: string) {
  142. const template: any = {
  143. DAY: "天卡",
  144. MONTH: "月卡",
  145. YEAR: "年卡"
  146. }
  147. if(type === "YEAR" && num >= 99) {
  148. return '永久卡'
  149. }
  150. return num + template[type]
  151. },
  152. onSubmit() {
  153. wx.redirectTo({
  154. url: '../index/index'
  155. })
  156. },
  157. setCanvasSize: function () {
  158. var size = {} as any;
  159. try {
  160. const res = wx.getWindowInfo()
  161. var scale = 750 / 262; //不同屏幕下canvas的适配比例;设计稿是750宽
  162. var width = res.windowWidth / scale;
  163. var height = width; //canvas画布为正方形
  164. size.w = width;
  165. size.h = height;
  166. } catch (e) {
  167. // Do something when catch error
  168. console.log("获取设备信息失败" + e);
  169. }
  170. return size;
  171. },
  172. createQrCode(content: any, canvasId: any) {
  173. const size = this.setCanvasSize();
  174. drawQrcode({
  175. width: size.w,
  176. height: size.h,
  177. canvasId: canvasId,
  178. text: content,
  179. callback: () => {
  180. // 安卓机上不准确,生成的二维码无法扫描,加延时解决
  181. setTimeout(() => {
  182. wx.canvasToTempFilePath(
  183. {
  184. canvasId: canvasId,
  185. success: (res) => {
  186. this.setData({
  187. canvasImg: res.tempFilePath,
  188. });
  189. },
  190. },
  191. this
  192. );
  193. }, 500);
  194. },
  195. });
  196. },
  197. onTimeout() {
  198. // 轮询10次查询订单状态
  199. const goodsInfo = this.data.goodsInfo
  200. const timerCount = this.data.timerCount
  201. const timer = this.data.timer
  202. if(goodsInfo.wechatStatus === 'WAIT_PAY' && timerCount <= 10) {
  203. let count = timerCount
  204. const tempT = setTimeout(async () => {
  205. count += 1
  206. await this.getDetail()
  207. this.setData({
  208. timer: tempT,
  209. timerCount: count
  210. }, () => {
  211. this.onTimeout()
  212. })
  213. }, 3000);
  214. } else {
  215. clearTimeout(timer)
  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. } else {
  231. wx.showToast({ title: data.message, icon: 'none' })
  232. }
  233. } catch {}
  234. },
  235. /** 申请退款 */
  236. useRefound() {
  237. this.setData({
  238. refoundStatus: true,
  239. refoundValue: ""
  240. })
  241. },
  242. textareaInput(e: { detail: any }) {
  243. this.setData({
  244. refoundValue: e.detail.value
  245. })
  246. },
  247. phoneInput(e: {detail: any}) {
  248. this.setData({
  249. refoundPhone: e.detail.value
  250. })
  251. },
  252. onRefoundClose() {
  253. this.setData({
  254. refoundStatus: false,
  255. refoundValue: ''
  256. })
  257. },
  258. checkPhone(phone: string) {
  259. const phoneRule =
  260. /^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-9]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\d{8}$/;
  261. return phoneRule.test(phone);
  262. },
  263. async onRefound() {
  264. const refoundValue = this.data.refoundValue
  265. const refoundPhone = this.data.refoundPhone
  266. if(!refoundValue) {
  267. wx.showToast({ title: '请输入退款原因', icon: 'none' })
  268. return
  269. }
  270. if(!this.checkPhone(refoundPhone)) {
  271. wx.showToast({ title: '请输入正确的手机号码', icon: 'none' })
  272. return
  273. }
  274. try {
  275. const { goods, orderNo } = this.data.goodsInfo
  276. const details: any = []
  277. goods.forEach((item: any) => {
  278. details.push({
  279. num: item.goodsNum,
  280. onlyRefund: false,
  281. userPaymentOrderDetailId: item.id,
  282. refundAmount: item.currentPrice
  283. })
  284. })
  285. const params = {
  286. merOrderNo: orderNo,
  287. serviceCharge: false,
  288. refundReason: refoundValue,
  289. phone: refoundPhone, // 手机号
  290. serviceChargeFee: 0,
  291. userRefundOrderDetails: details
  292. }
  293. const {data} = await api_userPaymentOrderRefundPayment(params)
  294. if(data.code == 200) {
  295. wx.showToast({ title: '申请成功', icon: 'none' })
  296. this.setData({
  297. refoundStatus: false
  298. })
  299. setTimeout(() => {
  300. wx.navigateBack({
  301. delta: 1
  302. })
  303. }, 1000);
  304. } else {
  305. wx.showToast({ title: data.message, icon: 'none' })
  306. }
  307. } catch {}
  308. },
  309. onCopy(e: { currentTarget: any }) {
  310. console.log(e, 'e')
  311. wx.setClipboardData({
  312. data: e.currentTarget.dataset.orderno,
  313. success: () => {
  314. wx.showToast({title: '复制成功', icon: 'none'})
  315. },
  316. fail: () => {
  317. wx.showToast({title: '复制失败,请稍后再试', icon: 'none'})
  318. }
  319. })
  320. }
  321. })