index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div>
  3. <el-tabs type="border-card">
  4. <el-tab-pane label="秒" v-if="shouldHide('second')">
  5. <CrontabSecond
  6. @update="updateCrontabValue"
  7. :check="checkNumber"
  8. :cron="crontabValueObj"
  9. ref="cronsecond"
  10. />
  11. </el-tab-pane>
  12. <el-tab-pane label="分钟" v-if="shouldHide('min')">
  13. <CrontabMin
  14. @update="updateCrontabValue"
  15. :check="checkNumber"
  16. :cron="crontabValueObj"
  17. ref="cronmin"
  18. />
  19. </el-tab-pane>
  20. <el-tab-pane label="小时" v-if="shouldHide('hour')">
  21. <CrontabHour
  22. @update="updateCrontabValue"
  23. :check="checkNumber"
  24. :cron="crontabValueObj"
  25. ref="cronhour"
  26. />
  27. </el-tab-pane>
  28. <el-tab-pane label="日" v-if="shouldHide('day')">
  29. <CrontabDay
  30. @update="updateCrontabValue"
  31. :check="checkNumber"
  32. :cron="crontabValueObj"
  33. ref="cronday"
  34. />
  35. </el-tab-pane>
  36. <el-tab-pane label="月" v-if="shouldHide('month')">
  37. <CrontabMonth
  38. @update="updateCrontabValue"
  39. :check="checkNumber"
  40. :cron="crontabValueObj"
  41. ref="cronmonth"
  42. />
  43. </el-tab-pane>
  44. <el-tab-pane label="周" v-if="shouldHide('week')">
  45. <CrontabWeek
  46. @update="updateCrontabValue"
  47. :check="checkNumber"
  48. :cron="crontabValueObj"
  49. ref="cronweek"
  50. />
  51. </el-tab-pane>
  52. <el-tab-pane label="年" v-if="shouldHide('year')">
  53. <CrontabYear
  54. @update="updateCrontabValue"
  55. :check="checkNumber"
  56. :cron="crontabValueObj"
  57. ref="cronyear"
  58. />
  59. </el-tab-pane>
  60. </el-tabs>
  61. <div class="popup-main">
  62. <div class="popup-result">
  63. <p class="title">时间表达式</p>
  64. <table>
  65. <thead>
  66. <th v-for="item of tabTitles" :key="item">{{item}}</th>
  67. <th>Cron 表达式</th>
  68. </thead>
  69. <tbody>
  70. <td>
  71. <span v-if="crontabValueObj.second.length < 10">{{crontabValueObj.second}}</span>
  72. <el-tooltip v-else :content="crontabValueObj.second" placement="top"><span>{{crontabValueObj.second}}</span></el-tooltip>
  73. </td>
  74. <td>
  75. <span v-if="crontabValueObj.min.length < 10">{{crontabValueObj.min}}</span>
  76. <el-tooltip v-else :content="crontabValueObj.min" placement="top"><span>{{crontabValueObj.min}}</span></el-tooltip>
  77. </td>
  78. <td>
  79. <span v-if="crontabValueObj.hour.length < 10">{{crontabValueObj.hour}}</span>
  80. <el-tooltip v-else :content="crontabValueObj.hour" placement="top"><span>{{crontabValueObj.hour}}</span></el-tooltip>
  81. </td>
  82. <td>
  83. <span v-if="crontabValueObj.day.length < 10">{{crontabValueObj.day}}</span>
  84. <el-tooltip v-else :content="crontabValueObj.day" placement="top"><span>{{crontabValueObj.day}}</span></el-tooltip>
  85. </td>
  86. <td>
  87. <span v-if="crontabValueObj.month.length < 10">{{crontabValueObj.month}}</span>
  88. <el-tooltip v-else :content="crontabValueObj.month" placement="top"><span>{{crontabValueObj.month}}</span></el-tooltip>
  89. </td>
  90. <td>
  91. <span v-if="crontabValueObj.week.length < 10">{{crontabValueObj.week}}</span>
  92. <el-tooltip v-else :content="crontabValueObj.week" placement="top"><span>{{crontabValueObj.week}}</span></el-tooltip>
  93. </td>
  94. <td>
  95. <span v-if="crontabValueObj.year.length < 10">{{crontabValueObj.year}}</span>
  96. <el-tooltip v-else :content="crontabValueObj.year" placement="top"><span>{{crontabValueObj.year}}</span></el-tooltip>
  97. </td>
  98. <td class="result">
  99. <span v-if="crontabValueString.length < 90">{{crontabValueString}}</span>
  100. <el-tooltip v-else :content="crontabValueString" placement="top"><span>{{crontabValueString}}</span></el-tooltip>
  101. </td>
  102. </tbody>
  103. </table>
  104. </div>
  105. <CrontabResult :ex="crontabValueString"></CrontabResult>
  106. <div class="pop_btn">
  107. <el-button type="primary" @click="submitFill">确定</el-button>
  108. <el-button type="warning" @click="clearCron">重置</el-button>
  109. <el-button @click="hidePopup">取消</el-button>
  110. </div>
  111. </div>
  112. </div>
  113. </template>
  114. <script setup>
  115. import CrontabSecond from "./second.vue"
  116. import CrontabMin from "./min.vue"
  117. import CrontabHour from "./hour.vue"
  118. import CrontabDay from "./day.vue"
  119. import CrontabMonth from "./month.vue"
  120. import CrontabWeek from "./week.vue"
  121. import CrontabYear from "./year.vue"
  122. import CrontabResult from "./result.vue"
  123. const { proxy } = getCurrentInstance()
  124. const emit = defineEmits(['hide', 'fill'])
  125. const props = defineProps({
  126. hideComponent: {
  127. type: Array,
  128. default: () => [],
  129. },
  130. expression: {
  131. type: String,
  132. default: ""
  133. }
  134. })
  135. const tabTitles = ref(["秒", "分钟", "小时", "日", "月", "周", "年"])
  136. const tabActive = ref(0)
  137. const hideComponent = ref([])
  138. const expression = ref('')
  139. const crontabValueObj = ref({
  140. second: "*",
  141. min: "*",
  142. hour: "*",
  143. day: "*",
  144. month: "*",
  145. week: "?",
  146. year: "",
  147. })
  148. const crontabValueString = computed(() => {
  149. const obj = crontabValueObj.value
  150. return obj.second
  151. + " "
  152. + obj.min
  153. + " "
  154. + obj.hour
  155. + " "
  156. + obj.day
  157. + " "
  158. + obj.month
  159. + " "
  160. + obj.week
  161. + (obj.year === "" ? "" : " " + obj.year)
  162. })
  163. watch(expression, () => resolveExp())
  164. function shouldHide(key) {
  165. return !(hideComponent.value && hideComponent.value.includes(key))
  166. }
  167. function resolveExp() {
  168. // 反解析 表达式
  169. if (expression.value) {
  170. const arr = expression.value.split(/\s+/)
  171. if (arr.length >= 6) {
  172. //6 位以上是合法表达式
  173. let obj = {
  174. second: arr[0],
  175. min: arr[1],
  176. hour: arr[2],
  177. day: arr[3],
  178. month: arr[4],
  179. week: arr[5],
  180. year: arr[6] ? arr[6] : ""
  181. }
  182. crontabValueObj.value = {
  183. ...obj,
  184. }
  185. }
  186. } else {
  187. // 没有传入的表达式 则还原
  188. clearCron()
  189. }
  190. }
  191. // tab切换值
  192. function tabCheck(index) {
  193. tabActive.value = index
  194. }
  195. // 由子组件触发,更改表达式组成的字段值
  196. function updateCrontabValue(name, value, from) {
  197. crontabValueObj.value[name] = value
  198. }
  199. // 表单选项的子组件校验数字格式(通过-props传递)
  200. function checkNumber(value, minLimit, maxLimit) {
  201. // 检查必须为整数
  202. value = Math.floor(value)
  203. if (value < minLimit) {
  204. value = minLimit
  205. } else if (value > maxLimit) {
  206. value = maxLimit
  207. }
  208. return value
  209. }
  210. // 隐藏弹窗
  211. function hidePopup() {
  212. emit("hide")
  213. }
  214. // 填充表达式
  215. function submitFill() {
  216. emit("fill", crontabValueString.value)
  217. hidePopup()
  218. }
  219. function clearCron() {
  220. // 还原选择项
  221. crontabValueObj.value = {
  222. second: "*",
  223. min: "*",
  224. hour: "*",
  225. day: "*",
  226. month: "*",
  227. week: "?",
  228. year: "",
  229. }
  230. }
  231. onMounted(() => {
  232. expression.value = props.expression
  233. hideComponent.value = props.hideComponent
  234. })
  235. </script>
  236. <style lang="scss" scoped>
  237. .pop_btn {
  238. text-align: center;
  239. margin-top: 20px;
  240. }
  241. .popup-main {
  242. position: relative;
  243. margin: 10px auto;
  244. background: #fff;
  245. border-radius: 5px;
  246. font-size: 12px;
  247. overflow: hidden;
  248. }
  249. .popup-title {
  250. overflow: hidden;
  251. line-height: 34px;
  252. padding-top: 6px;
  253. background: #f2f2f2;
  254. }
  255. .popup-result {
  256. box-sizing: border-box;
  257. line-height: 24px;
  258. margin: 25px auto;
  259. padding: 15px 10px 10px;
  260. border: 1px solid #ccc;
  261. position: relative;
  262. }
  263. .popup-result .title {
  264. position: absolute;
  265. top: -28px;
  266. left: 50%;
  267. width: 140px;
  268. font-size: 14px;
  269. margin-left: -70px;
  270. text-align: center;
  271. line-height: 30px;
  272. background: #fff;
  273. }
  274. .popup-result table {
  275. text-align: center;
  276. width: 100%;
  277. margin: 0 auto;
  278. }
  279. .popup-result table td:not(.result) {
  280. width: 3.5rem;
  281. min-width: 3.5rem;
  282. max-width: 3.5rem;
  283. }
  284. .popup-result table span {
  285. display: block;
  286. width: 100%;
  287. font-family: arial;
  288. line-height: 30px;
  289. height: 30px;
  290. white-space: nowrap;
  291. overflow: hidden;
  292. border: 1px solid #e8e8e8;
  293. }
  294. .popup-result-scroll {
  295. font-size: 12px;
  296. line-height: 24px;
  297. height: 10em;
  298. overflow-y: auto;
  299. }
  300. </style>