historyOrder.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. date: '',
  12. startDate: '',
  13. endDate: '',
  14. pageNum:1,//查询页码
  15. pageSize:5,//页数
  16. isHaveOrder:true,//是否还有订单
  17. paymentMode: -1,
  18. /** 后支付订单状态 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成)*/
  19. payStatusValue: [{
  20. orderStatus: 0,
  21. status: '未支付',
  22. statusColor: '#ff8d1a'
  23. },
  24. {
  25. orderStatus: 1,
  26. status: '已完成',
  27. statusColor: '#a6a6a6'
  28. },
  29. {
  30. orderStatus: 5,
  31. status: '已完成',
  32. statusColor: '#a6a6a6'
  33. }
  34. ],
  35. /** 预支付订单状态 授权状态(未授权:0;已授权:1)
  36. * + 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成) */
  37. prepayStatuValue:[
  38. {
  39. authorizationStatus: 0,
  40. orderStatus: 0,
  41. status: '支付失败',
  42. statusColor: '#f1912f'
  43. },
  44. {
  45. authorizationStatus: 0,
  46. orderStatus: 1,
  47. status: '授权失败',
  48. statusColor: '#f0d02e'
  49. },
  50. {
  51. authorizationStatus: 1,
  52. orderStatus: 1,
  53. status: '授权成功',
  54. statusColor: '#a2cd4a'
  55. },
  56. {
  57. authorizationStatus: 0,
  58. orderStatus: 2,
  59. status: '已完成',
  60. statusColor: '#a6a6a6'
  61. },
  62. {
  63. authorizationStatus: 0,
  64. orderStatus: 3,
  65. status: '已完成',
  66. statusColor: '#a6a6a6'
  67. },
  68. {
  69. authorizationStatus: 0,
  70. orderStatus: 5,
  71. status: '已完成',
  72. statusColor: '#a6a6a6'
  73. },
  74. ],
  75. orders: []
  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.getOrder();
  142. // this.startCountdow();
  143. },
  144. /**
  145. * 生命周期函数--监听页面初次渲染完成
  146. */
  147. onReady() {
  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. const that = this;
  257. api.request_WXFindOrders(this.data.date, this.data.pageNum, this.data.pageSize).then(res => {
  258. // api.request_WXFindOrders(this.data.date, 3, 5).then(res => {
  259. console.log("获取历史订单", res)
  260. let datas = res.data.data;
  261. if(datas.length <= 0) {
  262. that.data.isHaveOrder = false;
  263. return
  264. }
  265. var getOrders = that.turnOnOrder(datas)
  266. var orderList = [...this.data.orders,...getOrders]
  267. this.setData({
  268. orders: orderList
  269. })
  270. console.log("转换后的订单", this.data.orders)
  271. }).catch(err => {
  272. console.log("获取历史订单失败", err)
  273. })
  274. },
  275. /** 打包订单信息 */
  276. turnOnOrder(orders){
  277. const that = this;
  278. var getOrders = orders.map(order => {
  279. //获取订单时间
  280. var times = [order.authorizationTime, order.createTime, order.fuelItemTransactionEndTime, order.transactionTime];
  281. var timeFormate = times.find(t => t != undefined && t != null && t != '');
  282. var time = util.formatDateNotSecond(timeFormate);
  283. //获取订单升数
  284. var volume = (order.originalQty != null && order.originalQty != undefined) ? order.originalQty : order.qty
  285. //获取订单状态
  286. var status = undefined;
  287. //根据不同的模式获取订单状态值
  288. if(that.data.paymentMode == 0) {
  289. status = that.data.payStatusValue.find(state => state.orderStatus == order.orderStatus)
  290. } else {
  291. status = that.data.prepayStatuValue.find(state =>
  292. state.authorizationStatus == order.authorizationStatus &&
  293. state.orderStatus == order.orderStatus
  294. )
  295. }
  296. var stute = '';
  297. var stateColor = '';
  298. var bt1 = ''
  299. var bt2 = ''
  300. if (status != undefined) {
  301. stute = status.status
  302. stateColor = status.statusColor
  303. }
  304. if(that.data.paymentMode == 0) {
  305. if(stute == '未支付') bt2 = '重新支付';
  306. return {
  307. order: {
  308. status: stute,
  309. statusColor: stateColor,
  310. oilName: order.productName,
  311. nozzle: order.nozzleId,
  312. volume: volume,
  313. amount: order.originalAmount,
  314. payAmount: order.actualPaymentAmount,
  315. discount: order.originalAmount - order.actualPaymentAmount,
  316. orderId: order.id,
  317. time: time
  318. },
  319. bottonText2: bt2
  320. }
  321. } else {
  322. if(stute == "授权成功") bt2 = "请尽快提枪"
  323. if(stute == "支付失败") {
  324. bt1 = "取消"
  325. bt2 = "继续支付"
  326. }
  327. if(stute == "授权失败") {
  328. bt1 = "退款"
  329. bt2 = "重新授权"
  330. }
  331. return {
  332. order: {
  333. status: stute,
  334. statusColor: stateColor,
  335. oilName: order.productName,
  336. nozzle: order.nozzleId,
  337. volume: volume,
  338. amount: order.originalAmount,
  339. payAmount: order.actualPaymentAmount,
  340. discount: order.originalAmount - order.actualPaymentAmount,
  341. refund:order.refundAmount,
  342. orderId: order.id,
  343. time: time
  344. },
  345. bottonText1:bt1,
  346. bottonText2: bt2
  347. }
  348. }
  349. })
  350. return getOrders;
  351. },
  352. /** 刷新订单 */
  353. refreshOrder(){
  354. this.setData({
  355. date: '',
  356. orders:[]
  357. });
  358. console.log(this.data.date)
  359. this.getOrder()
  360. }
  361. })