index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <schedule-wrapper
  3. class="c-schedule-swiper"
  4. :name="name"
  5. :hide-header="hideHeader"
  6. :editable="editable"
  7. :dirty="dirty"
  8. @add="onAdd"
  9. @submit="submit"
  10. @save="onSave"
  11. >
  12. <el-empty
  13. v-if="isEmpty"
  14. description="暂无节目"
  15. />
  16. <template v-if="editable">
  17. <draggable
  18. v-model="scheduleOptions.events"
  19. class="l-flex__auto c-schedule-swiper__list"
  20. :class="{ disabled }"
  21. handle=".mover"
  22. animation="300"
  23. :disabled="disabled"
  24. >
  25. <div
  26. v-for="(program, index) in events"
  27. :key="program.key"
  28. class="l-flex--row c-schedule-swiper__item"
  29. >
  30. <div class="l-flex__auto l-flex--row mover">
  31. <i class="l-flex__none c-schedule-swiper__mover el-icon-sort" />
  32. <div
  33. class="l-flex__auto c-schedule-swiper__program u-ellipsis u-pointer"
  34. @click="onView(program)"
  35. >
  36. {{ program.name }}
  37. </div>
  38. </div>
  39. <span class="l-flex__none">持续时间(秒):</span>
  40. <el-input-number
  41. v-model="program.duration"
  42. class="l-flex__none"
  43. controls-position="right"
  44. :min="1"
  45. :max="2592000"
  46. :step="1"
  47. step-strictly
  48. />
  49. <i
  50. class="l-flex__none c-schedule-swiper__del o-icon--active el-icon-delete u-pointer"
  51. @click="onDel(program, index)"
  52. />
  53. </div>
  54. </draggable>
  55. <table-dialog
  56. ref="programDialog"
  57. title="节目选择"
  58. :schema="programSchema"
  59. @choosen="onChoosen"
  60. />
  61. </template>
  62. <template v-else>
  63. <div class="c-schedule-swiper__list">
  64. <div
  65. v-for="program in events"
  66. :key="program.key"
  67. class="l-flex--row c-schedule-swiper__item readonly u-pointer"
  68. @click="onView(program)"
  69. >
  70. <div class="c-schedule-swiper__program u-ellipsis">
  71. {{ program.name }}
  72. </div>
  73. <span class="l-flex__none">持续{{ program.duration }}</span>
  74. </div>
  75. </div>
  76. </template>
  77. </schedule-wrapper>
  78. </template>
  79. <script>
  80. import { getPrograms } from '@/api/program'
  81. import { State } from '@/constant'
  82. import scheduleMixin from '../mixins/schedule'
  83. import Draggable from 'vuedraggable'
  84. import ScheduleWrapper from '../components/ScheduleWrapper'
  85. export default {
  86. name: 'ScheduleSwiper',
  87. components: {
  88. Draggable,
  89. ScheduleWrapper
  90. },
  91. mixins: [scheduleMixin],
  92. data () {
  93. return {
  94. record: null,
  95. programSchema: {
  96. condition: { status: State.RESOLVED, resolutionRatio: this.ratio, name: '' },
  97. list: getPrograms,
  98. filters: [
  99. { key: 'name', type: 'search', placeholder: '节目名称' }
  100. ],
  101. cols: [
  102. { prop: 'name', label: '节目名称' },
  103. {
  104. type: 'invoke', render: [
  105. { label: '查看', on: this.onView }
  106. ]
  107. }
  108. ]
  109. }
  110. }
  111. },
  112. computed: {
  113. disabled () {
  114. return this.events.length < 2
  115. },
  116. dirty () {
  117. if (this.editable && this.record) {
  118. const { events, count } = this.record
  119. const currEvents = this.events
  120. if (count !== currEvents.length) {
  121. return true
  122. }
  123. for (let i = 0; i < events.length; i++) {
  124. if (events[i].id !== currEvents[i].id || events[i].duration !== currEvents[i].duration) {
  125. return true
  126. }
  127. }
  128. }
  129. return false
  130. }
  131. },
  132. methods: {
  133. transformEvents (events) {
  134. return events.sort((a, b) => a.index - b.index).map(this.transformEvent)
  135. },
  136. transformEvent (event) {
  137. const { programId, programName, programUrl, spend } = event
  138. return {
  139. key: Math.random().toString(16).slice(2),
  140. id: programId,
  141. name: programName,
  142. url: programUrl,
  143. duration: this.transformDuration(spend)
  144. }
  145. },
  146. getEvents () {
  147. return this.events.map(({ id, name, url, duration }, index) => {
  148. return {
  149. index,
  150. freq: 'SECONDLY',
  151. spend: duration,
  152. programId: id,
  153. programName: name,
  154. programUrl: url
  155. }
  156. })
  157. },
  158. transformDuration (duration) {
  159. if (this.editable) {
  160. return duration
  161. }
  162. const seconds = duration % 60
  163. let minutes = duration / 60 | 0
  164. let s = ''
  165. if (minutes > 60) {
  166. let hours = minutes / 60 | 0
  167. s += `${hours / 24 | 0}天`
  168. hours %= 24
  169. if (hours) {
  170. s += `${hours}小时`
  171. }
  172. minutes %= 60
  173. }
  174. if (minutes) {
  175. s += `${minutes}分`
  176. }
  177. if (seconds) {
  178. s += `${seconds}秒`
  179. }
  180. return s
  181. },
  182. init () {
  183. this.record = {
  184. events: this.events.map(({ id, duration }) => {
  185. return { id, duration }
  186. }),
  187. count: this.events.length
  188. }
  189. },
  190. onSave () {
  191. this.save().then(this.init)
  192. },
  193. onAdd () {
  194. this.$refs.programDialog.show()
  195. },
  196. onChoosen ({ value: { id, name, duration, buckets, itemConfigName }, done }) {
  197. done()
  198. this.events.push({
  199. key: Math.random().toString(16).slice(2),
  200. id, name,
  201. url: `${buckets}/${itemConfigName}`,
  202. duration: duration || 60
  203. })
  204. },
  205. onView ({ id }) {
  206. window.open(this.$router.resolve({
  207. name: 'program',
  208. params: { id }
  209. }).href, '_blank')
  210. },
  211. onDel ({ name }, index) {
  212. this.$confirm(
  213. `确定移除节目 ${name}?`,
  214. { type: 'warning' }
  215. ).then(() => {
  216. this.events.splice(index, 1)
  217. })
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss" scoped>
  223. .c-schedule-swiper {
  224. color: $black;
  225. &__tip {
  226. line-height: 1;
  227. }
  228. &__list {
  229. display: flex;
  230. flex-direction: column;
  231. font-size: 14px;
  232. line-height: 1;
  233. overflow-y: auto;
  234. }
  235. &__list.disabled &__mover {
  236. visibility: hidden;
  237. }
  238. &__item {
  239. border: 1px solid $gray;
  240. border-radius: 6px;
  241. &.sortable-chosen {
  242. border-color: #c6e2ff;
  243. background-color: $blue--light;
  244. }
  245. & + & {
  246. margin-top: 10px;
  247. }
  248. &.readonly {
  249. padding-right: 10px;
  250. &:hover {
  251. color: #409eff;
  252. border-color: #c6e2ff;
  253. background-color: $blue--light;
  254. }
  255. .c-schedule-swiper__program {
  256. font-size: 14px;
  257. }
  258. }
  259. }
  260. &__mover {
  261. padding: 10px 16px;
  262. font-size: 18px;
  263. cursor: move;
  264. }
  265. &__program {
  266. padding: 16px 10px;
  267. font-size: 16px;
  268. }
  269. &__del {
  270. padding: 10px 16px;
  271. margin-left: 6px;
  272. font-size: 18px;
  273. }
  274. }
  275. </style>