charging.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <ax-body blank="0" hideIndicatorArea>
  3. <image src="@/static/img/page-bg01.png" class="page-background"></image>
  4. <view class="body">
  5. <view v-if="exiting.visible" class="exiting">
  6. <view class="app-flex c-center column contet">
  7. <view class="exiting-title">长按停止按钮结束充电</view>
  8. <view class="countdown-ring" :class="{ active: exiting.lock }" @touchstart="exiting.lock = true"
  9. @touchend="exiting.lock = false">
  10. <view class="_half left" @animationend="submitExit()"></view>
  11. <view class="_half right"></view>
  12. <view class="_button">
  13. <view>停止</view>
  14. <view>充电</view>
  15. </view>
  16. </view>
  17. <view class="close" @click="exiting.visible = false">取消</view>
  18. </view>
  19. <view @click="exiting.visible = false" class="mask"></view>
  20. </view>
  21. <view v-if="deviceInfo.status == '0'" class="timer">
  22. <view class="value" style="font-size: 22px;">充电正在启动中......</view>
  23. <view v-if="isShowBtn" class="name" style="color: red;font-size: 14px;">长时间无法启动可点击“长按结束充电”按钮结束订单。</view>
  24. </view>
  25. <!-- 主图 -->
  26. <view class="app-flex c-center host-graph">
  27. <image
  28. src="https://national-motion.oss-cn-beijing.aliyuncs.com/20251230/9ca38a2aa5784e06b9cac31587b188b3.png"
  29. mode="widthFix" class="image"></image>
  30. </view>
  31. <!-- 主参数 -->
  32. <view class="parameter-info-box">
  33. <!-- 充电动画 -->
  34. <view class="charge-loading-box" v-if="deviceInfo.status == '1'">
  35. <view class="charge-loading">
  36. <image class="charge-icon" src="../../static/img/charge_loading.svg" mode=""></image>
  37. <view class="c-loading"></view>
  38. </view>
  39. </view>
  40. <!-- 计时器 -->
  41. <view class="timer" v-if="deviceInfo.status == '1'">
  42. <view class="value">{{ timer.text }}</view>
  43. <view class="name">充电时间</view>
  44. </view>
  45. <view class="parameter">
  46. <view class="param">
  47. <view class="name">电流A</view>
  48. <view class="value">{{ deviceInfo.current || '0' }}</view>
  49. </view>
  50. <view class="split"></view>
  51. <view class="param">
  52. <view class="name">电压V</view>
  53. <view class="value">{{ deviceInfo.voltage || '0' }}</view>
  54. </view>
  55. <view class="split"></view>
  56. <view class="param">
  57. <view class="name">功率KW</view>
  58. <view class="value">{{ deviceInfo.power || '0' }}</view>
  59. </view>
  60. <view class="split"></view>
  61. <view class="param">
  62. <view class="name">电量/度</view>
  63. <view class="value">{{ deviceInfo.totalPower || '0' }}</view>
  64. </view>
  65. <view class="split"></view>
  66. <view class="param">
  67. <view class="name">费用/元</view>
  68. <view class="value">{{ deviceInfo.totalMoney || '0' }}</view>
  69. </view>
  70. </view>
  71. <view class="end-charge-box">
  72. <button @click="exiting.visible = true" :disabled="!isShowBtn" class="end-charge">结束充电</button>
  73. </view>
  74. </view>
  75. <!-- 信息 -->
  76. <view id="info" class="info">
  77. <view>
  78. <view class="cell">
  79. <view class="lable">订单编号</view>
  80. <view class="contet">{{ deviceInfo.chargeOrderNo }}</view>
  81. </view>
  82. <view class="cell">
  83. <view class="lable">终端编号</view>
  84. <view class="contet">{{ deviceInfo.connectorCode }}</view>
  85. </view>
  86. <view class="cell">
  87. <view class="lable">充电电站</view>
  88. <view class="contet">{{ deviceInfo.stationName }}</view>
  89. </view>
  90. <view class="cell">
  91. <view class="lable">充电终端</view>
  92. <view class="contet">{{ deviceInfo.connectorName }}</view>
  93. </view>
  94. </view>
  95. <view class="tips">账单信息可能会有所延迟,具体以实际结算为准</view>
  96. <!-- <view>
  97. <button @click="exiting.visible=true" :disabled="!isShowBtn" class="end">结束充电</button>
  98. <ax-ios-indicator min="10"></ax-ios-indicator>
  99. </view> -->
  100. </view>
  101. </view>
  102. </ax-body>
  103. </template>
  104. <script>
  105. // 终态状态码:2-结算中, 3-已完成, 5-未成功充电
  106. const TERMINAL_STATUS = ['2', '3', '5'];
  107. export default {
  108. onLoad(opts) {
  109. console.log("启动充值页面的参数:", opts)
  110. this.orderInfo.id = opts.orderId;
  111. this.deviceInfo.id = opts.deviceId;
  112. this.chargeOrderNo = opts.chargeOrderNo || '';
  113. this.getDeviceInfo(true, opts.chargeOrderNo);
  114. },
  115. onShow() {
  116. // this.getOrderInfo();
  117. },
  118. data() {
  119. return {
  120. exiting: {
  121. visible: false,
  122. lock: false
  123. },
  124. timer: {
  125. id: 0,
  126. start: '2024/08/07 09:00:00',
  127. text: '00:00:00',
  128. isSatrt: false,
  129. },
  130. pollingTimer: null, //轮询定时器ID
  131. deviceInfo: {}, //充电桩的信息
  132. orderInfo: {
  133. id: 1
  134. }, //订单信息
  135. chargeOrderNo: '',
  136. isShowBtn: true, //是否显示长按结束按钮
  137. isStoppingCharge: false, //是否正在停止充电(用户主动停止)
  138. }
  139. },
  140. destroyed() {
  141. console.log("关闭页面了......")
  142. // 页面销毁时清除所有定时器
  143. this.clearAllTimers();
  144. },
  145. methods: {
  146. /**
  147. * 清除所有定时器(轮询+计时器)
  148. */
  149. clearAllTimers() {
  150. this.stopPolling();
  151. if (this.timer.id) {
  152. clearInterval(this.timer.id);
  153. this.timer.id = 0;
  154. }
  155. },
  156. /**
  157. * 检查是否为终态状态(需要跳转的状态)
  158. * @param {string} status 状态码
  159. * @returns {boolean}
  160. */
  161. isTerminalStatus(status) {
  162. return TERMINAL_STATUS.includes(String(status));
  163. },
  164. /**
  165. * 处理终态跳转(统一跳转和清理逻辑)
  166. */
  167. handleTerminalRedirect() {
  168. uni.hideLoading();
  169. this.clearAllTimers();
  170. this.isStoppingCharge = false;
  171. this.$app.url.goto('/subPackages/order/order/order');
  172. },
  173. /**
  174. * 获取设备的详情信息(含轮询)
  175. * @param {boolean} showLoading 是否显示loading
  176. * @param {string} orderNo 订单号
  177. */
  178. getDeviceInfo(showLoading = true, orderNo) {
  179. this.$api.base("get", "/applet/v1/station/charging-cost", {
  180. orderNo: orderNo || this.chargeOrderNo
  181. }, {
  182. loading: showLoading
  183. }).then(res => {
  184. this.deviceInfo = res.data;
  185. // 检查订单状态,终态则跳转订单列表
  186. // 状态:0-待启动, 1-充电中, 2-结算中, 3-已完成, 5-未成功充电
  187. if (this.isTerminalStatus(res.data.status)) {
  188. this.handleTerminalRedirect();
  189. return;
  190. }
  191. // 充电中状态,启动计时器
  192. // 订单状态:1-启动中, 2-充电中, 3-停止中, 4-已结束, 5-未知
  193. if (res.data.orderStatus == 2 && !this.timer.isSatrt) {
  194. this.startup();
  195. }
  196. // 检查是否显示结束按钮
  197. this.checkShowEndBtn(res.data.startTime);
  198. // 启动轮询(无论是否正在停止充电,都继续轮询等待状态变化)
  199. this.startPolling();
  200. }).catch(err => {
  201. console.error('获取充电信息失败:', err);
  202. // 请求失败时也要确保轮询继续
  203. this.startPolling();
  204. });
  205. },
  206. /**
  207. * 启动轮询定时器,每5秒查询一次
  208. */
  209. startPolling() {
  210. if (this.pollingTimer) return; // 已启动则不重复
  211. this.pollingTimer = setInterval(() => {
  212. this.getDeviceInfo(false);
  213. }, 5000);
  214. },
  215. /**
  216. * 停止轮询
  217. */
  218. stopPolling() {
  219. if (this.pollingTimer) {
  220. clearInterval(this.pollingTimer);
  221. this.pollingTimer = null;
  222. }
  223. },
  224. /**
  225. * 检查是否显示长按结束按钮
  226. * @param {string} orderTime 订单开始时间
  227. */
  228. checkShowEndBtn(orderTime) {
  229. if (!orderTime) return;
  230. const t1 = new Date(orderTime.replace(/-/g, '/')).getTime();
  231. const t2 = Date.now();
  232. const diffSeconds = (t2 - t1) / 1000;
  233. // 启动时间大于60秒或状态为充电中,显示结束按钮
  234. if (diffSeconds >= 60 || this.deviceInfo.orderStatus == 2) {
  235. this.isShowBtn = true;
  236. }
  237. },
  238. /**
  239. * 启动充电计时器
  240. */
  241. startup() {
  242. const start = new Date(this.deviceInfo.startTime);
  243. const obj = {
  244. hour: 0,
  245. minute: 0,
  246. second: 0
  247. };
  248. const handle = () => {
  249. this.timer.isSatrt = true;
  250. const diff = Date.now() - start.getTime();
  251. obj.hour = Math.floor(diff / 1000 / 60 / 60);
  252. obj.minute = Math.floor(diff / 1000 / 60 % 60);
  253. obj.second = Math.floor(diff / 1000 % 60);
  254. this.timer.text =
  255. `${String(obj.hour).padStart(2, '0')}:${String(obj.minute).padStart(2, '0')}:${String(obj.second).padStart(2, '0')}`;
  256. };
  257. clearInterval(this.timer.id);
  258. this.timer.id = setInterval(handle, 1000);
  259. handle();
  260. },
  261. /**
  262. * 退出充电(调用停止充电接口)
  263. */
  264. exit() {
  265. this.isStoppingCharge = true;
  266. this.$api.base("post", "/applet/v1/charge/stopCharge", {
  267. "chargeOrderNo": this.deviceInfo?.chargeOrderNo || this.chargeOrderNo
  268. }, {}).then(res => {
  269. if (res.code == '00000') {
  270. // 显示结算中loading
  271. this.$app.popup.loading(true, {
  272. title: "结算中,请稍候",
  273. timeout: 120 * 1000
  274. });
  275. this.exiting.lock = false;
  276. // 不停止轮询,继续轮询等待状态变为终态(2、3、5)再跳转
  277. // 轮询会在 getDeviceInfo 中检测到终态后自动跳转并清理
  278. } else {
  279. // 停止充电失败,重置状态
  280. this.isStoppingCharge = false;
  281. this.exiting.lock = false;
  282. }
  283. }).catch(() => {
  284. this.isStoppingCharge = false;
  285. this.exiting.lock = false;
  286. });
  287. },
  288. /**
  289. * 长按动画结束,提交停止充电
  290. */
  291. submitExit() {
  292. this.exiting.lock = false;
  293. this.exiting.visible = false;
  294. this.exit();
  295. }
  296. }
  297. }
  298. </script>
  299. <style lang="less" scoped>
  300. @import url("charging.css");
  301. /* 充电loading动画 */
  302. .charge-loading-box {
  303. text-align: center;
  304. margin-top: 20rpx;
  305. }
  306. .charge-loading {
  307. width: 364rpx;
  308. height: 36rpx;
  309. background: #FFFFFF;
  310. border-radius: 18rpx;
  311. overflow: hidden;
  312. position: relative;
  313. margin: auto;
  314. }
  315. .charge-icon {
  316. width: 28rpx;
  317. height: 28rpx;
  318. z-index: 1000;
  319. position: absolute;
  320. left: 20rpx;
  321. top: 5rpx;
  322. }
  323. .c-loading {
  324. width: 100%;
  325. height: 100%;
  326. background: linear-gradient(66deg,
  327. rgba(139, 243, 251, 0),
  328. #60C8FE,
  329. #53D4FF,
  330. rgba(139, 243, 251, 0.7), );
  331. border-radius: 18rpx;
  332. animation: loading 3s linear infinite;
  333. background-size: 200% 100%;
  334. }
  335. @keyframes loading {
  336. 0% {
  337. background-position: 100% 0;
  338. }
  339. 100% {
  340. background-position: -100% 0;
  341. }
  342. }
  343. </style>