index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <c-table
  9. ref="table"
  10. :curr="currOptions"
  11. row-key="id"
  12. @pagination="getList"
  13. @row-click="onRowClick"
  14. >
  15. <template #header>
  16. <div class="l-flex__auto c-sibling-item">
  17. <button
  18. v-if="canEdit"
  19. class="o-button"
  20. @click="onAdd"
  21. >
  22. <i class="o-button__icon el-icon-circle-plus-outline" />
  23. 新增
  24. </button>
  25. </div>
  26. <el-select
  27. v-model="options.params.productId"
  28. class="l-flex__none c-sibling-item"
  29. placeholder="请选择产品"
  30. :loading="fetching"
  31. @visible-change="getProducts"
  32. @change="search"
  33. >
  34. <el-option
  35. v-for="item in productOptions"
  36. :key="item.value"
  37. :label="item.label"
  38. :value="item.value"
  39. />
  40. </el-select>
  41. <search-input
  42. v-model.trim="options.params.name"
  43. class="l-flex__none c-sibling-item"
  44. placeholder="设备名称"
  45. @search="search"
  46. />
  47. <button
  48. class="l-flex__none c-sibling-item o-button"
  49. @click="search"
  50. >
  51. 搜索
  52. </button>
  53. </template>
  54. <el-table-column
  55. width="50"
  56. align="center"
  57. >
  58. <template #header>
  59. <i
  60. class="o-refresh el-icon-refresh"
  61. @click="getList"
  62. />
  63. </template>
  64. <template v-slot="scope">
  65. <i
  66. v-if="scope.row.isMaster"
  67. class="o-expand-icon u-pointer"
  68. :class="{ expand: scope.row.expand, 'el-icon-arrow-right': !scope.row.loading, 'el-icon-loading': scope.row.loading }"
  69. />
  70. </template>
  71. </el-table-column>
  72. <el-table-column
  73. label="设备名称"
  74. align="center"
  75. min-width="120"
  76. show-overflow-tooltip
  77. >
  78. <template v-slot="scope">
  79. <span
  80. v-if="scope.row.empty"
  81. class="o-empty"
  82. >
  83. 暂无备份设备
  84. </span>
  85. <template v-else>{{ scope.row.name }}</template>
  86. </template>
  87. </el-table-column>
  88. <el-table-column
  89. prop="productName"
  90. label="产品"
  91. align="center"
  92. min-width="120"
  93. show-overflow-tooltip
  94. />
  95. <el-table-column
  96. prop="serialNumber"
  97. label="序列号"
  98. align="center"
  99. min-width="120"
  100. show-overflow-tooltip
  101. />
  102. <el-table-column
  103. prop="mac"
  104. label="MAC"
  105. align="center"
  106. class-name="copy"
  107. min-width="120"
  108. show-overflow-tooltip
  109. />
  110. <el-table-column
  111. prop="resolutionRatio"
  112. label="分辨率"
  113. align="center"
  114. min-width="120"
  115. />
  116. <el-table-column
  117. label="状态"
  118. align="center"
  119. min-width="100"
  120. >
  121. <template v-slot="scope">
  122. <el-tag
  123. v-if="scope.row.activate === 0"
  124. class="o-tag u-pointer"
  125. type="warning"
  126. size="medium"
  127. @click.stop="toggleActivate(scope.row)"
  128. >
  129. 未激活
  130. </el-tag>
  131. <el-tag
  132. v-if="scope.row.activate === 1"
  133. class="o-tag u-pointer"
  134. size="medium"
  135. @click.stop="toggleActivate(scope.row)"
  136. >
  137. 已激活
  138. </el-tag>
  139. <el-tag
  140. v-if="scope.row.activate === 2"
  141. class="o-tag u-pointer"
  142. :type="scope.row.onlineStatus === 1 ? 'success' : 'danger'"
  143. size="medium"
  144. @click.stop="toggleActivate(scope.row)"
  145. >
  146. {{ scope.row.onlineStatus === 1 ? '在线' : '离线' }}
  147. </el-tag>
  148. </template>
  149. </el-table-column>
  150. <el-table-column
  151. label="操作"
  152. align="center"
  153. :width="operationColumnWidth"
  154. >
  155. <template v-slot="scope">
  156. <template v-if="!scope.row.empty">
  157. <div
  158. class="c-table__btn u-pointer"
  159. @click.stop="onEditDevice(scope.row)"
  160. >
  161. 查看
  162. </div>
  163. <template v-if="canEdit">
  164. <div
  165. v-if="scope.row.isMaster"
  166. class="c-table__btn u-pointer"
  167. @click.stop="onAddSubDevice(scope.row)"
  168. >
  169. 添加备份设备
  170. </div>
  171. <div
  172. v-if="scope.row.isMaster && scope.row.activate === 2"
  173. class="c-table__btn u-pointer"
  174. @click.stop="setDefaultProgram(scope.row)"
  175. >
  176. 默认播放
  177. </div>
  178. <div
  179. class="c-table__btn u-pointer"
  180. @click.stop="onDelDevice(scope.row)"
  181. >
  182. 删除
  183. </div>
  184. </template>
  185. </template>
  186. </template>
  187. </el-table-column>
  188. </c-table>
  189. <el-dialog
  190. :title="dialogTitle"
  191. :visible.sync="show"
  192. custom-class="c-dialog"
  193. :close-on-click-modal="false"
  194. :before-close="close"
  195. >
  196. <div class="c-form">
  197. <div class="c-form__section">
  198. <span class="c-form__label required">名称:</span>
  199. <el-input
  200. v-model.trim="currObj.name"
  201. class="c-form__item"
  202. maxlength="50"
  203. show-word-limit
  204. />
  205. </div>
  206. <div class="c-form__section">
  207. <span class="c-form__label required">产品:</span>
  208. <el-select
  209. v-model="currObj.productId"
  210. class="c-form__item"
  211. placeholder="请选择产品"
  212. :loading="fetching"
  213. :disabled="isSub"
  214. @visible-change="getProducts"
  215. >
  216. <el-option
  217. v-for="product in products"
  218. :key="product.value"
  219. :label="product.label"
  220. :value="product.value"
  221. />
  222. </el-select>
  223. </div>
  224. <div class="c-form__section">
  225. <span class="c-form__label required">序列号:</span>
  226. <el-input
  227. v-model.trim="currObj.serialNumber"
  228. class="c-form__item"
  229. maxlength="50"
  230. />
  231. </div>
  232. <div class="c-form__section extra o-mac">
  233. <span class="c-form__label required">MAC:</span>
  234. <el-input
  235. v-model.trim="currObj.mac"
  236. class="c-form__item"
  237. maxlength="17"
  238. />
  239. </div>
  240. <div class="c-form__section">
  241. <span class="c-form__label required">地址:</span>
  242. <el-input
  243. v-model="currObj.remark"
  244. class="c-form__item"
  245. type="textarea"
  246. :rows="3"
  247. maxlength="100"
  248. show-word-limit
  249. />
  250. </div>
  251. </div>
  252. <template #footer>
  253. <button
  254. class="o-button"
  255. @click="onSave"
  256. >
  257. 确定
  258. </button>
  259. <button
  260. class="o-button cancel"
  261. @click="close"
  262. >
  263. 取消
  264. </button>
  265. </template>
  266. </el-dialog>
  267. <el-dialog
  268. :visible.sync="choosing"
  269. title="节目选择"
  270. custom-class="c-dialog hidden-footer"
  271. >
  272. <event-target-choose
  273. v-if="choosing"
  274. :ratio="ratio"
  275. @choose="onChoose"
  276. />
  277. </el-dialog>
  278. </wrapper>
  279. </template>
  280. <script>
  281. import {
  282. getDevices,
  283. addDevice,
  284. deleteDevice,
  285. updateDevice,
  286. getSubDevices,
  287. addSubDevice,
  288. activateDevice,
  289. deactivateDevice,
  290. getProducts
  291. } from '@/api/device'
  292. import { publish } from '@/api/publish'
  293. import {
  294. EventPriority,
  295. EventFreq,
  296. PublishType
  297. } from '@/constant'
  298. import { createListOptions } from '@/utils'
  299. import { toDateStr } from '@/utils/event'
  300. import mixin from './mixins'
  301. import EventTargetChoose from '@/components/EventTargetChoose'
  302. export default {
  303. name: 'DeviceList',
  304. components: {
  305. EventTargetChoose
  306. },
  307. mixins: [mixin],
  308. data () {
  309. return {
  310. proxy: {
  311. list: getDevices,
  312. add: addDevice,
  313. update: updateDevice,
  314. del: deleteDevice
  315. },
  316. options: createListOptions({
  317. productId: '',
  318. name: ''
  319. }),
  320. productOptions: [
  321. { value: '', label: '全部设备' }
  322. ],
  323. loaded: false,
  324. fetching: false,
  325. firstLoadSize: 999,
  326. isSub: false,
  327. device: null,
  328. choosing: false,
  329. ratio: null
  330. }
  331. },
  332. computed: {
  333. canEdit () {
  334. return this.accessSet.has(this.Access.MANAGE_DEVICES)
  335. },
  336. canEditDefaultProgram () {
  337. return this.canEdit || this.accessSet.has(this.Access.MANAGE_DEVICE)
  338. },
  339. operationColumnWidth () {
  340. return this.canEdit ? 340 : this.canEditDefaultProgram ? 260 : 120
  341. },
  342. type () {
  343. return this.isSub ? '备份设备' : '设备'
  344. },
  345. products () {
  346. return this.isSub
  347. ? [{ value: this.$master.productId, label: this.$master.productName }]
  348. : this.loaded
  349. ? this.productOptions.slice(1)
  350. : []
  351. },
  352. tableData () {
  353. const arr = []
  354. this.list.forEach(item => {
  355. arr.push(item)
  356. if (item.loaded && item.expand) {
  357. arr.push(...item.subs)
  358. }
  359. })
  360. return arr
  361. },
  362. currOptions () {
  363. const arr = []
  364. this.options.list.forEach(item => {
  365. arr.push(item)
  366. if (item.loaded && item.expand) {
  367. arr.push(...item.subs)
  368. }
  369. })
  370. return {
  371. ...this.options,
  372. list: arr
  373. }
  374. }
  375. },
  376. activated () {
  377. this.getList()
  378. },
  379. methods: {
  380. _getProducts () {
  381. this.fetching = true
  382. getProducts({ pageNum: 1, pageSize: this.firstLoadSize }).then(({ data, totalCount }) => {
  383. if (totalCount > this.firstLoadSize) {
  384. this.firstLoadSize = totalCount
  385. this._getProducts()
  386. } else {
  387. this.productOptions = this.productOptions.concat(data.map(({ id, name }) => {
  388. return { value: id, label: name }
  389. }))
  390. this.fetching = false
  391. this.loaded = true
  392. }
  393. }, () => {
  394. this.fetching = false
  395. })
  396. },
  397. getProducts (visible) {
  398. if (visible && !this.fetching && !this.loaded) {
  399. this._getProducts()
  400. }
  401. },
  402. onRowClick (row) {
  403. if (row.isMaster) {
  404. if (row.loaded) {
  405. row.expand = !row.expand
  406. } else if (!row.loading) {
  407. this.getSubDevices(row)
  408. }
  409. }
  410. },
  411. getDefaults () {
  412. return {
  413. name: '',
  414. productId: this.isSub && this.$master.productId || this.options.params.productId,
  415. serialNumber: '',
  416. mac: '',
  417. remark: ''
  418. }
  419. },
  420. transform (data) {
  421. return data.map(item => {
  422. return {
  423. ...item,
  424. loaded: false,
  425. loading: false,
  426. expand: false,
  427. subs: [],
  428. isMaster: true
  429. }
  430. })
  431. },
  432. getSubDevices (item, pageSize = 999) {
  433. item.loading = true
  434. getSubDevices({
  435. id: item.id,
  436. pageNum: 1,
  437. pageSize
  438. }).then(({ data, totalCount }) => {
  439. if (totalCount > pageSize) {
  440. this.getSubDevices(item, totalCount)
  441. } else {
  442. item.loading = false
  443. item.loaded = true
  444. item.expand = true
  445. if (data.length === 0) {
  446. item.subs = [{ id: `${Math.random()}`, empty: true }]
  447. } else {
  448. item.subs = data.map(device => {
  449. return {
  450. ...device,
  451. isMaster: false,
  452. empty: false,
  453. parent: item
  454. }
  455. })
  456. }
  457. }
  458. }, () => {
  459. item.loading = false
  460. })
  461. },
  462. afterAdd () {
  463. const params = this.options.params
  464. if (params.name && !(new RegExp(params.name).test(this.currObj.name))) {
  465. params.name = ''
  466. }
  467. if (params.productId && params.productId !== this.currObj.productId) {
  468. params.productId = ''
  469. }
  470. },
  471. check (item) {
  472. if (!item.name) {
  473. this.$message({
  474. type: 'warning',
  475. message: '名称不能为空'
  476. })
  477. return false
  478. }
  479. if (!item.productId) {
  480. this.$message({
  481. type: 'warning',
  482. message: '请选择产品'
  483. })
  484. return false
  485. }
  486. if (!item.serialNumber) {
  487. this.$message({
  488. type: 'warning',
  489. message: '序列号不能为空'
  490. })
  491. return false
  492. }
  493. if (!item.mac) {
  494. this.$message({
  495. type: 'warning',
  496. message: 'MAC不能为空'
  497. })
  498. return false
  499. }
  500. if (!/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/.test(item.mac)) {
  501. this.$message({
  502. type: 'warning',
  503. message: 'MAC格式不正确,例 ff:ff:ff:ff:ff:ff'
  504. })
  505. return false
  506. }
  507. item.mac = item.mac.toLowerCase()
  508. if (!item.remark) {
  509. this.$message({
  510. type: 'warning',
  511. message: '地址不能为空'
  512. })
  513. return false
  514. }
  515. return true
  516. },
  517. close () {
  518. this.isSub = false
  519. this.$device = null
  520. this.show = false
  521. },
  522. onAddSubDevice (item) {
  523. this.isSub = true
  524. this.$master = item
  525. this.onAdd()
  526. },
  527. onEditDevice (item) {
  528. this.$router.push({
  529. name: 'device-detail',
  530. params: {
  531. id: item.id
  532. }
  533. })
  534. },
  535. reloadSubDevices (item) {
  536. item.loaded = false
  537. item.children = []
  538. this.getSubDevices(item)
  539. },
  540. onSave () {
  541. if (this.isSub) {
  542. if (this.check(this.currObj)) {
  543. addSubDevice(this.$master, this.currObj).then(() => {
  544. this.reloadSubDevices(this.$master)
  545. this.close()
  546. })
  547. }
  548. } else {
  549. this.save()
  550. }
  551. },
  552. onDelDevice (item) {
  553. if (item.isMaster) {
  554. this.onDel(item)
  555. } else {
  556. this.proxy.del(item).then(() => {
  557. this.reloadSubDevices(item.parent)
  558. })
  559. }
  560. },
  561. toggleActivate (item) {
  562. (item.activate ? deactivateDevice : activateDevice)(item).then(() => {
  563. if (item.isMaster) {
  564. this.getList()
  565. } else {
  566. this.reloadSubDevices(item.parent)
  567. }
  568. })
  569. },
  570. setDefaultProgram (device) {
  571. this.ratio = device.resolutionRatio
  572. this.$device = {
  573. id: device.id,
  574. name: device.name
  575. }
  576. this.choosing = true
  577. },
  578. onChoose (target) {
  579. this.$confirm(`确定将设备 ${this.$device.name} 的默认播放设置为 ${target.name}?`).then(() => {
  580. return publish(
  581. [this.$device.id],
  582. {
  583. type: PublishType.EVENT,
  584. detail: {
  585. priority: EventPriority.DEFAULT,
  586. freq: EventFreq.ONCE,
  587. start: `${toDateStr(new Date())} 00:00:00`,
  588. until: null,
  589. target
  590. }
  591. },
  592. {
  593. programCalendarName: target.name,
  594. resolutionRatio: this.ratio
  595. }
  596. )
  597. }).then(() => {
  598. this.choosing = false
  599. })
  600. }
  601. }
  602. }
  603. </script>
  604. <style lang="scss" scoped>
  605. .o-item {
  606. display: block;
  607. margin-right: 0;
  608. margin-bottom: 0;
  609. ::v-deep {
  610. label {
  611. width: 90px;
  612. color: #99a9bf;
  613. }
  614. }
  615. }
  616. .o-expand-icon {
  617. transition: transform 0.2s;
  618. &.expand {
  619. transform: rotate(90deg);
  620. }
  621. }
  622. .o-empty {
  623. color: $gray;
  624. font-size: 14px;
  625. }
  626. .o-mac::after {
  627. content: "例: ff:ff:ff:ff:ff:ff";
  628. }
  629. </style>