index.vue 10 KB

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