historyOrder.js 11 KB

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