historyOrder.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. latitude: 0, //用户当前经度
  14. longitude: 0, //用户当前纬度
  15. stationName:'',//油站名
  16. stationLatitude: 0, //站点经度
  17. stationLongitude: 0, //站点纬度
  18. startDate: '',
  19. endDate: '',
  20. pageNum: 1,//查询页码
  21. pageSize: 5,//页数
  22. isHaveOrder: true,//是否还有订单
  23. paymentMode: -1,
  24. /** 后支付订单状态 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成)*/
  25. payStatusValue: [{
  26. orderStatus: 0,
  27. status: '未支付',
  28. statusColor: '#ff8d1a'
  29. },
  30. {
  31. orderStatus: 1,
  32. status: '已完成',
  33. statusColor: '#a6a6a6'
  34. },
  35. {
  36. orderStatus: 5,
  37. status: '已完成',
  38. statusColor: '#a6a6a6'
  39. }
  40. ],
  41. /** 预支付订单状态 授权状态(未授权:0;已授权:1)
  42. * + 订单状态(0:未支付; 1:已支付; 2:订单全额退款;3:订单部分退款;5:已完成) */
  43. prepayStatuValue: [
  44. {
  45. authorizationStatus: 0,
  46. orderStatus: 0,
  47. status: '支付失败',
  48. statusColor: '#f1912f'
  49. },
  50. {
  51. authorizationStatus: 0,
  52. orderStatus: 1,
  53. status: '授权失败',
  54. statusColor: '#f0d02e'
  55. },
  56. {
  57. authorizationStatus: 1,
  58. orderStatus: 1,
  59. status: '授权成功',
  60. statusColor: '#a2cd4a'
  61. },
  62. {
  63. authorizationStatus: 0,
  64. orderStatus: 2,
  65. status: '已完成',
  66. statusColor: '#a6a6a6'
  67. },
  68. {
  69. authorizationStatus: 0,
  70. orderStatus: 3,
  71. status: '已完成',
  72. statusColor: '#a6a6a6'
  73. },
  74. {
  75. authorizationStatus: 0,
  76. orderStatus: 5,
  77. status: '已完成',
  78. statusColor: '#a6a6a6'
  79. },
  80. ],
  81. },
  82. /** 获取当前日期 */
  83. getNowDate() {
  84. const date = new Date(); // 获取当前日期
  85. const year = date.getFullYear(); // 获取年份
  86. const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份,补零
  87. const day = String(date.getDate()).padStart(2, '0'); // 获取日期,补零
  88. return `${year}-${month}-${day}`; // 拼接成 yyyy-MM-dd 格式
  89. },
  90. /** 获取上个月日期 */
  91. getLastMouthDate() {
  92. const currentDate = new Date(); // 获取当前日期
  93. const year = currentDate.getFullYear(); // 当前年份
  94. const month = currentDate.getMonth(); // 当前月份(0-11)
  95. const day = currentDate.getDate(); // 当前日期
  96. // 计算一个月前的日期
  97. const oneMonthAgoDate = new Date(year, month - 1, day);
  98. // 处理跨年问题(如果当前月份是 1 月,month - 1 会是 0,即上一年的 12 月)
  99. // 无需额外处理,Date 对象会自动处理
  100. // 格式化日期为 YYYY-MM-DD
  101. const formattedDate = `${oneMonthAgoDate.getFullYear()}-${String(oneMonthAgoDate.getMonth() + 1).padStart(2, '0')}-${String(oneMonthAgoDate.getDate()).padStart(2, '0')}`;
  102. return formattedDate;
  103. },
  104. /** 计算倒计时,倒计时结束后发起取消授权 */
  105. startCountdow() {
  106. this.timer = setInterval(() => {
  107. const countDownZeroOrders = []
  108. const orders = this.data.orders.map(order => {
  109. if (order.countdown && order.countdown != '0') {
  110. const currentCountDown = (Number(order.countdown) - 1);
  111. order.countdown = currentCountDown.toString();
  112. if (currentCountDown <= 0) countDownZeroOrders.push(order)
  113. }
  114. return order
  115. })
  116. this.setData({
  117. orders: orders
  118. })
  119. this.toUnAnthorization(countDownZeroOrders)
  120. }, 1000);
  121. },
  122. /** 结束倒计时 */
  123. endCountDown() {
  124. if (this.timer) {
  125. clearInterval(this.timer)
  126. this.timer = null
  127. }
  128. },
  129. /** 发送取消授权 */
  130. toUnAnthorization(orders) {
  131. console.log(orders)
  132. },
  133. /**
  134. * 生命周期函数--监听页面加载
  135. */
  136. onLoad(options) {
  137. const today = this.getNowDate();
  138. const lastMouthDay = this.getLastMouthDate();
  139. this.setData({
  140. // date: today,
  141. paymentMode: options.paymentMode,
  142. startDate: today,
  143. endDate: lastMouthDay
  144. });
  145. console.log("历史页当前data", this.data)
  146. this.getStationInfo();
  147. // this.startCountdow();
  148. },
  149. /** 获取站点信息 */
  150. getStationInfo() {
  151. const that = this;
  152. api.request_GetSiteInfo().then(res => {
  153. if (res.data.statusCode == 203) {
  154. //若为203,证明还未登录,跳转到登录页,这里可能刚从主页跳转过来,频繁的跳转可能会跳转页面超时,故而加上延时
  155. setTimeout(() => {
  156. wx.navigateTo({
  157. url: '../login/login'
  158. })
  159. }, 500)
  160. return
  161. }
  162. console.log("站点信息", res)
  163. const stationLocation = res.data.data.site.gpsCoordinates.split(",");
  164. const name = res.data.data.site.name;
  165. that.setData({
  166. stationName:name,
  167. })
  168. if (stationLocation.length == 2) {
  169. that.setData({
  170. stationLatitude: stationLocation[0],
  171. stationLongitude: stationLocation[1]
  172. })
  173. }
  174. this.getLocation();
  175. })
  176. },
  177. /** 获取用户经纬度 */
  178. getLocation() {
  179. const that = this;
  180. wx.getLocation({
  181. type: 'wgs84',
  182. success(res) {
  183. console.log("获取经纬度结果", res)
  184. that.setData({
  185. latitude: res.latitude,
  186. longitude: res.longitude
  187. })
  188. }
  189. })
  190. },
  191. /**
  192. * 生命周期函数--监听页面初次渲染完成
  193. */
  194. onReady() {
  195. this.getOrder();
  196. },
  197. /**
  198. * 生命周期函数--监听页面显示
  199. */
  200. onShow() {
  201. },
  202. /**
  203. * 生命周期函数--监听页面隐藏
  204. */
  205. onHide() {
  206. },
  207. /**
  208. * 生命周期函数--监听页面卸载
  209. */
  210. onUnload() {
  211. this.endCountDown()
  212. },
  213. /**
  214. * 页面相关事件处理函数--监听用户下拉动作
  215. */
  216. onPullDownRefresh() {
  217. this.refreshOrder()
  218. },
  219. /**
  220. * 页面上拉触底事件的处理函数
  221. */
  222. onReachBottom() {
  223. if (!this.data.isHaveOrder) return;
  224. var page = this.data.pageNum + 1;
  225. this.setData({
  226. pageNum: page
  227. })
  228. this.getOrder()
  229. },
  230. /**
  231. * 用户点击右上角分享
  232. */
  233. onShareAppMessage() {
  234. },
  235. /** 日期选择器选择时间 */
  236. bindDateChange(date) {
  237. console.log("日期选择",date)
  238. this.setData({
  239. date: date.detail.value,
  240. pageNum:1,
  241. pageSize:5,
  242. orders: []
  243. });
  244. console.log(date)
  245. this.getOrder()
  246. },
  247. /** 组件按钮一点击事件 */
  248. onOrderButtonClick1(event) {
  249. console.log(event)
  250. const order = event.detail.order;
  251. const message = event.detail.event;
  252. if (message == "退款") {
  253. wx.showLoading({
  254. title: '正在退款',
  255. })
  256. api.request_RefundTrx({ "trxId": order.orderId }).then(res => {
  257. console.log("退款", res)
  258. wx.hideLoading()
  259. if (res.data.statusCode == 200) {
  260. return util.subAndsendMessage(order.orderId, "退款")
  261. } else {
  262. wx.showToast({
  263. title: res.data.message,
  264. })
  265. }
  266. }).then(res => {
  267. this.refreshOrder();
  268. })
  269. .catch(err => {
  270. console.log("退款失败", err)
  271. wx.hideLoading()
  272. })
  273. }
  274. },
  275. /** 组件按钮二点击事件 */
  276. onOrderButtonClick2(event) {
  277. console.log(event)
  278. if (event.detail.event == "重新授权") {
  279. this.toAuthorization(event.detail.order.orderId)
  280. }
  281. if(event.detail.event == "继续支付") {
  282. this.toPay(event.detail.order.orderId)
  283. }
  284. },
  285. /** 支付按钮点击事件 */
  286. toPay(orderId) {
  287. wx.showLoading({
  288. title: '支付中',
  289. })
  290. this.subMessage(orderId)
  291. },
  292. /** 订阅消息模板 */
  293. subMessage(orderId) {
  294. const that = this;
  295. wx.requestSubscribeMessage({
  296. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  297. success(res) {
  298. that.pay(orderId)
  299. // 'accept'表示用户同意订阅该条id对应的模板消息
  300. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  301. }
  302. },
  303. fail(err) {
  304. that.pay(orderId)
  305. }
  306. })
  307. },
  308. /** 实际支付 */
  309. pay(orderId) {
  310. const that = this;
  311. api.request_wechatPay(orderId)
  312. .then(res => {
  313. wx.requestPayment({
  314. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  315. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  316. package: res.data.data.unifiedOrderResult.package,
  317. signType: res.data.data.unifiedOrderResult.signType,
  318. paySign: res.data.data.unifiedOrderResult.paySign,
  319. success: res => {
  320. wx.hideLoading()
  321. that.sendMessage(orderId)
  322. that.refreshOrder()
  323. },
  324. fail: res => {
  325. wx.hideLoading()
  326. }
  327. })
  328. });
  329. },
  330. /** 发送消息模板 */
  331. sendMessage(orderId) {
  332. const message = {
  333. trxid: orderId,
  334. orderType: '支付'
  335. }
  336. api.request_sendMessage(message).then(res => {
  337. console.log("发送消息模板结果", res)
  338. }).catch(err => {
  339. console.log("发送消息模板失败", err)
  340. })
  341. },
  342. /** 授权 */
  343. toAuthorization(orderId) {
  344. const that = this;
  345. wx.showLoading({
  346. title: '授权中',
  347. })
  348. const distance = util.haversine(this.data.stationLatitude,this.data.stationLongitude,this.data.latitude,this.data.longitude);
  349. if(distance > 500) {
  350. wx.showToast({
  351. title: '您不在油站范围内,请到油站时进行下单',
  352. })
  353. return
  354. }
  355. api.request_NozzleAuthorization(orderId).then(res => {
  356. console.log("授权", res)
  357. wx.hideLoading()
  358. if (res.data.statusCode == 200) {
  359. that.refreshOrder()
  360. } else {
  361. wx.showToast({
  362. title: '授权失败',
  363. })
  364. }
  365. }).catch(err => {
  366. console.log("授权报错", err)
  367. wx.showToast({
  368. title: '授权失败',
  369. })
  370. })
  371. },
  372. /** 获取订单 */
  373. getOrder() {
  374. wx.showLoading({
  375. title: '获取订单中',
  376. })
  377. const that = this;
  378. const findTime = this.data.date != '' ? this.data.date + "T23:59:59" : this.data.date;
  379. api.request_WXFindOrders(findTime, this.data.pageNum, this.data.pageSize).then(res => {
  380. wx.hideLoading()
  381. wx.stopPullDownRefresh()
  382. // api.request_WXFindOrders(this.data.date, 3, 5).then(res => {
  383. console.log("获取历史订单", res)
  384. let datas = res.data.data;
  385. if (datas.length <= 0) {
  386. that.data.isHaveOrder = false;
  387. return
  388. }
  389. var getOrders = that.turnOnOrder(datas)
  390. var orderList = [...this.data.orders, ...getOrders]
  391. this.setData({
  392. orders: orderList
  393. })
  394. console.log("转换后的订单", this.data.orders)
  395. }).catch(err => {
  396. wx.hideLoading()
  397. console.log("获取历史订单失败", err)
  398. wx.showModal({
  399. title: '提示',
  400. content: '获取订单失败',
  401. })
  402. })
  403. },
  404. /** 打包订单信息 */
  405. turnOnOrder(orders) {
  406. const that = this;
  407. var getOrders = orders.map(order => {
  408. //获取订单时间
  409. var times = [order.authorizationTime, order.createTime, order.fuelItemTransactionEndTime, order.transactionTime];
  410. var timeFormate = times.find(t => t != undefined && t != null && t != '');
  411. var time = util.formatDateNotSecond(timeFormate);
  412. //获取订单升数
  413. var volume = (order.originalQty != null && order.originalQty != undefined) ? order.originalQty : order.qty
  414. //获取订单状态
  415. var status = undefined;
  416. //根据不同的模式获取订单状态值
  417. if (order.orderType == 0) {
  418. status = that.data.payStatusValue.find(state => state.orderStatus == order.orderStatus)
  419. } else {
  420. status = that.data.prepayStatuValue.find(state =>
  421. state.authorizationStatus == order.authorizationStatus &&
  422. state.orderStatus == order.orderStatus
  423. )
  424. }
  425. var stute = '';
  426. var stateColor = '';
  427. var bt1 = ''
  428. var bt2 = ''
  429. if (status != undefined) {
  430. stute = status.status
  431. stateColor = status.statusColor
  432. }
  433. if (order.orderType == 0) {
  434. if (stute == '未支付') bt2 = '重新支付';
  435. return {
  436. order: {
  437. status: stute,
  438. statusColor: stateColor,
  439. oilName: order.productName,
  440. nozzle: order.nozzleId,
  441. volume: volume,
  442. amount: order.originalAmount,
  443. payAmount: order.actualPaymentAmount,
  444. discount: order.originalAmount - order.actualPaymentAmount,
  445. orderId: order.id,
  446. time: time,
  447. price: order.price,
  448. station:that.data.stationName
  449. },
  450. bottonText2: bt2,
  451. orderType:order.orderType
  452. }
  453. } else {
  454. if (stute == "授权成功") bt2 = "请尽快提枪"
  455. if (stute == "支付失败") {
  456. bt1 = "取消"
  457. bt2 = "继续支付"
  458. }
  459. if (stute == "授权失败") {
  460. bt1 = "退款"
  461. bt2 = "重新授权"
  462. }
  463. return {
  464. order: {
  465. status: stute,
  466. statusColor: stateColor,
  467. oilName: order.productName,
  468. nozzle: order.nozzleId,
  469. volume: volume,
  470. amount: order.originalAmount,
  471. payAmount: order.actualPaymentAmount,
  472. discount: order.originalAmount - order.actualPaymentAmount,
  473. refund: order.refundAmount,
  474. orderId: order.id,
  475. time: time,
  476. station:that.data.stationName
  477. },
  478. bottonText1: bt1,
  479. bottonText2: bt2,
  480. orderType:order.orderType
  481. }
  482. }
  483. })
  484. return getOrders;
  485. },
  486. /** 刷新订单 */
  487. refreshOrder() {
  488. this.setData({
  489. date: '',
  490. pageNum:1,
  491. pageSize:5,
  492. orders: []
  493. });
  494. console.log(this.data.date)
  495. this.getOrder()
  496. }
  497. })