useRegistration.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <div class="useRegistration">
  3. <van-nav-bar title="设备使用登记" left-text="返回" @click-left="back()" left-arrow style="z-index:1000">
  4. <van-icon name="scan" slot="right" class="tabIcon" @click="scanClick()" />
  5. </van-nav-bar>
  6. <div class="useRegistration-content">
  7. <van-cell-group>
  8. <van-field v-model="equipmentForm.code" label="设备编号" placeholder="请扫码" readonly clearable :label-width="100" />
  9. <van-field v-model="equipmentForm.name" label="设备名称" placeholder="请扫码" readonly clearable :label-width="100" />
  10. <van-field readonly v-model="equipmentForm.projectName" clickable type="textarea" autosize :label="'选择项目'"
  11. :placeholder="'请选择项目'" @click="showPopup('showPickerUserddp')" v-if="isItMe" />
  12. <van-field v-model="equipmentForm.startTime" label="开始使用时间" placeholder="请选择使用时间"
  13. @click="showPopup('endDateShow')" v-if="isItMe" readonly clickable></van-field>
  14. <van-field v-model="equipmentForm.endTime" label="结束使用时间" placeholder="请选择结束时间"
  15. @click="showPopup('endDateShowTwo', false)" v-if="status == 1 && isItMe" readonly clickable></van-field>
  16. <van-field v-model="durationOfUse" label="已使用时长(小时)" placeholder="请扫码" readonly clearable :label-width="120"
  17. v-if="status == 1 && isItMe" />
  18. </van-cell-group>
  19. <!-- 选择时间 -->
  20. <van-popup v-model="endDateShow" position="bottom">
  21. <van-datetime-picker type="datetime" title="选择开始时间" v-model="currentDate2" @confirm="endDateChange"
  22. @cancel="endDateShow = false; $forceUpdate();" />
  23. </van-popup>
  24. <van-popup v-model="endDateShowTwo" position="bottom">
  25. <van-datetime-picker type="datetime" title="选择结束时间" v-model="currentDatetwo2" @confirm="endDateChangeTwo"
  26. @cancel="endDateShowTwo = false; $forceUpdate();" />
  27. </van-popup>
  28. <!-- 不是本人 -->
  29. <template v-if="!isItMe">
  30. <van-field v-model="equipmentForm.usageUserName" label="使用人" readonly error clearable :label-width="100" />
  31. <div class="equipmentUsage">该设备当前已被占用,请换其他设备扫码</div>
  32. </template>
  33. <!-- 选择项目 -->
  34. <van-popup v-model="showPickerUserddp" position="bottom" style="height: 84%">
  35. <div class="popupDiv">
  36. <div class="popupSearch">
  37. <van-search v-model="userName" shape="round" background="#F4F4F4" placeholder="请输入项目名称/编号" @clear="sea()"
  38. @blur="sea()" @search="sea()" @input="sea()" />
  39. </div>
  40. <div class="popupCon">
  41. <div class="popupTitle">近期选择项目</div>
  42. <div v-for="(item, index) in integrationProjectList" :key="index" class="popupItem paddingDiv"
  43. @click="fZr(item, index)">
  44. <div class="popupItemOne" v-if="item.projectName">{{ item.projectName }}</div>
  45. <p class="popupItemTwo" v-if="item.projectCode">{{ item.projectCode }}</p>
  46. </div>
  47. <div class="popupTitle marginTop">全部项目</div>
  48. <div v-for="(item, index) in projectss" :key="item.id" class="popupItem paddingDiv"
  49. @click="fZr(item, index)">
  50. <div class="popupItemOne" v-if="item.projectName">{{ item.projectName }}</div>
  51. <p class="popupItemTwo" v-if="item.projectCode">{{ item.projectCode }}</p>
  52. </div>
  53. <!-- <div class="popupTitle paddingDiv">全部项目</div> -->
  54. </div>
  55. </div>
  56. </van-popup>
  57. </div>
  58. <div class="useRegistration-footer" v-if="isItMe">
  59. <van-button type="primary" size="info" round block @click="operation" :disabled="!this.equipmentForm.code">{{
  60. status == 0 ? '确定开始使用' : '确定结束使用' }}</van-button>
  61. </div>
  62. <!-- 扫码 -->
  63. <div class="qrcodeClass" v-if="showQrcode">
  64. <Qrcode @closeQrcode="closeQrcode" @determineQrcode="determineQrcode" />
  65. </div>
  66. </div>
  67. </template>
  68. <script>
  69. import Qrcode from '../../components/qrcode.vue'
  70. export default {
  71. components: { Qrcode },
  72. data() {
  73. return {
  74. equipmentForm: {
  75. code: '',
  76. name: '',
  77. projectName: '',
  78. startTime: this.getToday()
  79. },
  80. endDateShow: false,
  81. currentDate2: this.getToday(),
  82. showPickerUserddp: false,
  83. userName: '',
  84. integrationProjectList: [],
  85. projectss: [],
  86. project: [],
  87. proads: [],
  88. showQrcode: false,
  89. status: 0, // 0: 新增,1:编辑
  90. durationOfUse: 0,
  91. endDateShowTwo: false,
  92. currentDatetwo2: this.getToday(),
  93. userInfo: JSON.parse(localStorage.userInfo),
  94. isItMe: true // 是否为本人
  95. };
  96. },
  97. mounted() {
  98. this.reset()
  99. this.getRecentlyProject()
  100. this.getPeoject()
  101. },
  102. methods: {
  103. operation() {
  104. const { id, projectId, startTime, endTime, deviceLogId } = this.equipmentForm
  105. console.log(this.equipmentForm, '<===== this.equipmentForm')
  106. if (endTime) {
  107. const one = new Date(startTime)
  108. const two = new Date(endTime)
  109. if (one > two) {
  110. this.$toast.fail('结束时间不能早于开始时间')
  111. return
  112. }
  113. }
  114. this.$axios.post("/device-log/updateDeviceUsageStatus", {
  115. id,
  116. projectId,
  117. startTime,
  118. endTime,
  119. deviceLogId,
  120. startOrEnd: !this.status ? true : false,
  121. })
  122. .then(res => {
  123. if (res.code == "ok") {
  124. this.$toast.success('操作成功')
  125. this.resetData()
  126. } else {
  127. this.$toast.fail('获取失败:' + res.msg);
  128. }
  129. }).catch(err => { this.$toast.clear(); });
  130. },
  131. resetData() {
  132. this.equipmentForm = {
  133. code: '',
  134. name: '',
  135. projectName: '',
  136. startTime: this.getToday()
  137. }
  138. this.status = 0
  139. },
  140. closeQrcode() {
  141. this.showQrcode = false
  142. },
  143. scanClick() {
  144. this.showQrcode = true
  145. // setTimeout(() => {
  146. // // const val = '{"id":3,"deviceCode":"设备编号1","deviceName":"设备名称1","deviceModel":"设备型号1","manufacturer":"制造商1","purchaseDate":"2024-10-09","maintenanceDate":"2024-10-10","companyId":10,"usageStatus":false,"usageUserId":null,"usageUserName":null,"projectId":null,"projectName":null,"startTime":null,"endTime":null,"startOrEnd":null,"deviceLogId":null,"useTime":null}'
  147. // const val = localStorage.getItem('scanCodeData')
  148. // this.determineQrcode(val)
  149. // }, 3000)
  150. },
  151. determineQrcode(val) {
  152. console.log(val, '<=========== val')
  153. if (val != '') {
  154. const newVal = JSON.parse(val)
  155. this.$axios.post("/device/getDeviceDetail", { id: newVal.id })
  156. .then(res => {
  157. if (res.code == "ok") {
  158. console.log(res, '<===== 返回的数据')
  159. const values = res.data
  160. const endtimes = this.getToday()
  161. this.equipmentForm = {
  162. ...values,
  163. code: values.deviceCode,
  164. name: values.deviceName,
  165. projectName: values.projectName,
  166. startTime: values.startTime || this.getToday(),
  167. id: values.id,
  168. endTime: endtimes
  169. }
  170. this.durationOfUse = this.calculateUsageTime(res.data)
  171. this.currentDatetwo2 = endtimes
  172. this.status = values.usageStatus ? 1 : 0
  173. if (values.usageUserId) {
  174. this.isItMe = values.usageUserId == this.userInfo.id
  175. } else {
  176. this.isItMe = true
  177. }
  178. } else {
  179. this.$toast.fail('获取失败:' + res.msg);
  180. }
  181. }).catch(err => { this.$toast.clear(); });
  182. } else {
  183. this.$toast.fail('无效的二维码')
  184. }
  185. this.closeQrcode()
  186. },
  187. showPopup(type, flag = true) {
  188. if (this.status == 1 && flag) {
  189. return
  190. }
  191. this[type] = true
  192. },
  193. calculateUsageTime(row) {
  194. const { startTime } = row
  195. if (!startTime) {
  196. return 0
  197. }
  198. const endTime = this.getToday()
  199. const startDate = new Date(startTime);
  200. const endDate = new Date(endTime);
  201. const diffInMs = endDate - startDate;
  202. const diffInHours = diffInMs / (1000 * 60 * 60);
  203. return diffInHours.toFixed(2);
  204. },
  205. reset() {
  206. this.equipmentForm = {
  207. code: '',
  208. name: '',
  209. projectName: '',
  210. startTime: this.getToday()
  211. }
  212. },
  213. fZr(row, index) {
  214. this.equipmentForm.projectName = row.projectName
  215. this.equipmentForm.projectId = row.id
  216. this.showPickerUserddp = false
  217. },
  218. sea() {
  219. if (this.userName.length > 0) {
  220. let data = this.proads.filter(item => { return item.projectName.indexOf(this.userName.toUpperCase()) != '-1' });
  221. let dataList = this.proads.filter(item => { return item.projectCode.indexOf(this.userName.toUpperCase()) != '-1' });
  222. let dataTree = data.concat(dataList)
  223. let arrList = Array.from(new Set(dataTree))
  224. this.projectss = arrList
  225. } else {
  226. this.projectss = this.proads
  227. }
  228. },
  229. getToday() {
  230. return this.formatDate(new Date())
  231. },
  232. formatDate(date) {
  233. const year = date.getFullYear();
  234. const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份,并确保两位数
  235. const day = String(date.getDate()).padStart(2, '0'); // 获取日期,并确保两位数
  236. const hours = String(date.getHours()).padStart(2, '0'); // 获取小时,并确保两位数
  237. const minutes = String(date.getMinutes()).padStart(2, '0'); // 获取分钟,并确保两位数
  238. return `${year}-${month}-${day} ${hours}:${minutes}`;
  239. },
  240. endDateChange(date) {
  241. console.log('执行了')
  242. this.equipmentForm.startTime = this.formatDate(date)
  243. this.endDateShow = false
  244. },
  245. endDateChangeTwo(date) {
  246. this.equipmentForm.endTime = this.formatDate(date)
  247. this.endDateShowTwo = false
  248. },
  249. back() {
  250. history.back();
  251. },
  252. getRecentlyProject() {
  253. this.$axios.post('/project/nearProject', {})
  254. .then(res => {
  255. if (res.code == 'ok') {
  256. this.integrationProjectList = res.data;
  257. }
  258. }).catch(err => { this.$toast.clear(); this.cardRefLoading = false; })
  259. },
  260. getPeoject() {
  261. this.$axios.post("/project/getProjectList", { forReport: 1 })
  262. .then(res => {
  263. if (res.code == "ok") {
  264. for (var i in res.data) {
  265. if (res.data[i].projectCode == 'null' || res.data[i].projectCode == null) {
  266. res.data[i].projectCode = ' '
  267. }
  268. }
  269. this.projectss = res.data;
  270. this.project = res.data;
  271. this.projectss = this.projectss.filter(p => p.status == 1 || p.status == 4);
  272. this.proads = res.data
  273. } else {
  274. this.$toast.fail('获取失败:' + res.msg);
  275. }
  276. }).catch(err => { this.$toast.clear(); });
  277. },
  278. },
  279. };
  280. </script>
  281. <style lang="less" scoped>
  282. .useRegistration {
  283. width: 100%;
  284. height: 100vh;
  285. display: flex;
  286. flex-direction: column;
  287. background: #fff;
  288. .useRegistration-content {
  289. flex: 1;
  290. overflow: auto;
  291. }
  292. .useRegistration-footer {
  293. width: 100%;
  294. padding: 20px 20px 20px 20px;
  295. }
  296. .tabIcon {
  297. font-size: 20px;
  298. font-weight: bold;
  299. }
  300. .qrcodeClass {
  301. position: absolute;
  302. width: 100%;
  303. height: 100vh;
  304. z-index: 9999;
  305. }
  306. .equipmentUsage {
  307. width: 100%;
  308. text-align: center;
  309. color: #ee0a24;
  310. padding-top: 40px;
  311. }
  312. }
  313. </style>