Device.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <template>
  2. <schema-table
  3. ref="table"
  4. row-key="id"
  5. :schema="schema"
  6. >
  7. <confirm-dialog
  8. ref="editDialog"
  9. :title="dialogTitle"
  10. @confirm="onSave"
  11. >
  12. <template #default>
  13. <div class="c-grid-form u-align-self--center">
  14. <span class="c-grid-form__label u-required">名称</span>
  15. <el-input
  16. v-model.trim="currObj.name"
  17. placeholder="最多30个字符"
  18. maxlength="30"
  19. clearable
  20. />
  21. <span class="c-grid-form__label u-required">配置</span>
  22. <schema-select
  23. v-model="currObj.productId"
  24. class="u-width"
  25. :schema="productSelectSchema"
  26. placeholder="请选择配置"
  27. />
  28. <span class="c-grid-form__label u-required">型号</span>
  29. <el-autocomplete
  30. v-model="currObj.remark"
  31. class="u-width"
  32. :fetch-suggestions="querySearch"
  33. placeholder="播控器的系列或型号"
  34. />
  35. <span class="c-grid-form__label u-required">序列号</span>
  36. <el-input
  37. v-model.trim="currObj.serialNumber"
  38. placeholder="最多50个字符"
  39. maxlength="50"
  40. clearable
  41. />
  42. <span class="c-grid-form__label u-required">MAC</span>
  43. <el-input
  44. v-model.trim="currObj.mac"
  45. class="u-width"
  46. placeholder="ff:ff:ff:ff:ff:ff"
  47. maxlength="17"
  48. clearable
  49. />
  50. <div class="c-grid-form__label u-required">开关机时间</div>
  51. <el-time-picker
  52. v-model="currObj.range"
  53. class="u-width u-pointer"
  54. is-range
  55. value-format="HH:mm:ss"
  56. :clearable="false"
  57. />
  58. <span class="c-grid-form__label u-required">地址</span>
  59. <el-input
  60. v-model.trim="currObj.address"
  61. type="textarea"
  62. placeholder="最多100个字符"
  63. maxlength="100"
  64. :rows="2"
  65. show-word-limit
  66. />
  67. <span class="c-grid-form__label">坐标</span>
  68. <div class="l-flex--row c-grid-form__option">
  69. <span class="c-sibling-item">{{ currObj.longitude }},{{ currObj.latitude }}</span>
  70. <i
  71. class="c-sibling-item el-icon-edit u-color--blue has-active"
  72. @click="onEditCoordinate"
  73. />
  74. </div>
  75. </div>
  76. </template>
  77. </confirm-dialog>
  78. <coordinate-dialog
  79. ref="coordinateDialog"
  80. @confirm="onChangeCoordinate"
  81. />
  82. <mesh-dialog ref="meshDialog" />
  83. <confirm-dialog
  84. ref="replaceDialog"
  85. title="设备替换"
  86. @confirm="onConfirmReplace"
  87. >
  88. <template #default>
  89. <div class="c-grid-form u-align-self--center">
  90. <span class="c-grid-form__label u-required">序列号</span>
  91. <el-input
  92. v-model.trim="currObj.serialNumber"
  93. placeholder="最多50个字符"
  94. maxlength="50"
  95. clearable
  96. />
  97. <span class="c-grid-form__label u-required">MAC</span>
  98. <el-input
  99. v-model.trim="currObj.mac"
  100. class="u-width"
  101. placeholder="ff:ff:ff:ff:ff:ff"
  102. maxlength="17"
  103. clearable
  104. />
  105. <span class="c-grid-form__label">配置</span>
  106. <schema-select
  107. ref="productSelect"
  108. v-model="currObj.productId"
  109. class="u-width"
  110. :schema="replaceProductSelectSchema"
  111. />
  112. </div>
  113. </template>
  114. </confirm-dialog>
  115. </schema-table>
  116. </template>
  117. <script>
  118. import { BoxModels } from '@/constant'
  119. import {
  120. validMAC,
  121. validLongitude,
  122. validLatitude
  123. } from '@/utils/validate'
  124. import {
  125. getDevicesByTenant,
  126. addDevice,
  127. deleteDevice,
  128. getSubDevices,
  129. addSubDevice,
  130. activateDevice,
  131. deactivateDevice,
  132. getProducts,
  133. updateDevice,
  134. replaceDevice
  135. } from '@/api/device'
  136. import MeshDialog from '../../components/MeshDialog.vue'
  137. export default {
  138. name: 'Device',
  139. components: {
  140. MeshDialog
  141. },
  142. props: {
  143. tenant: {
  144. type: String,
  145. required: true
  146. }
  147. },
  148. data () {
  149. const productSelectSchema = {
  150. remote: this.getProducts,
  151. pagination: true,
  152. value: 'id',
  153. label: 'name'
  154. }
  155. return {
  156. currObj: {},
  157. isSub: false,
  158. productSelectSchema,
  159. replaceProductSelectSchema: {
  160. ...productSelectSchema,
  161. placeholder: '不选择时使用原配置'
  162. },
  163. schema: {
  164. keepalive: true,
  165. props: {
  166. size: 'small'
  167. },
  168. // listeners: {
  169. // 'row-click': __SUB_DEVICE__ ? this.onRowClick : null
  170. // },
  171. list: this.getDevicesByTenant,
  172. transform: this.transform,
  173. // transformData: __SUB_DEVICE__ && this.transformTableData,
  174. buttons: [
  175. { type: 'add', on: this.onAddDevice }
  176. ],
  177. filters: [
  178. { key: 'productId', type: 'select', placeholder: '配置', ...productSelectSchema },
  179. { key: 'boundFlag', type: 'select', placeholder: '使用情况', options: [
  180. { value: 1, label: '已使用' },
  181. { value: 0, label: '未使用' }
  182. ] },
  183. { key: 'serialNumber', type: 'search', placeholder: '序列号' },
  184. { key: 'mac', type: 'search', placeholder: 'MAC' },
  185. { key: 'name', type: 'search', placeholder: '设备名称' }
  186. ],
  187. cols: [
  188. // { type: 'refresh', render: __SUB_DEVICE__
  189. // ? (data, h) => data.isMaster
  190. // ? h('i', {
  191. // staticClass: `o-expand-icon u-pointer ${data.loading ? 'el-icon-loading' : 'el-icon-arrow-right'}`,
  192. // class: { expand: data.expand }
  193. // })
  194. // : null
  195. // : null },
  196. // __SUB_DEVICE__
  197. // ? { label: '设备名称', render: (data, h) => data.empty ? h('span', { staticClass: 'u-color--info' }, '暂无备份设备') : data.name, 'min-width': 120 }
  198. // : { prop: 'name', label: '设备名称', 'min-width': 120 },
  199. { type: 'refresh' },
  200. { prop: 'name', label: '设备名称', 'min-width': 120 },
  201. { label: '型号', render: (data, h) => data.empty
  202. ? ''
  203. : h('edit-input', {
  204. props: {
  205. value: data.remark,
  206. placeholder: '-'
  207. },
  208. on: { edit: val => this.onEditRemark(data, val) }
  209. }), 'class-name': 'c-edit-column' },
  210. { prop: 'productName', label: '配置' },
  211. { prop: 'serialNumber', label: '序列号', 'min-width': 160 },
  212. { prop: 'mac', label: 'MAC', 'min-width': 120 },
  213. { type: 'tag', render: ({ empty, activate, onlineStatus }) => empty
  214. ? null
  215. : activate
  216. ? onlineStatus === 0
  217. ? { type: 'primary', label: '待接入' }
  218. : onlineStatus === 1
  219. ? { type: 'success', label: '在线' }
  220. : { type: 'danger', label: '离线' }
  221. : { type: 'warning', label: '未激活' },
  222. 'size': 'sm', width: 80, on: this.onTagClick },
  223. { label: '使用情况', type: 'tag', render: ({ empty, isMaster, bound }) => empty || !isMaster
  224. ? null
  225. : bound
  226. ? { type: 'success', label: '已使用' }
  227. : { type: 'primary', label: '未使用', ignore: true }, 'size': 'sm', width: 80, on: this.onViewMesh },
  228. { type: 'invoke', render: [
  229. { label: '配置', render: ({ isMaster }) => isMaster, on: this.onSettingDevice },
  230. { label: '替换', render: ({ isMaster }) => isMaster, on: this.onReplace },
  231. // __SUB_DEVICE__ && { label: '备机', render: ({ isMaster }) => isMaster, on: this.onAddSubDevice },
  232. { label: '删除', render: ({ empty }) => !empty, on: this.onDelDevice }
  233. ], width: 140 }
  234. ]
  235. }
  236. }
  237. },
  238. computed: {
  239. dialogTitle () {
  240. return this.isSub ? '新增备份设备' : '新增设备'
  241. }
  242. },
  243. methods: {
  244. querySearch (val, cb) {
  245. cb(val ? [] : [...BoxModels])
  246. },
  247. getProducts (params) {
  248. return getProducts({
  249. tenant: this.tenant,
  250. ...params
  251. })
  252. },
  253. getDevicesByTenant (params) {
  254. return getDevicesByTenant(this.tenant, params)
  255. },
  256. transform (data) {
  257. return {
  258. ...data,
  259. loaded: false,
  260. loading: false,
  261. expand: false,
  262. subs: [],
  263. isMaster: true
  264. }
  265. },
  266. transformTableData (data) {
  267. const arr = []
  268. data.forEach(item => {
  269. arr.push(item)
  270. if (item.loaded && item.expand) {
  271. arr.push(...item.subs)
  272. }
  273. })
  274. return arr
  275. },
  276. onAddDevice () {
  277. this.isSub = false
  278. this.$master = null
  279. this.productSelectSchema.option = null
  280. this.onAdd()
  281. },
  282. onAddSubDevice (item) {
  283. this.isSub = true
  284. this.$master = item
  285. this.productSelectSchema.option = { value: item.productId, label: item.productName }
  286. this.onAdd()
  287. },
  288. onAdd () {
  289. if (this.isSub) {
  290. const { productId, openTime, closeTime, address, longitude, latitude } = this.$master
  291. this.currObj = {
  292. name: '',
  293. remark: '',
  294. productId,
  295. serialNumber: '',
  296. mac: '',
  297. range: [openTime, closeTime],
  298. address,
  299. longitude,
  300. latitude,
  301. tenant: this.tenant
  302. }
  303. } else {
  304. this.currObj = {
  305. name: '',
  306. remark: '',
  307. productId: '',
  308. serialNumber: '',
  309. mac: '',
  310. range: ['08:00:00', '22:00:00'],
  311. address: '',
  312. longitude: '',
  313. latitude: '',
  314. tenant: this.tenant
  315. }
  316. }
  317. this.$refs.editDialog.show()
  318. },
  319. onEditCoordinate () {
  320. const { longitude, latitude, address } = this.currObj
  321. this.$refs.coordinateDialog.show({
  322. longitude,
  323. latitude,
  324. address
  325. })
  326. },
  327. onChangeCoordinate ({ value: { longitude, latitude, address }, done }) {
  328. this.currObj.longitude = longitude
  329. this.currObj.latitude = latitude
  330. this.currObj.address = address
  331. done()
  332. },
  333. onSave (done) {
  334. if (this.check(this.currObj)) {
  335. const { mac, range, ...data } = this.currObj
  336. if (this.isSub) {
  337. addSubDevice(this.$master, {
  338. openTime: range[0],
  339. closeTime: range[1],
  340. mac: mac.toLowerCase(),
  341. ...data
  342. }).then(() => {
  343. done()
  344. this.reloadSubDevices(this.$master)
  345. })
  346. } else {
  347. addDevice({
  348. openTime: range[0],
  349. closeTime: range[1],
  350. mac: mac.toLowerCase(),
  351. ...data
  352. }).then(() => {
  353. done()
  354. this.$refs.table.resetCondition({
  355. boundFlag: 0,
  356. name: this.currObj.name,
  357. productId: this.currObj.productId,
  358. serialNumber: this.currObj.serialNumber,
  359. mac: mac.toLowerCase()
  360. })
  361. })
  362. }
  363. }
  364. },
  365. check (item) {
  366. if (!item.name) {
  367. this.$message({
  368. type: 'warning',
  369. message: '名称不能为空'
  370. })
  371. return false
  372. }
  373. if (!item.productId) {
  374. this.$message({
  375. type: 'warning',
  376. message: '请选择配置'
  377. })
  378. return false
  379. }
  380. if (!item.serialNumber) {
  381. this.$message({
  382. type: 'warning',
  383. message: '序列号不能为空'
  384. })
  385. return false
  386. }
  387. if (!item.mac) {
  388. this.$message({
  389. type: 'warning',
  390. message: 'MAC不能为空'
  391. })
  392. return false
  393. }
  394. if (!validMAC(item.mac)) {
  395. this.$message({
  396. type: 'warning',
  397. message: 'MAC格式不正确,例 ff:ff:ff:ff:ff:ff'
  398. })
  399. return false
  400. }
  401. if (!item.range || !item.range[0] || !item.range[1]) {
  402. this.$message({
  403. type: 'warning',
  404. message: '请选择开关机时间'
  405. })
  406. return false
  407. }
  408. if (item.range[0] >= item.range[1]) {
  409. this.$message({
  410. type: 'warning',
  411. message: '开机时间必须小于关机时间'
  412. })
  413. return false
  414. }
  415. if (!item.address) {
  416. this.$message({
  417. type: 'warning',
  418. message: '地址不能为空'
  419. })
  420. return false
  421. }
  422. if (item.longitude && !validLongitude(item.longitude)) {
  423. this.$message({
  424. type: 'warning',
  425. message: '经度格式错误,-180 ~ +180'
  426. })
  427. return false
  428. }
  429. if (item.latitude && !validLatitude(item.latitude)) {
  430. this.$message({
  431. type: 'warning',
  432. message: '纬度格式错误,-90 ~ +90'
  433. })
  434. return false
  435. }
  436. return true
  437. },
  438. onViewDevice (item) {
  439. this.$router.push({
  440. name: 'device-management-detail',
  441. params: {
  442. id: item.id
  443. }
  444. })
  445. },
  446. onDelDevice (item) {
  447. deleteDevice(item).then(() => {
  448. if (item.isMaster) {
  449. this.$refs.table.decrease(1)
  450. } else {
  451. this.reloadSubDevices(item.parent)
  452. }
  453. })
  454. },
  455. reloadSubDevices (item) {
  456. item.loaded = false
  457. item.children = []
  458. this.getSubDevices(item)
  459. },
  460. getSubDevices (item, pageSize = 999) {
  461. item.loading = true
  462. getSubDevices({
  463. id: item.id,
  464. pageNum: 1,
  465. pageSize
  466. }).then(({ data, totalCount }) => {
  467. if (totalCount > pageSize) {
  468. this.getSubDevices(item, totalCount)
  469. } else {
  470. item.loading = false
  471. item.loaded = true
  472. item.expand = true
  473. if (data.length === 0) {
  474. item.subs = [{ id: `${Math.random()}`, empty: true }]
  475. } else {
  476. item.subs = data.map(device => {
  477. return {
  478. ...device,
  479. isMaster: false,
  480. empty: false,
  481. parent: item
  482. }
  483. })
  484. }
  485. }
  486. }, () => {
  487. item.loading = false
  488. })
  489. },
  490. onRowClick (data) {
  491. if (__SUB_DEVICE__ && data.isMaster) {
  492. if (data.loaded) {
  493. data.expand = !data.expand
  494. } else if (!data.loading) {
  495. this.getSubDevices(data)
  496. }
  497. }
  498. },
  499. onTagClick (data) {
  500. (data.activate ? deactivateDevice : activateDevice)(data).then(() => {
  501. if (data.isMaster) {
  502. this.$refs.table.pageTo()
  503. } else {
  504. this.reloadSubDevices(data.parent)
  505. }
  506. })
  507. },
  508. onSettingDevice ({ id }) {
  509. this.$router.push({
  510. name: 'box-settings',
  511. params: { id }
  512. })
  513. },
  514. onViewMesh ({ id, bound }) {
  515. if (bound) {
  516. this.$refs.meshDialog.show(id)
  517. }
  518. },
  519. onEditRemark (device, { newVal, oldVal }) {
  520. if (newVal === oldVal) {
  521. return
  522. }
  523. device.remark = newVal
  524. updateDevice({
  525. id: device.id,
  526. remark: newVal
  527. }).catch(() => {
  528. device.remark = oldVal
  529. })
  530. },
  531. onReplace (device) {
  532. this.$device = device
  533. this.currObj = {
  534. serialNumber: '',
  535. mac: '',
  536. productId: ''
  537. }
  538. this.$refs.replaceDialog.show()
  539. },
  540. onConfirmReplace (done) {
  541. const { serialNumber, mac, productId } = this.currObj
  542. if (!serialNumber) {
  543. this.$message({
  544. type: 'warning',
  545. message: '序列号不能为空'
  546. })
  547. return
  548. }
  549. if (!mac) {
  550. this.$message({
  551. type: 'warning',
  552. message: 'MAC不能为空'
  553. })
  554. return
  555. }
  556. if (!validMAC(mac)) {
  557. this.$message({
  558. type: 'warning',
  559. message: 'MAC格式不正确,例 ff:ff:ff:ff:ff:ff'
  560. })
  561. return
  562. }
  563. const realMac = mac.toLowerCase()
  564. if (serialNumber === this.$device.serialNumber && realMac === this.$device.mac) {
  565. this.$message({
  566. type: 'warning',
  567. message: '序列号与MAC不能均与原数据一致'
  568. })
  569. return
  570. }
  571. const productName = productId ? this.$refs.productSelect.getOptions().find(({ value }) => value === productId).label : ''
  572. replaceDevice(this.$device.id, { serialNumber, mac: realMac, productId }).then(() => {
  573. this.$device.serialNumber = serialNumber
  574. this.$device.mac = realMac
  575. if (productId) {
  576. this.$device.productId = productId
  577. this.$device.productName = productName
  578. }
  579. this.$device = null
  580. done()
  581. })
  582. }
  583. }
  584. }
  585. </script>