index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <template>
  2. <wrapper
  3. auto
  4. margin
  5. padding
  6. background
  7. >
  8. <div class="l-flex__none l-flex c-device-detail has-bottom-padding">
  9. <div
  10. class="l-flex__none c-device-detail__screen o-program"
  11. :class="{ 'u-pointer': programProxy }"
  12. :style="programStyle"
  13. @click="onView"
  14. />
  15. <div class="l-flex__auto l-flex--col">
  16. <div class="l-flex__none c-device-detail__name u-ellipsis">{{ deivceName }}</div>
  17. <template v-if="programProxy">
  18. <div class="l-flex__none c-device-detail__program u-ellipsis">
  19. <span
  20. class="u-pointer"
  21. @click="onView"
  22. >{{ programName }}</span>
  23. </div>
  24. <div class="l-flex__none c-device-detail__time">{{ programTime }}</div>
  25. </template>
  26. </div>
  27. </div>
  28. <div
  29. v-loading="deviceOptions.loading"
  30. class="l-flex__fill l-flex--col c-timeline"
  31. >
  32. <div class="l-flex__none l-flex--row has-bottom-padding">
  33. <div class="l-flex__auto c-sibling-item" />
  34. <schema-select
  35. v-model="deviceOptions.params.productId"
  36. class="c-sibling-item"
  37. placeholder="全部产品"
  38. :schema="productSelectSchema"
  39. />
  40. <el-date-picker
  41. v-model="current"
  42. class="l-flex__none c-sibling-item u-pointer"
  43. type="date"
  44. placeholder="选择日期"
  45. :picker-options="pickerOptions"
  46. :editable="false"
  47. :clearable="false"
  48. @change="onTimeChange"
  49. />
  50. <search-input
  51. v-model.trim="deviceOptions.params.name"
  52. class="l-flex__none c-sibling-item"
  53. placeholder="设备名称"
  54. @search="search"
  55. />
  56. <button
  57. class="l-flex__none c-sibling-item o-button"
  58. @click="search"
  59. >
  60. 搜索
  61. </button>
  62. </div>
  63. <div class="l-flex__none l-flex c-timeline__row header">
  64. <div class="l-flex__none c-timeline__left">
  65. <span class="o-priority priority3">高</span>
  66. <span class="o-priority priority2">中</span>
  67. <span class="o-priority priority1">低</span>
  68. </div>
  69. <div class="l-flex__auto l-flex--row c-timeline__right">
  70. <i
  71. class="l-flex__none c-timeline__arrow left el-icon-arrow-left u-pointer"
  72. :class="{ display: canPrevious }"
  73. @click="offsetTime(-1)"
  74. />
  75. <div class="l-flex__auto l-flex--row c-timeline__time">
  76. <div
  77. v-for="time in times"
  78. :key="time"
  79. class="l-flex__none"
  80. >
  81. {{ time }}
  82. </div>
  83. </div>
  84. <i
  85. class="l-flex__none c-timeline__arrow right display el-icon-arrow-right u-pointer"
  86. @click="offsetTime(1)"
  87. />
  88. </div>
  89. </div>
  90. <div class="l-flex--col c-timeline__main u-relative">
  91. <div class="l-flex__auto u-overflow-y--auto">
  92. <div
  93. v-for="item in deviceOptions.list"
  94. :key="item.id"
  95. class="l-flex c-timeline__row"
  96. :class="{ selected: item.id === deviceId }"
  97. @click="chooseProgramProxy(item)"
  98. >
  99. <div class="l-flex__none c-timeline__left u-relative u-pointer">
  100. <div class="u-ellipsis">{{ item.name }}</div>
  101. </div>
  102. <div class="l-flex__auto l-flex--col c-timeline__right">
  103. <div
  104. v-if="item.options.loading"
  105. class="l-flex--row c-timeline__programs"
  106. >
  107. <i class="el-icon-loading has-padding--h" />加载中...
  108. </div>
  109. <div
  110. v-else-if="item.options.error"
  111. class="l-flex--row c-timeline__programs has-padding--h"
  112. >
  113. <el-link
  114. type="warning"
  115. @click.stop="getTimeline(item)"
  116. >
  117. 获取失败,点击重试
  118. </el-link>
  119. </div>
  120. <template v-else-if="item.options.list.length">
  121. <div
  122. v-for="(programs, index) in item.options.list"
  123. :key="index"
  124. class="c-timeline__programs l-flex--row u-relative"
  125. >
  126. <div
  127. v-for="program in programs"
  128. :key="program.key"
  129. class="l-flex__none l-flex--row c-event-program u-pointer"
  130. :class="[{ 'selected': program.event.selected }, `priority${program.event.priority}`]"
  131. :style="program.style"
  132. @click.stop="chooseProgramProxy(item, program)"
  133. >
  134. <i
  135. class="l-flex__none c-event-program__img o-program"
  136. :style="program.event.style"
  137. />
  138. <div class="l-flex__auto">
  139. <auto-text
  140. class="c-event-program__time"
  141. :text="program.time"
  142. :tag="program.style.width"
  143. />
  144. <auto-text
  145. class="c-event-program__name"
  146. :text="program.event.target.name"
  147. :tag="program.style.width"
  148. />
  149. </div>
  150. </div>
  151. </div>
  152. </template>
  153. <div
  154. v-else
  155. class="l-flex--row c-timeline__programs has-padding--h"
  156. >
  157. 当前时段暂无节目
  158. </div>
  159. </div>
  160. </div>
  161. </div>
  162. <div
  163. v-show="style"
  164. class="c-timeline__line"
  165. >
  166. <div
  167. class="c-timeline__mask"
  168. :style="style"
  169. />
  170. </div>
  171. </div>
  172. <status-wrapper
  173. v-if="isAbnormal"
  174. :error="deviceOptions.error"
  175. @click="getDevices"
  176. />
  177. <pagination
  178. :total="deviceOptions.totalCount"
  179. :page.sync="deviceOptions.params.pageNum"
  180. :limit.sync="deviceOptions.params.pageSize"
  181. @pagination="getDevices"
  182. />
  183. </div>
  184. <schedule-dialog ref="scheduleDialog" />
  185. </wrapper>
  186. </template>
  187. <script>
  188. import {
  189. getProducts,
  190. getDevices,
  191. getTimeline
  192. } from '@/api/device'
  193. import {
  194. EventFreq,
  195. ScheduleType
  196. } from '@/constant'
  197. import { createListOptions } from '@/utils'
  198. import {
  199. toDate,
  200. toDateStr,
  201. toTimeStr,
  202. toZeroPoint,
  203. getNearestHitDate,
  204. getStartDate,
  205. getFinishDate,
  206. pickMin,
  207. pickMax
  208. } from '@/utils/event'
  209. import { EventCache } from '@/utils/cache'
  210. export default {
  211. name: 'ScheduleTimeline',
  212. data () {
  213. return {
  214. productSelectSchema: {
  215. remote: getProducts,
  216. pagination: true,
  217. value: 'id',
  218. label: 'name'
  219. },
  220. deviceOptions: createListOptions({
  221. productId: '',
  222. name: '',
  223. activate: 2,
  224. pageSize: 5
  225. }),
  226. style: null,
  227. canPrevious: false,
  228. startHour: 0,
  229. times: [],
  230. current: null,
  231. device: null,
  232. programProxy: null
  233. }
  234. },
  235. computed: {
  236. deviceId () {
  237. return this.device?.id
  238. },
  239. deivceName () {
  240. return this.device?.name
  241. },
  242. programName () {
  243. return this.programProxy?.event.target.name
  244. },
  245. programTime () {
  246. return this.programProxy?.event.time
  247. },
  248. programStyle () {
  249. return this.programProxy?.event.style
  250. },
  251. isAbnormal () {
  252. const deviceOptions = this.deviceOptions
  253. return deviceOptions.error || !deviceOptions.loading && deviceOptions.totalCount === 0
  254. },
  255. pickerOptions () {
  256. return {
  257. disabledDate: this.isDisableDate
  258. }
  259. }
  260. },
  261. created () {
  262. this.initTimes(new Date())
  263. this.getDevices()
  264. this.$timer = setInterval(this.calcLine, 1000)
  265. },
  266. beforeDestroy () {
  267. clearInterval(this.$timer)
  268. },
  269. methods: {
  270. getDevices () {
  271. this.device = null
  272. if (this.programProxy) {
  273. this.programProxy.event.selected = false
  274. }
  275. this.programProxy = null
  276. const options = this.deviceOptions
  277. options.error = false
  278. options.loading = true
  279. getDevices(options.params).then(({ data, totalCount }) => {
  280. options.list = data.map(this.transform)
  281. options.totalCount = totalCount
  282. options.list.forEach(this.getTimeline)
  283. }, () => {
  284. options.error = true
  285. options.list = []
  286. }).finally(() => {
  287. options.loading = false
  288. })
  289. },
  290. search () {
  291. const options = this.deviceOptions
  292. options.list = []
  293. options.totalCount = 0
  294. options.params.pageNum = 1
  295. this.getDevices()
  296. },
  297. transform (device) {
  298. const { id, name } = device
  299. return {
  300. id, name,
  301. options: {
  302. loading: true,
  303. error: false,
  304. events: [],
  305. list: []
  306. }
  307. }
  308. },
  309. getTimeline (device) {
  310. const options = device.options
  311. options.error = false
  312. options.loading = true
  313. getTimeline(device.id, { custom: true }).finally(() => {
  314. options.loading = false
  315. }).then(
  316. events => {
  317. const now = Date.now()
  318. options.events = this.transformEvents(events.map(this.transformEvent).filter(({ until }) => !until || now < toDate(until)))
  319. this.calcEvents(options)
  320. },
  321. () => {
  322. options.error = true
  323. options.list = []
  324. }
  325. )
  326. },
  327. transformEvent (event) {
  328. return {
  329. ...event,
  330. time: this.getTime(event),
  331. startDateTime: toDate(event.start),
  332. endDateTime: toDate(event.until),
  333. style: null,
  334. selected: false,
  335. img () {
  336. EventCache.getImage(this.target.type, this.target.id).then(img => {
  337. if (img) {
  338. this.style = { backgroundImage: `url("${img}")` }
  339. }
  340. })
  341. }
  342. }
  343. },
  344. transformEvents (events) {
  345. const map = {}
  346. for (let i = 0; i < events.length; i++) {
  347. const event = events[i]
  348. if (!map[event.priority]) {
  349. map[event.priority] = []
  350. }
  351. map[event.priority].push(event)
  352. }
  353. return Object.keys(map)
  354. .sort((a, b) => a > b ? -1 : 1)
  355. .map(key => map[key].sort((a, b) => toDate(a.start) - toDate(b.start)))
  356. },
  357. getTime (event) {
  358. const { freq, start, until, byDay, startTime, endTime } = event
  359. switch (freq) {
  360. case EventFreq.WEEKLY:
  361. return `自${start.split(' ')[0]}开始${until ? `至${until.split(' ')[0]}前` : ''} 每周${byDay.split(',').map(val => ['日', '一', '二', '三', '四', '五', '六'][val]).join('、')} ${startTime} - ${endTime}`
  362. default:
  363. return until ? `${start} - ${until}` : `自${start}开始`
  364. }
  365. },
  366. isDisableDate (date) {
  367. const now = new Date()
  368. const min = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  369. const max = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 30)
  370. return date < min || date > max
  371. },
  372. initTimes (date) {
  373. const maxDate = pickMax(toDate(date), new Date())
  374. this.current = toZeroPoint(maxDate)
  375. this.startHour = Math.min(20, maxDate.getHours())
  376. this.refreshTimes(this.startHour)
  377. },
  378. refreshTimes (start) {
  379. const times = []
  380. for (let i = 0; i < 5; i++) {
  381. times.push(`${(start + i).toString().padStart(2, '0')}:00`)
  382. }
  383. this.times = times
  384. this.$startDateTime = toDate(this.current.getTime() + start * 3600000)
  385. this.$endDateTime = toDate(this.current.getTime() + (start + 4) * 3600000)
  386. this.calcLine()
  387. this.refreshTimeline()
  388. },
  389. calcLine () {
  390. const now = Date.now()
  391. this.canPrevious = now < this.$startDateTime
  392. if (now < this.$startDateTime || now >= this.$endDateTime) {
  393. this.style = null
  394. } else {
  395. this.style = {
  396. left: `${Math.min(100, (now - this.$startDateTime) / 144000)}%`
  397. }
  398. }
  399. },
  400. refreshTimeline () {
  401. this.deviceOptions.list.forEach(device => {
  402. this.calcEvents(device.options)
  403. })
  404. },
  405. offsetTime (offset) {
  406. const next = this.startHour + offset
  407. if (offset < 0) {
  408. const timestamp = this.current.getTime() + next * 3600000
  409. const now = new Date()
  410. now.setMinutes(0)
  411. now.setSeconds(0)
  412. now.setMilliseconds(0)
  413. if (timestamp >= now) {
  414. if (next < 0) {
  415. this.initTimes(timestamp)
  416. } else {
  417. this.refreshTimes(this.startHour = next)
  418. }
  419. }
  420. } else if (next > 20) {
  421. this.initTimes(this.$endDateTime)
  422. } else {
  423. this.refreshTimes(this.startHour = next)
  424. }
  425. },
  426. onTimeChange (val) {
  427. this.initTimes(val)
  428. },
  429. calcSamePriorityEvents (events) {
  430. const total = 144000
  431. const arr = []
  432. for (let i = 0; i < events.length; i++) {
  433. const event = events[i]
  434. const { startDateTime, endDateTime } = event
  435. if (endDateTime && endDateTime <= this.$startDateTime || startDateTime >= this.$endDateTime) {
  436. continue
  437. }
  438. const hit = getNearestHitDate(event, this.$startDateTime, this.$endDateTime)
  439. if (hit) {
  440. const startDate = getStartDate(event, hit)
  441. const endDate = getFinishDate(event, hit)
  442. arr.push({
  443. key: event.target.id,
  444. event,
  445. time: `${toDateStr(startDate)} ${toTimeStr(startDate)} - ${toDateStr(endDate)} ${toTimeStr(endDate)}`,
  446. style: {
  447. left: `${(hit - this.$startDateTime) / total}%`,
  448. width: `${(pickMin(this.$endDateTime, getFinishDate(event, hit)) - hit) / total}%`,
  449. zIndex: event.priority
  450. }
  451. })
  452. event.img?.()
  453. if (endDate >= this.$endDateTime) {
  454. break
  455. }
  456. }
  457. }
  458. return arr
  459. },
  460. calcEvents (options) {
  461. if (options.loading || options.error) {
  462. return
  463. }
  464. options.list = options.events.map(this.calcSamePriorityEvents).filter(events => events.length)
  465. },
  466. chooseProgramProxy (device, programProxy) {
  467. this.device = device
  468. if (this.programProxy) {
  469. this.programProxy.event.selected = false
  470. }
  471. if (programProxy) {
  472. programProxy.event.selected = true
  473. }
  474. this.programProxy = programProxy
  475. },
  476. onView () {
  477. if (this.programProxy) {
  478. switch (this.programProxy.event.target.type) {
  479. case ScheduleType.CALENDAR:
  480. window.open(this.$router.resolve({
  481. name: 'program',
  482. params: { id: this.programProxy.event.target.id }
  483. }).href, '_blank')
  484. break
  485. case ScheduleType.RECUR:
  486. this.$refs.scheduleDialog.show(this.programProxy.event.target.id)
  487. break
  488. default:
  489. break
  490. }
  491. }
  492. }
  493. }
  494. }
  495. </script>
  496. <style lang="scss" scoped>
  497. .c-device-detail {
  498. height: 200px;
  499. &__screen {
  500. width: 354px;
  501. margin-right: 24px;
  502. border-radius: $radius--mini;
  503. }
  504. &__name {
  505. color: $black;
  506. font-size: 20px;
  507. font-weight: bold;
  508. line-height: 1;
  509. }
  510. &__program {
  511. margin-top: 20px;
  512. color: $blue;
  513. font-size: 20px;
  514. font-weight: bold;
  515. }
  516. &__time {
  517. margin-top: $spacing;
  518. color: $info--dark;
  519. font-size: 20px;
  520. }
  521. }
  522. .c-timeline {
  523. min-height: 400px;
  524. &__main {
  525. flex: 0 1 auto;
  526. min-height: 0;
  527. border: 1px solid $border;
  528. }
  529. &__row {
  530. & + & {
  531. border-top: 1px solid $border;
  532. }
  533. }
  534. &__row.header {
  535. .c-timeline__left {
  536. border-right: none;
  537. }
  538. }
  539. &__row.header &__right {
  540. color: $black;
  541. user-select: none;
  542. background-color: transparent;
  543. }
  544. &__row.selected &__left {
  545. color: #fff;
  546. background-color: #9fbfe8;
  547. }
  548. &__row.selected &__left::after {
  549. content: "";
  550. position: absolute;
  551. top: 0;
  552. left: 0;
  553. bottom: 0;
  554. width: 4px;
  555. background-color: $blue;
  556. }
  557. &__left {
  558. display: inline-flex;
  559. align-items: center;
  560. width: 210px;
  561. padding: 0 10px 0 20px;
  562. margin-right: 1px;
  563. color: $black;
  564. font-size: 16px;
  565. background-color: #fafbfc;
  566. border-right: 1px solid $border;
  567. }
  568. &__right {
  569. position: relative;
  570. color: $info--dark;
  571. font-size: 14px;
  572. line-height: 1;
  573. overflow: hidden;
  574. }
  575. &__programs {
  576. box-sizing: content-box;
  577. height: 48px;
  578. & + & {
  579. border-top: 1px dashed $border;
  580. }
  581. }
  582. &__arrow {
  583. visibility: hidden;
  584. position: absolute;
  585. top: 50%;
  586. padding: 6px;
  587. color: #fff;
  588. font-size: 12px;
  589. border-radius: $radius--mini;
  590. background-color: $blue;
  591. transform: translateY(-50%);
  592. &.left {
  593. left: 0;
  594. }
  595. &.right {
  596. right: 0;
  597. }
  598. &.display {
  599. visibility: visible;
  600. }
  601. }
  602. &__time {
  603. justify-content: space-around;
  604. padding: 10px 0;
  605. color: $black;
  606. font-size: 14px;
  607. }
  608. &__line {
  609. position: absolute;
  610. top: -12px;
  611. left: 210px;
  612. right: 0;
  613. bottom: 0;
  614. pointer-events: none;
  615. }
  616. &__mask {
  617. position: absolute;
  618. top: 0;
  619. left: 0;
  620. bottom: 0;
  621. color: #ff0000;
  622. border-right: 1px solid currentColor;
  623. z-index: 999;
  624. &::after {
  625. content: "";
  626. position: absolute;
  627. top: 0;
  628. right: -5px;
  629. width: 9px;
  630. height: 9px;
  631. border-radius: 50%;
  632. background-color: currentColor;
  633. }
  634. }
  635. }
  636. .c-event-program {
  637. position: absolute;
  638. top: 0;
  639. bottom: 0;
  640. font-size: 12px;
  641. overflow: hidden;
  642. &:hover {
  643. color: #fff;
  644. background-color: rgba($blue, 0.4);
  645. }
  646. &.selected {
  647. color: #fff;
  648. background-color: $blue;
  649. z-index: 99;
  650. }
  651. &__img {
  652. width: 84px;
  653. height: 48px;
  654. margin-right: 8px;
  655. border-radius: $radius--mini;
  656. }
  657. &__name {
  658. margin-top: 10px;
  659. }
  660. }
  661. .o-priority {
  662. display: inline-block;
  663. padding: 4px;
  664. font-size: 12px;
  665. border-radius: $radius--mini;
  666. & + & {
  667. margin-left: 4px;
  668. }
  669. }
  670. .priority1 {
  671. color: #8e929c;
  672. background-color: #edf0f6;
  673. }
  674. .priority2 {
  675. color: #7642fd;
  676. background-color: #eae2fe;
  677. }
  678. .priority3 {
  679. color: #ff2222;
  680. background-color: #ffecec;
  681. }
  682. .o-program {
  683. background: url("~@/assets/program_bg.png") 0 0 / 100% 100% no-repeat;
  684. }
  685. </style>