usageHistory.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="useRegistration">
  3. <van-nav-bar title="设备使用记录" left-text="返回" @click-left="back()" left-arrow style="z-index:1000" />
  4. <van-dropdown-menu>
  5. <van-dropdown-item :title="`开始日期:${starDate}`" ref="starCustom">
  6. <van-datetime-picker type="date" title="选择开始时间" v-model="starCurrentDate"
  7. @confirm="(date) => dataStartChange(date, 'star', 'starCustom')"
  8. @cancel="closeDataStartChange('starCustom')" />
  9. </van-dropdown-item>
  10. <van-dropdown-item :title="`结束日期:${endDate}`" ref="endCustom">
  11. <van-datetime-picker type="date" title="选择结束时间" v-model="endCurrentDate"
  12. @confirm="(date) => dataStartChange(date, 'end', 'endCustom')"
  13. @cancel="closeDataStartChange('endCustom')" />
  14. </van-dropdown-item>
  15. </van-dropdown-menu>
  16. <!-- 主要内容 -->
  17. <div class="primaryCoverage">
  18. <div class="primaryCoverageItem" v-for="(item, index) in historicalRecordsList" :key="index">
  19. <van-row>
  20. <van-col span="6" class="primaryLabel">设备编码</van-col>
  21. <van-col span="18">{{ item.deviceCode }}</van-col>
  22. </van-row>
  23. <van-row>
  24. <van-col span="6" class="primaryLabel">设备名称</van-col>
  25. <van-col span="18">{{ item.deviceName }}</van-col>
  26. </van-row>
  27. <van-row>
  28. <van-col span="6" class="primaryLabel">投入项目</van-col>
  29. <van-col span="18">{{ item.projectName }}</van-col>
  30. </van-row>
  31. <van-row>
  32. <van-col span="6" class="primaryLabel">使用时段</van-col>
  33. <van-col span="18" class="timeText
  34. ">{{ item.startTime }} - {{ item.usageStatus ? '至今' : item.endTime }} </van-col>
  35. </van-row>
  36. <van-row style="display: flex;align-items: center;">
  37. <van-col span="6" class="primaryLabel">使用时长</van-col>
  38. <van-col span="14">
  39. <span v-if="item.usageStatus" style="color: #FF5F4E;">使用中</span>
  40. <span v-else>{{ item.useTime }} 小时</span>
  41. </van-col>
  42. <van-col span="4">
  43. <template v-if="item.usageStatus">
  44. <van-button type="primary" size="mini" @click="endOfUse(item)">结束使用</van-button>
  45. </template>
  46. <template v-if="!item.usageStatus">
  47. <span style="color: #0283EF;">已完成</span>
  48. </template>
  49. </van-col>
  50. </van-row>
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. export default {
  57. data() {
  58. return {
  59. starDate: this.getFirstDayOfMonth(),
  60. endDate: this.getCurrentDate(),
  61. starCurrentDate: new Date(),
  62. endCurrentDate: new Date(),
  63. historicalRecordsList: []
  64. };
  65. },
  66. mounted() {
  67. this.retrieveUsageRecords()
  68. },
  69. methods: {
  70. endOfUse(row) {
  71. const { deviceId, projectId, startTime, deviceLogId, deviceName } = row
  72. const endTime = this.getToday()
  73. this.$dialog.confirm({
  74. message: `确定结束使用【${deviceName}】设备?`
  75. }).then(() => {
  76. this.postData(`/device-log/updateDeviceUsageStatus`, { id: deviceId, projectId, startTime, endTime, deviceLogId, startOrEnd: false }).then((res) => {
  77. this.$toast.success('操作成功')
  78. this.retrieveUsageRecords()
  79. })
  80. })
  81. },
  82. retrieveUsageRecords() {
  83. this.postData(`/device-log/getDeviceLogList`, {
  84. startTime: `${this.starDate} 00:00`,
  85. endTime: `${this.endDate} 23:59`
  86. }).then((res) => {
  87. const { data } = res
  88. this.historicalRecordsList = data.map((item) => {
  89. return {
  90. ...item,
  91. startTime: this.formatDateTime(item.startTime),
  92. endTime: item.endTime ? this.formatDateTime(item.endTime) : ''
  93. }
  94. })
  95. })
  96. },
  97. dataStartChange(date, type, refs) {
  98. const one = new Date(date)
  99. const two = new Date(this.starDate + ' 00:00:00')
  100. const three = new Date(this.endDate)
  101. if (type == 'star') {
  102. if(one > three) {
  103. this.$toast('选择开始时间不能大于结束时间')
  104. return
  105. }
  106. this.starDate = this.formatDate(date)
  107. }
  108. if (type == 'end') {
  109. if(one < two) {
  110. this.$toast('选择结束时间不能小于开始时间')
  111. return
  112. }
  113. this.endDate = this.formatDate(date)
  114. }
  115. this.closeDataStartChange(refs)
  116. this.retrieveUsageRecords()
  117. },
  118. closeDataStartChange(type) {
  119. if (type == 'starCustom') {
  120. this.$refs.starCustom.toggle(false);
  121. } else {
  122. this.$refs.endCustom.toggle(false);
  123. }
  124. },
  125. getToday() {
  126. return this.formatDateTime(new Date())
  127. },
  128. formatDateTime(date) {
  129. const dates = new Date(date);
  130. return dates.toLocaleString('zh-CN', {
  131. year: 'numeric',
  132. month: '2-digit',
  133. day: '2-digit',
  134. hour: '2-digit',
  135. minute: '2-digit',
  136. hour12: false
  137. }).replace(/\//g, '-');
  138. },
  139. // 格式化日期
  140. formatDate(date) {
  141. const year = date.getFullYear();
  142. const month = String(date.getMonth() + 1).padStart(2, '0');
  143. const day = String(date.getDate()).padStart(2, '0');
  144. return `${year}-${month}-${day}`;
  145. },
  146. // 获取当月第一天 YYYY-MM-DD
  147. getFirstDayOfMonth() {
  148. const now = new Date();
  149. const year = now.getFullYear();
  150. const month = String(now.getMonth() + 1).padStart(2, '0');
  151. const firstDay = '01';
  152. return `${year}-${month}-${firstDay}`;
  153. },
  154. // 获取当天的日期 YYYY-MM-DD
  155. getCurrentDate() {
  156. const now = new Date();
  157. const year = now.getFullYear();
  158. const month = String(now.getMonth() + 1).padStart(2, '0');
  159. const day = String(now.getDate()).padStart(2, '0');
  160. return `${year}-${month}-${day}`;
  161. },
  162. async postData(urls, param) {
  163. return new Promise((resolve, reject) => {
  164. this.$axios.post(urls, { ...param })
  165. .then(res => {
  166. if (res.code == "ok") {
  167. resolve(res)
  168. } else {
  169. this.$toast.fail('获取失败:' + res.msg);
  170. reject(res)
  171. }
  172. }).catch(err => { this.$toast.clear(); reject(err) });
  173. })
  174. },
  175. back() {
  176. history.back();
  177. },
  178. },
  179. };
  180. </script>
  181. <style lang="less" scoped>
  182. .useRegistration {
  183. width: 100%;
  184. height: 100vh;
  185. display: flex;
  186. flex-direction: column;
  187. .primaryCoverage {
  188. flex: 1;
  189. overflow-y: auto;
  190. box-sizing: border-box;
  191. padding: 0 10px;
  192. margin: 10px 0;
  193. }
  194. .primaryCoverageItem {
  195. background: #fff;
  196. width: 100%;
  197. margin-bottom: 10px;
  198. padding: 15px 20px;
  199. box-sizing: border-box;
  200. position: relative;
  201. border-radius: 4px;
  202. overflow: hidden;
  203. }
  204. .timeText {
  205. font-size: 14px;
  206. }
  207. .primaryLabel {
  208. color: #999 !important;
  209. }
  210. .van-row {
  211. margin-bottom: 10px;
  212. .van-col {
  213. color: #333;
  214. }
  215. &:last-child {
  216. margin-bottom: 0;
  217. }
  218. }
  219. }
  220. </style>