historyOrder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. const {
  2. default: api
  3. } = require("../../js/api");
  4. const util = require('../../utils/util.js')
  5. // pages/historyOrderAfter/historyOrderAfter.js
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. orders: [
  12. {
  13. order:{
  14. status: '未支付',
  15. statusColor: '#ff8d1a',
  16. oilName: "hk",
  17. nozzle: "1",
  18. volume: "1",
  19. amount: "1",
  20. payAmount: "1",
  21. discount: "1",
  22. orderId: "1",
  23. time: "2025-4-3",
  24. price:'666'
  25. },
  26. bottonText2: ""
  27. }
  28. ],
  29. date: '',
  30. startDate: '',
  31. endDate: '',
  32. pageNum: 1,//查询页码
  33. pageSize: 5,//页数
  34. isHaveOrder: true,//是否还有订单
  35. paymentMode: -1,
  36. /** 后支付订单状态 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成)*/
  37. payStatusValue: [{
  38. orderStatus: 0,
  39. status: '未支付',
  40. statusColor: '#ff8d1a'
  41. },
  42. {
  43. orderStatus: 1,
  44. status: '已完成',
  45. statusColor: '#a6a6a6'
  46. },
  47. {
  48. orderStatus: 5,
  49. status: '已完成',
  50. statusColor: '#a6a6a6'
  51. }
  52. ],
  53. /** 预支付订单状态 授权状态(未授权:0;已授权:1)
  54. * + 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成) */
  55. prepayStatuValue: [
  56. {
  57. authorizationStatus: 0,
  58. orderStatus: 0,
  59. status: '支付失败',
  60. statusColor: '#f1912f'
  61. },
  62. {
  63. authorizationStatus: 0,
  64. orderStatus: 1,
  65. status: '授权失败',
  66. statusColor: '#f0d02e'
  67. },
  68. {
  69. authorizationStatus: 1,
  70. orderStatus: 1,
  71. status: '授权成功',
  72. statusColor: '#a2cd4a'
  73. },
  74. {
  75. authorizationStatus: 0,
  76. orderStatus: 2,
  77. status: '已完成',
  78. statusColor: '#a6a6a6'
  79. },
  80. {
  81. authorizationStatus: 0,
  82. orderStatus: 3,
  83. status: '已完成',
  84. statusColor: '#a6a6a6'
  85. },
  86. {
  87. authorizationStatus: 0,
  88. orderStatus: 5,
  89. status: '已完成',
  90. statusColor: '#a6a6a6'
  91. },
  92. ],
  93. },
  94. /** 获取当前日期 */
  95. getNowDate() {
  96. const date = new Date(); // 获取当前日期
  97. const year = date.getFullYear(); // 获取年份
  98. const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份,补零
  99. const day = String(date.getDate()).padStart(2, '0'); // 获取日期,补零
  100. return `${year}-${month}-${day}`; // 拼接成 yyyy-MM-dd 格式
  101. },
  102. /** 获取上个月日期 */
  103. getLastMouthDate() {
  104. const currentDate = new Date(); // 获取当前日期
  105. const year = currentDate.getFullYear(); // 当前年份
  106. const month = currentDate.getMonth(); // 当前月份(0-11)
  107. const day = currentDate.getDate(); // 当前日期
  108. // 计算一个月前的日期
  109. const oneMonthAgoDate = new Date(year, month - 1, day);
  110. // 处理跨年问题(如果当前月份是 1 月,month - 1 会是 0,即上一年的 12 月)
  111. // 无需额外处理,Date 对象会自动处理
  112. // 格式化日期为 YYYY-MM-DD
  113. const formattedDate = `${oneMonthAgoDate.getFullYear()}-${String(oneMonthAgoDate.getMonth() + 1).padStart(2, '0')}-${String(oneMonthAgoDate.getDate()).padStart(2, '0')}`;
  114. return formattedDate;
  115. },
  116. /** 计算倒计时,倒计时结束后发起取消授权 */
  117. startCountdow() {
  118. this.timer = setInterval(() => {
  119. const countDownZeroOrders = []
  120. const orders = this.data.orders.map(order => {
  121. if (order.countdown && order.countdown != '0') {
  122. const currentCountDown = (Number(order.countdown) - 1);
  123. order.countdown = currentCountDown.toString();
  124. if (currentCountDown <= 0) countDownZeroOrders.push(order)
  125. }
  126. return order
  127. })
  128. this.setData({
  129. orders: orders
  130. })
  131. this.toUnAnthorization(countDownZeroOrders)
  132. }, 1000);
  133. },
  134. /** 结束倒计时 */
  135. endCountDown() {
  136. if (this.timer) {
  137. clearInterval(this.timer)
  138. this.timer = null
  139. }
  140. },
  141. /** 发送取消授权 */
  142. toUnAnthorization(orders) {
  143. console.log(orders)
  144. },
  145. /**
  146. * 生命周期函数--监听页面加载
  147. */
  148. onLoad(options) {
  149. const today = this.getNowDate();
  150. const lastMouthDay = this.getLastMouthDate();
  151. this.setData({
  152. // date: today,
  153. paymentMode: options.paymentMode,
  154. startDate: today,
  155. endDate: lastMouthDay
  156. });
  157. console.log("历史页当前data", this.data)
  158. // this.startCountdow();
  159. },
  160. /**
  161. * 生命周期函数--监听页面初次渲染完成
  162. */
  163. onReady() {
  164. this.getOrder();
  165. },
  166. /**
  167. * 生命周期函数--监听页面显示
  168. */
  169. onShow() {
  170. },
  171. /**
  172. * 生命周期函数--监听页面隐藏
  173. */
  174. onHide() {
  175. },
  176. /**
  177. * 生命周期函数--监听页面卸载
  178. */
  179. onUnload() {
  180. this.endCountDown()
  181. },
  182. /**
  183. * 页面相关事件处理函数--监听用户下拉动作
  184. */
  185. onPullDownRefresh() {
  186. this.refreshOrder()
  187. },
  188. /**
  189. * 页面上拉触底事件的处理函数
  190. */
  191. onReachBottom() {
  192. if (!this.data.isHaveOrder) return;
  193. var page = this.data.pageNum + 1;
  194. this.setData({
  195. pageNum: page
  196. })
  197. this.getOrder()
  198. },
  199. /**
  200. * 用户点击右上角分享
  201. */
  202. onShareAppMessage() {
  203. },
  204. /** 日期选择器选择时间 */
  205. bindDateChange(date) {
  206. this.setData({
  207. date: date.detail.value,
  208. orders: []
  209. });
  210. console.log(date)
  211. this.getOrder()
  212. },
  213. /** 组件按钮一点击事件 */
  214. onOrderButtonClick1(event) {
  215. console.log(event)
  216. const order = event.detail.order;
  217. const message = event.detail.event;
  218. if (message == "退款") {
  219. wx.showLoading({
  220. title: '正在退款',
  221. })
  222. api.request_RefundTrx({ "trxId": order.orderId }).then(res => {
  223. console.log("退款", res)
  224. wx.hideLoading()
  225. if (res.data.statusCode == 200) {
  226. return util.subAndsendMessage(order.orderId, "退款")
  227. } else {
  228. wx.showToast({
  229. title: res.data.message,
  230. })
  231. }
  232. }).then(res => {
  233. this.refreshOrder();
  234. })
  235. .catch(err => {
  236. console.log("退款失败", err)
  237. wx.hideLoading()
  238. })
  239. }
  240. },
  241. /** 组件按钮二点击事件 */
  242. onOrderButtonClick2(event) {
  243. console.log(event)
  244. if (event.detail.event == "重新授权") {
  245. this.toAuthorization(event.detail.order.orderId)
  246. }
  247. },
  248. /** 授权 */
  249. toAuthorization(orderId) {
  250. const that = this;
  251. wx.showLoading({
  252. title: '授权中',
  253. })
  254. api.request_NozzleAuthorization(orderId).then(res => {
  255. console.log("授权", res)
  256. wx.hideLoading()
  257. if (res.data.statusCode == 200) {
  258. that.refreshOrder()
  259. } else {
  260. wx.showToast({
  261. title: '授权失败',
  262. })
  263. }
  264. }).catch(err => {
  265. console.log("授权报错", err)
  266. wx.showToast({
  267. title: '授权失败',
  268. })
  269. })
  270. },
  271. /** 获取订单 */
  272. getOrder() {
  273. wx.showLoading({
  274. title: '获取订单中',
  275. })
  276. const that = this;
  277. api.request_WXFindOrders(this.data.date, this.data.pageNum, this.data.pageSize).then(res => {
  278. wx.hideLoading()
  279. // api.request_WXFindOrders(this.data.date, 3, 5).then(res => {
  280. console.log("获取历史订单", res)
  281. let datas = res.data.data;
  282. if (datas.length <= 0) {
  283. that.data.isHaveOrder = false;
  284. return
  285. }
  286. var getOrders = that.turnOnOrder(datas)
  287. var orderList = [...this.data.orders, ...getOrders]
  288. this.setData({
  289. orders: orderList
  290. })
  291. console.log("转换后的订单", this.data.orders)
  292. }).catch(err => {
  293. wx.hideLoading()
  294. console.log("获取历史订单失败", err)
  295. wx.showModal({
  296. title: '提示',
  297. content: '获取订单失败',
  298. })
  299. })
  300. },
  301. /** 打包订单信息 */
  302. turnOnOrder(orders) {
  303. const that = this;
  304. var getOrders = orders.map(order => {
  305. //获取订单时间
  306. var times = [order.authorizationTime, order.createTime, order.fuelItemTransactionEndTime, order.transactionTime];
  307. var timeFormate = times.find(t => t != undefined && t != null && t != '');
  308. var time = util.formatDateNotSecond(timeFormate);
  309. //获取订单升数
  310. var volume = (order.originalQty != null && order.originalQty != undefined) ? order.originalQty : order.qty
  311. //获取订单状态
  312. var status = undefined;
  313. //根据不同的模式获取订单状态值
  314. if (that.data.paymentMode == 0) {
  315. status = that.data.payStatusValue.find(state => state.orderStatus == order.orderStatus)
  316. } else {
  317. status = that.data.prepayStatuValue.find(state =>
  318. state.authorizationStatus == order.authorizationStatus &&
  319. state.orderStatus == order.orderStatus
  320. )
  321. }
  322. var stute = '';
  323. var stateColor = '';
  324. var bt1 = ''
  325. var bt2 = ''
  326. if (status != undefined) {
  327. stute = status.status
  328. stateColor = status.statusColor
  329. }
  330. if (that.data.paymentMode == 0) {
  331. if (stute == '未支付') bt2 = '重新支付';
  332. return {
  333. order: {
  334. status: stute,
  335. statusColor: stateColor,
  336. oilName: order.productName,
  337. nozzle: order.nozzleId,
  338. volume: volume,
  339. amount: order.originalAmount,
  340. payAmount: order.actualPaymentAmount,
  341. discount: order.originalAmount - order.actualPaymentAmount,
  342. orderId: order.id,
  343. time: time,
  344. price: order.price
  345. },
  346. bottonText2: bt2
  347. }
  348. } else {
  349. if (stute == "授权成功") bt2 = "请尽快提枪"
  350. if (stute == "支付失败") {
  351. bt1 = "取消"
  352. bt2 = "继续支付"
  353. }
  354. if (stute == "授权失败") {
  355. bt1 = "退款"
  356. bt2 = "重新授权"
  357. }
  358. return {
  359. order: {
  360. status: stute,
  361. statusColor: stateColor,
  362. oilName: order.productName,
  363. nozzle: order.nozzleId,
  364. volume: volume,
  365. amount: order.originalAmount,
  366. payAmount: order.actualPaymentAmount,
  367. discount: order.originalAmount - order.actualPaymentAmount,
  368. refund: order.refundAmount,
  369. orderId: order.id,
  370. time: time
  371. },
  372. bottonText1: bt1,
  373. bottonText2: bt2
  374. }
  375. }
  376. })
  377. return getOrders;
  378. },
  379. /** 刷新订单 */
  380. refreshOrder() {
  381. this.setData({
  382. date: '',
  383. orders: []
  384. });
  385. console.log(this.data.date)
  386. this.getOrder()
  387. }
  388. })