charging.vue 11 KB

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