index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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="scope.row.isMaster">
  164. <div
  165. v-if="canEdit"
  166. class="c-table__btn u-pointer"
  167. @click.stop="onAddSubDevice(scope.row)"
  168. >
  169. 添加备份设备
  170. </div>
  171. <div
  172. v-if="canEditDefaultProgram && scope.row.activate === 2"
  173. class="c-table__btn u-pointer"
  174. @click.stop="setDefaultProgram(scope.row)"
  175. >
  176. 默认播放
  177. </div>
  178. </template>
  179. <div
  180. v-if="canEdit"
  181. class="c-table__btn u-pointer"
  182. @click.stop="onDelDevice(scope.row)"
  183. >
  184. 删除
  185. </div>
  186. </template>
  187. </template>
  188. </el-table-column>
  189. </c-table>
  190. <confirm-dialog
  191. ref="editDialog"
  192. :title="dialogTitle"
  193. @confirm="onSave"
  194. >
  195. <div class="c-form">
  196. <div class="c-form__section">
  197. <span class="c-form__label required">名称:</span>
  198. <el-input
  199. v-model.trim="currObj.name"
  200. class="c-form__item"
  201. maxlength="50"
  202. show-word-limit
  203. />
  204. </div>
  205. <div class="c-form__section">
  206. <span class="c-form__label required">产品:</span>
  207. <el-select
  208. v-model="currObj.productId"
  209. class="c-form__item"
  210. placeholder="请选择产品"
  211. :loading="fetching"
  212. :disabled="isSub"
  213. @visible-change="getProducts"
  214. >
  215. <el-option
  216. v-for="product in products"
  217. :key="product.value"
  218. :label="product.label"
  219. :value="product.value"
  220. />
  221. </el-select>
  222. </div>
  223. <div class="c-form__section">
  224. <span class="c-form__label required">序列号:</span>
  225. <el-input
  226. v-model.trim="currObj.serialNumber"
  227. class="c-form__item"
  228. maxlength="50"
  229. />
  230. </div>
  231. <div class="c-form__section extra o-mac">
  232. <span class="c-form__label required">MAC:</span>
  233. <el-input
  234. v-model.trim="currObj.mac"
  235. class="c-form__item"
  236. maxlength="17"
  237. />
  238. </div>
  239. <div class="c-form__section">
  240. <span class="c-form__label required">地址:</span>
  241. <el-input
  242. v-model="currObj.remark"
  243. class="c-form__item"
  244. type="textarea"
  245. :rows="3"
  246. maxlength="100"
  247. show-word-limit
  248. />
  249. </div>
  250. </div>
  251. </confirm-dialog>
  252. <el-dialog
  253. :visible.sync="choosing"
  254. title="节目选择"
  255. custom-class="c-dialog hidden-footer"
  256. >
  257. <event-target-choose
  258. v-if="choosing"
  259. :ratio="ratio"
  260. @choose="onChoose"
  261. />
  262. </el-dialog>
  263. </wrapper>
  264. </template>
  265. <script>
  266. import {
  267. getDevices,
  268. addDevice,
  269. deleteDevice,
  270. updateDevice,
  271. getSubDevices,
  272. addSubDevice,
  273. activateDevice,
  274. deactivateDevice,
  275. getProducts
  276. } from '@/api/device'
  277. import { publish } from '@/api/publish'
  278. import {
  279. EventPriority,
  280. EventFreq,
  281. PublishType
  282. } from '@/constant'
  283. import { createListOptions } from '@/utils'
  284. import { toDateStr } from '@/utils/event'
  285. import mixin from './mixins'
  286. import EventTargetChoose from '@/components/EventTargetChoose'
  287. export default {
  288. name: 'DeviceList',
  289. components: {
  290. EventTargetChoose
  291. },
  292. mixins: [mixin],
  293. data () {
  294. return {
  295. proxy: {
  296. list: getDevices,
  297. add: addDevice,
  298. update: updateDevice,
  299. del: deleteDevice
  300. },
  301. options: createListOptions({
  302. productId: '',
  303. name: ''
  304. }),
  305. productOptions: [
  306. { value: '', label: '全部设备' }
  307. ],
  308. loaded: false,
  309. fetching: false,
  310. firstLoadSize: 999,
  311. isSub: false,
  312. device: null,
  313. choosing: false,
  314. ratio: null
  315. }
  316. },
  317. computed: {
  318. canEdit () {
  319. return this.accessSet.has(this.Access.MANAGE_DEVICES)
  320. },
  321. canEditDefaultProgram () {
  322. return this.canEdit || this.accessSet.has(this.Access.MANAGE_DEVICE)
  323. },
  324. operationColumnWidth () {
  325. return this.canEdit ? 340 : this.canEditDefaultProgram ? 260 : 120
  326. },
  327. type () {
  328. return this.isSub ? '备份设备' : '设备'
  329. },
  330. products () {
  331. return this.isSub
  332. ? [{ value: this.$master.productId, label: this.$master.productName }]
  333. : this.loaded
  334. ? this.productOptions.slice(1)
  335. : []
  336. },
  337. tableData () {
  338. const arr = []
  339. this.list.forEach(item => {
  340. arr.push(item)
  341. if (item.loaded && item.expand) {
  342. arr.push(...item.subs)
  343. }
  344. })
  345. return arr
  346. },
  347. currOptions () {
  348. const arr = []
  349. this.options.list.forEach(item => {
  350. arr.push(item)
  351. if (item.loaded && item.expand) {
  352. arr.push(...item.subs)
  353. }
  354. })
  355. return {
  356. ...this.options,
  357. list: arr
  358. }
  359. }
  360. },
  361. activated () {
  362. this.getList()
  363. },
  364. methods: {
  365. transform (data) {
  366. return data.map(item => {
  367. return {
  368. ...item,
  369. loaded: false,
  370. loading: false,
  371. expand: false,
  372. subs: [],
  373. isMaster: true
  374. }
  375. })
  376. },
  377. getDefaults () {
  378. return {
  379. name: '',
  380. productId: this.isSub && this.$master.productId || this.options.params.productId,
  381. serialNumber: '',
  382. mac: '',
  383. remark: ''
  384. }
  385. },
  386. onAddSubDevice (item) {
  387. this.isSub = true
  388. this.$master = item
  389. this.onAdd()
  390. },
  391. onSave (done) {
  392. if (this.check(this.currObj)) {
  393. if (this.isSub) {
  394. addSubDevice(this.$master, this.currObj).then(() => {
  395. done()
  396. this.reloadSubDevices(this.$master)
  397. })
  398. } else {
  399. this.save(done)
  400. }
  401. }
  402. },
  403. afterAdd () {
  404. const params = this.options.params
  405. if (params.name && !(new RegExp(params.name).test(this.currObj.name))) {
  406. params.name = ''
  407. }
  408. if (params.productId && params.productId !== this.currObj.productId) {
  409. params.productId = ''
  410. }
  411. },
  412. check (item) {
  413. if (!item.name) {
  414. this.$message({
  415. type: 'warning',
  416. message: '名称不能为空'
  417. })
  418. return false
  419. }
  420. if (!item.productId) {
  421. this.$message({
  422. type: 'warning',
  423. message: '请选择产品'
  424. })
  425. return false
  426. }
  427. if (!item.serialNumber) {
  428. this.$message({
  429. type: 'warning',
  430. message: '序列号不能为空'
  431. })
  432. return false
  433. }
  434. if (!item.mac) {
  435. this.$message({
  436. type: 'warning',
  437. message: 'MAC不能为空'
  438. })
  439. return false
  440. }
  441. if (!/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$/.test(item.mac)) {
  442. this.$message({
  443. type: 'warning',
  444. message: 'MAC格式不正确,例 ff:ff:ff:ff:ff:ff'
  445. })
  446. return false
  447. }
  448. item.mac = item.mac.toLowerCase()
  449. if (!item.remark) {
  450. this.$message({
  451. type: 'warning',
  452. message: '地址不能为空'
  453. })
  454. return false
  455. }
  456. return true
  457. },
  458. onEditDevice (item) {
  459. this.$router.push({
  460. name: 'device-detail',
  461. params: {
  462. id: item.id
  463. }
  464. })
  465. },
  466. onDelDevice (item) {
  467. if (item.isMaster) {
  468. this.onDel(item)
  469. } else {
  470. this.proxy.del(item).then(() => {
  471. this.reloadSubDevices(item.parent)
  472. })
  473. }
  474. },
  475. reloadSubDevices (item) {
  476. item.loaded = false
  477. item.children = []
  478. this.getSubDevices(item)
  479. },
  480. getSubDevices (item, pageSize = 999) {
  481. item.loading = true
  482. getSubDevices({
  483. id: item.id,
  484. pageNum: 1,
  485. pageSize
  486. }).then(({ data, totalCount }) => {
  487. if (totalCount > pageSize) {
  488. this.getSubDevices(item, totalCount)
  489. } else {
  490. item.loading = false
  491. item.loaded = true
  492. item.expand = true
  493. if (data.length === 0) {
  494. item.subs = [{ id: `${Math.random()}`, empty: true }]
  495. } else {
  496. item.subs = data.map(device => {
  497. return {
  498. ...device,
  499. isMaster: false,
  500. empty: false,
  501. parent: item
  502. }
  503. })
  504. }
  505. }
  506. }, () => {
  507. item.loading = false
  508. })
  509. },
  510. _getProducts () {
  511. this.fetching = true
  512. getProducts({ pageNum: 1, pageSize: this.firstLoadSize }).then(({ data, totalCount }) => {
  513. if (totalCount > this.firstLoadSize) {
  514. this.firstLoadSize = totalCount
  515. this._getProducts()
  516. } else {
  517. this.productOptions = this.productOptions.concat(data.map(({ id, name }) => {
  518. return { value: id, label: name }
  519. }))
  520. this.fetching = false
  521. this.loaded = true
  522. }
  523. }, () => {
  524. this.fetching = false
  525. })
  526. },
  527. getProducts (visible) {
  528. if (visible && !this.fetching && !this.loaded) {
  529. this._getProducts()
  530. }
  531. },
  532. onRowClick (row) {
  533. if (row.isMaster) {
  534. if (row.loaded) {
  535. row.expand = !row.expand
  536. } else if (!row.loading) {
  537. this.getSubDevices(row)
  538. }
  539. }
  540. },
  541. toggleActivate (item) {
  542. (item.activate ? deactivateDevice : activateDevice)(item).then(() => {
  543. if (item.isMaster) {
  544. this.getList()
  545. } else {
  546. this.reloadSubDevices(item.parent)
  547. }
  548. })
  549. },
  550. setDefaultProgram (device) {
  551. this.ratio = device.resolutionRatio
  552. this.$device = {
  553. id: device.id,
  554. name: device.name
  555. }
  556. this.choosing = true
  557. },
  558. onChoose (target) {
  559. this.$confirm(`确定将设备 ${this.$device.name} 的默认播放设置为 ${target.name}?`).then(() => {
  560. return publish(
  561. [this.$device.id],
  562. {
  563. type: PublishType.EVENT,
  564. detail: {
  565. priority: EventPriority.DEFAULT,
  566. freq: EventFreq.ONCE,
  567. start: `${toDateStr(new Date())} 00:00:00`,
  568. until: null,
  569. target
  570. }
  571. },
  572. {
  573. programCalendarName: target.name,
  574. resolutionRatio: this.ratio
  575. }
  576. )
  577. }).then(() => {
  578. this.choosing = false
  579. })
  580. }
  581. }
  582. }
  583. </script>
  584. <style lang="scss" scoped>
  585. .o-item {
  586. display: block;
  587. margin-right: 0;
  588. margin-bottom: 0;
  589. ::v-deep {
  590. label {
  591. width: 90px;
  592. color: #99a9bf;
  593. }
  594. }
  595. }
  596. .o-expand-icon {
  597. transition: transform 0.2s;
  598. &.expand {
  599. transform: rotate(90deg);
  600. }
  601. }
  602. .o-empty {
  603. color: $gray;
  604. font-size: 14px;
  605. }
  606. .o-mac::after {
  607. content: "例: ff:ff:ff:ff:ff:ff";
  608. }
  609. </style>