index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <confirm-dialog
  3. ref="dialog"
  4. append-to-body
  5. v-bind="$attrs"
  6. @confirm="onSave"
  7. >
  8. <template #default>
  9. <slot />
  10. <div
  11. v-for="(task, index) in tasks"
  12. :key="index"
  13. class="c-sibling-item--v c-grid-form u-align-self--center"
  14. >
  15. <div class="l-flex--row c-grid-form__row c-grid-form__option u-font-size--sm u-color--black u-bold">
  16. <div class="c-sibling-item">
  17. 区间段 {{ index + 1 }}
  18. </div>
  19. <i
  20. v-if="multi && tasks.length > 1"
  21. class="c-sibling-item el-icon-delete has-active"
  22. @click="onRemoveBlock(index)"
  23. />
  24. </div>
  25. <div class="c-grid-form__label">
  26. 生效日期
  27. </div>
  28. <el-date-picker
  29. v-model="task.date"
  30. class="u-width--lg"
  31. type="daterange"
  32. range-separator="至"
  33. start-placeholder="开始日期"
  34. end-placeholder="结束日期"
  35. value-format="yyyy-MM-dd"
  36. :picker-options="pickerOptions"
  37. :clearable="false"
  38. />
  39. <div class="c-grid-form__label c-grid-form__auto">
  40. 每周
  41. </div>
  42. <el-checkbox-group
  43. v-model="task.dayOfWeek"
  44. class="l-flex--row c-grid-form__auto"
  45. size="mini"
  46. fill="#1c5cb0"
  47. :min="1"
  48. >
  49. <el-checkbox-button
  50. v-for="week in weeks"
  51. :key="week.value"
  52. :label="week.value"
  53. >
  54. {{ week.label }}
  55. </el-checkbox-button>
  56. </el-checkbox-group>
  57. <div class="c-grid-form__label">
  58. 执行时间
  59. </div>
  60. <div style="display: flex; flex-wrap: wrap; gap: 6px;">
  61. <div
  62. v-for="(item, tindex) in task.executeTime"
  63. :key="tindex"
  64. class="l-flex--row inline"
  65. >
  66. <template v-if="strictly">
  67. <el-time-picker
  68. v-model="item.time"
  69. class="c-sibling-item u-width--lg"
  70. is-range
  71. range-separator="-"
  72. :start-placeholder="startPlaceholder"
  73. :end-placeholder="endPlaceholder"
  74. value-format="HH:mm:00"
  75. format="HH:mm"
  76. placeholder="选择时间范围"
  77. clearable
  78. />
  79. </template>
  80. <template v-else>
  81. <el-time-picker
  82. v-model="item.start"
  83. class="u-width--xs"
  84. :placeholder="startPlaceholder"
  85. value-format="HH:mm:00"
  86. format="HH:mm"
  87. clearable
  88. @change="onTimeChanged(item)"
  89. />
  90. <span class="has-padding--h">
  91. -
  92. </span>
  93. <el-time-picker
  94. v-model="item.end"
  95. class="c-sibling-item u-width--xs"
  96. :placeholder="endPlaceholder"
  97. value-format="HH:mm:00"
  98. format="HH:mm"
  99. clearable
  100. @change="onTimeChanged(item)"
  101. />
  102. </template>
  103. <el-tooltip
  104. v-if="item.warning"
  105. placement="top"
  106. effect="dark"
  107. :content="item.warning"
  108. >
  109. <i class="c-sibling-item el-icon-warning u-color--error dark u-font-size--md" />
  110. </el-tooltip>
  111. <i
  112. v-if="task.executeTime.length > 1"
  113. class="c-sibling-item el-icon-delete u-font-size--md has-active"
  114. @click="onRemoveTime(index, tindex)"
  115. />
  116. </div>
  117. <i
  118. class="c-grid-form__option l-flex--row el-icon-circle-plus-outline u-font-size--md has-active"
  119. style="margin-left: 6px;"
  120. @click="onAddTime(index)"
  121. />
  122. </div>
  123. </div>
  124. <div
  125. v-if="multi"
  126. class="c-sibling-item--v c-grid-form u-align-self--center"
  127. >
  128. <div class="l-flex--row c-grid-form__row">
  129. <div
  130. class="o-button"
  131. @click="onAddBlock"
  132. >
  133. <i class="o-button__icon el-icon-circle-plus-outline" />
  134. 新增区间段
  135. </div>
  136. </div>
  137. </div>
  138. </template>
  139. </confirm-dialog>
  140. </template>
  141. <script>
  142. import { mapGetters } from 'vuex'
  143. import {
  144. FOREVER,
  145. ONE_DAY
  146. } from '@/constant.js'
  147. import {
  148. parseTime,
  149. getCronWeekOptions
  150. } from '@/utils'
  151. import { toDate } from '@/utils/event.js'
  152. export default {
  153. name: 'TaskDialog',
  154. props: {
  155. multi: {
  156. type: Boolean,
  157. default: true
  158. },
  159. startPlaceholder: {
  160. type: String,
  161. default: '开启时间'
  162. },
  163. endPlaceholder: {
  164. type: String,
  165. default: '关闭时间'
  166. },
  167. strictly: {
  168. type: [Boolean, String],
  169. default: false
  170. }
  171. },
  172. data () {
  173. return {
  174. weeks: getCronWeekOptions(),
  175. pickerOptions: {
  176. disabledDate: this.isDisableDate,
  177. shortcuts: [
  178. {
  179. text: '本月',
  180. onClick (picker) {
  181. const end = new Date()
  182. const start = new Date()
  183. end.setDate(1)
  184. end.setMonth(end.getMonth() + 1)
  185. end.setDate(0)
  186. picker.$emit('pick', [start, end])
  187. }
  188. },
  189. {
  190. text: '今年',
  191. onClick (picker) {
  192. const start = new Date().getFullYear()
  193. const startYear = new Date()
  194. const endYear = new Date(start, 11, 31)
  195. picker.$emit('pick', [startYear, endYear])
  196. }
  197. },
  198. {
  199. text: '永久生效',
  200. onClick (picker) {
  201. const start = new Date()
  202. const end = new Date(FOREVER)
  203. picker.$emit('pick', [start, end])
  204. }
  205. }
  206. ]
  207. },
  208. tasks: []
  209. }
  210. },
  211. computed: {
  212. ...mapGetters(['account'])
  213. },
  214. methods: {
  215. isDisableDate (date) {
  216. return date <= Date.now() - ONE_DAY
  217. },
  218. isExpired (endDate) {
  219. return endDate !== FOREVER && toDate(`${endDate} 00:00:00`) <= Date.now() - ONE_DAY
  220. },
  221. show ({ startDate, endDate, dayOfWeek, executeTime } = {}) {
  222. const today = parseTime(new Date(), '{y}-{m}-{d}')
  223. this.tasks = [{
  224. date: startDate && endDate ? [startDate, endDate] : [today, today],
  225. dayOfWeek: dayOfWeek && dayOfWeek !== '?' ? dayOfWeek.split(',') : this.weeks.map(({ value }) => value),
  226. executeTime: executeTime
  227. ? executeTime.map(this.createTime)
  228. : [this.createTime()],
  229. flag: Date.now()
  230. }]
  231. this.$refs.dialog.show()
  232. },
  233. createTime (data) {
  234. const start = data?.start?.replace(/\d{2}$/, '00') || ''
  235. const end = data?.end?.replace(/\d{2}$/, '00') || ''
  236. return this.strictly
  237. ? { time: start && end ? [start, end] : null, warning: '' }
  238. : { start, end, warning: '' }
  239. },
  240. onAddBlock () {
  241. const today = parseTime(new Date(), '{y}-{m}-{d}')
  242. this.tasks.push({
  243. flag: Date.now(),
  244. date: [today, today],
  245. dayOfWeek: this.weeks.map(({ value }) => value),
  246. executeTime: [this.createTime()]
  247. })
  248. },
  249. onRemoveBlock (index) {
  250. this.tasks.splice(index, 1)
  251. },
  252. onAddTime (index) {
  253. this.tasks[index].executeTime.push(this.createTime())
  254. },
  255. onRemoveTime (index, tindex) {
  256. this.tasks[index].executeTime.splice(tindex, 1)
  257. },
  258. onTimeChanged (executeTime) {
  259. const { start, end } = executeTime
  260. if (start && end && start >= end) {
  261. executeTime.warning = '开启时间必须先于关闭时间'
  262. } else {
  263. executeTime.warning = ''
  264. }
  265. },
  266. hasTime (data) {
  267. return this.strictly
  268. ? !!data.time
  269. : !!data.start || !!data.end
  270. },
  271. transformTime (data) {
  272. return this.strictly
  273. ? `${data.time[0]}_${data.time[1]}`
  274. : `${data.start || ''}_${data.end || ''}`
  275. },
  276. onSave (done) {
  277. if (this.tasks.some(({ executeTime }) => executeTime.some(({ warning }) => !!warning))) {
  278. this.$message({
  279. type: 'warning',
  280. message: '请先完成异常数据的修改'
  281. })
  282. return
  283. }
  284. const tasks = this.tasks.filter(({ date, executeTime }) => !this.isExpired(date[1]) && executeTime.some(this.hasTime))
  285. if (!tasks.length) {
  286. this.$message({
  287. type: 'warning',
  288. message: '请至少添加一个有效执行时间'
  289. })
  290. return
  291. }
  292. this.$emit('confirm', {
  293. value: tasks.sort((a, b) => b.flag - a.flag).map(({ flag, date, dayOfWeek, executeTime }) => {
  294. return {
  295. flag: `${this.account}_${flag}`,
  296. startDate: date[0],
  297. endDate: date[1],
  298. dayOfWeek: dayOfWeek.length === 7 ? '?' : dayOfWeek.sort().join(','),
  299. executeTime: [...new Set(executeTime.filter(this.hasTime).map(this.transformTime))].sort().map(time => {
  300. const timeArr = time.split('_')
  301. return {
  302. start: timeArr[0] || '',
  303. end: timeArr[1] || ''
  304. }
  305. })
  306. }
  307. }),
  308. done
  309. })
  310. }
  311. }
  312. }
  313. </script>