Parcourir la source

添加报表自动cron表达式

FinalYu il y a 2 ans
Parent
commit
13a30882a1

+ 33 - 0
chuanyi_client2/package-lock.json

@@ -11,6 +11,7 @@
       "dependencies": {
         "axios": "^0.27.2",
         "core-js": "^3.26.1",
+        "cron-parser": "^4.7.1",
         "electron-icon-builder": "^2.0.1",
         "element-ui": "^2.15.10",
         "exceljs": "^4.3.0",
@@ -5327,6 +5328,17 @@
         "sha.js": "^2.4.8"
       }
     },
+    "node_modules/cron-parser": {
+      "version": "4.7.1",
+      "resolved": "https://registry.npmmirror.com/cron-parser/-/cron-parser-4.7.1.tgz",
+      "integrity": "sha512-WguFaoQ0hQ61SgsCZLHUcNbAvlK0lypKXu62ARguefYmjzaOXIVRNrAmyXzabTwUn4sQvQLkk6bjH+ipGfw8bA==",
+      "dependencies": {
+        "luxon": "^3.2.1"
+      },
+      "engines": {
+        "node": ">=12.0.0"
+      }
+    },
     "node_modules/cross-spawn": {
       "version": "6.0.5",
       "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-6.0.5.tgz",
@@ -10646,6 +10658,14 @@
       "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==",
       "peer": true
     },
+    "node_modules/luxon": {
+      "version": "3.2.1",
+      "resolved": "https://registry.npmmirror.com/luxon/-/luxon-3.2.1.tgz",
+      "integrity": "sha512-QrwPArQCNLAKGO/C+ZIilgIuDnEnKx5QYODdDtbFaxzsbZcc/a7WFq7MhsVYgRlwawLtvOUESTlfJ+hc/USqPg==",
+      "engines": {
+        "node": ">=12"
+      }
+    },
     "node_modules/make-dir": {
       "version": "3.1.0",
       "resolved": "https://registry.npmmirror.com/make-dir/-/make-dir-3.1.0.tgz",
@@ -23364,6 +23384,14 @@
         "sha.js": "^2.4.8"
       }
     },
+    "cron-parser": {
+      "version": "4.7.1",
+      "resolved": "https://registry.npmmirror.com/cron-parser/-/cron-parser-4.7.1.tgz",
+      "integrity": "sha512-WguFaoQ0hQ61SgsCZLHUcNbAvlK0lypKXu62ARguefYmjzaOXIVRNrAmyXzabTwUn4sQvQLkk6bjH+ipGfw8bA==",
+      "requires": {
+        "luxon": "^3.2.1"
+      }
+    },
     "cross-spawn": {
       "version": "6.0.5",
       "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-6.0.5.tgz",
@@ -27664,6 +27692,11 @@
         }
       }
     },
+    "luxon": {
+      "version": "3.2.1",
+      "resolved": "https://registry.npmmirror.com/luxon/-/luxon-3.2.1.tgz",
+      "integrity": "sha512-QrwPArQCNLAKGO/C+ZIilgIuDnEnKx5QYODdDtbFaxzsbZcc/a7WFq7MhsVYgRlwawLtvOUESTlfJ+hc/USqPg=="
+    },
     "make-dir": {
       "version": "3.1.0",
       "resolved": "https://registry.npmmirror.com/make-dir/-/make-dir-3.1.0.tgz",

+ 1 - 0
chuanyi_client2/package.json

@@ -15,6 +15,7 @@
   "dependencies": {
     "axios": "^0.27.2",
     "core-js": "^3.26.1",
+    "cron-parser": "^4.7.1",
     "electron-icon-builder": "^2.0.1",
     "element-ui": "^2.15.10",
     "exceljs": "^4.3.0",

+ 7 - 1
chuanyi_client2/src/api/datasource.js

@@ -472,8 +472,14 @@ export function getReportTableById(id) {
  * @returns {AxiosPromise}
  */
 export function tableExchangeTypeById(params) {
+    let uri = '/reportTable/tableExchangeTypeById'
+    uri += '?id=' + params.id
+    uri += '&isAutoReport=' + params.isAutoReport
+    if (params.cron) {
+        uri += '&cron=' + params.cron
+    }
     return request({
-        url: '/reportTable/tableExchangeTypeById?id=' + params.id + '&isAutoReport=' + params.isAutoReport,
+        url: uri,
         headers: {
             isToken: true
         },

+ 121 - 6
chuanyi_client2/src/views/my_report/index.vue

@@ -85,6 +85,36 @@
       </el-form>
     </el-dialog>
 
+    <!-- 自动报表CRON定时表达式 -->
+    <el-dialog
+        title="请输入定时任务表达式"
+        width="80%"
+        top="10vh"
+        center
+        :before-close="dialogClose"
+        :visible.sync="dialogAutoReportVisible"
+        :close-on-click-modal="false"
+        :append-to-body="true">
+      <el-form label-width="80px">
+        <el-form-item label="表达式">
+          <el-input type="text" placeholder="请输入定时任务表达式" v-model="cronVal" maxlength="50"></el-input>
+        </el-form-item>
+        <div>
+          <label>常用定时任务表达式例子</label>
+          <ul>
+            <li v-for="(item, i) in cronList" style="margin-bottom: 5px;">
+              <span class="cron-txt" @click="cronNodeEvent(item)">{{ item.value }}</span>
+              <span class="cron-txt-desc">{{ item.label }}</span>
+            </li>
+          </ul>
+        </div>
+        <el-form-item style="text-align: center;">
+          <el-button type="primary" @click="chooseCronEvent" style="margin-top: 20px;">确定</el-button>
+          <el-button @click="dialogClose" style="margin-top: 20px;">取消</el-button>
+        </el-form-item>
+      </el-form>
+    </el-dialog>
+
     <!-- 报表下载类型选择 -->
     <el-dialog
         title="选择下载类型"
@@ -120,12 +150,13 @@ import {
   getAllTableTemplate, getChartData,
   getDataModelById, getReportTableById, getTableData,
   getTableTemplateById, saveReport, tableExchangeTypeById
-} from "@/api/datasource";
-import {insertEChart/*, exportExcel, packtable*/} from 'luckytool'
-import ExcelJS from 'exceljs'
+} from '@/api/datasource'
+import {insertEChart} from 'luckytool'
+// import {insertEChart, exportExcel, packtable} from 'luckytool'
+// import ExcelJS from 'exceljs'
 import Print from 'print-js'
 import {getUsername} from '@/utils/auth'
-import {exportExcel} from "@/utils/export";
+import {exportExcel} from '@/utils/export'
 
 export default {
   name: "index",
@@ -142,8 +173,12 @@ export default {
       chooseMyReport: null,
       dialogReportTemplateVisible: false,
       dialogDownloadReportTypeVisible: false,
+      dialogAutoReportVisible: false,
       downloadType: '1',
       btnType: '',
+      cronVal: '',
+      cronList: [],
+      reportId: null,
       reportTemplateList: [],
       dataModelList: [],
       chooseReportTemplate: null,
@@ -180,6 +215,7 @@ export default {
   created() {
     luckysheet.destroy()
     this.loadReport()
+    this.initCronList()
   },
   methods: {
     handleSizeChange(val) {
@@ -195,14 +231,64 @@ export default {
       this.reportPage = 1
       this.loadReport()
     },
+    validCron(value) {
+      const cronParse = require('cron-parser')
+      try {
+        const interval = cronParse.parseExpression(value)
+        console.log('cronDate:', interval.next().toDate())
+        return true
+      } catch (e) {
+      }
+      return false
+    },
+    chooseCronEvent() {
+      if (!this.cronVal) {
+        this.$message({
+          message: '请输入定时任务表达式!',
+          type: 'warning'
+        })
+        return
+      }
+      if (!this.validCron(this.cronVal)) {
+        this.$message({
+          message: '定时任务表达式格式不正确,请检查!',
+          type: 'warning'
+        })
+        return
+      }
+      const loading = showLoading(this, '请稍候···')
+      let params = {
+        'id': this.reportId,
+        'isAutoReport': '1',
+        'cron': this.cronVal
+      }
+      tableExchangeTypeById(params).then(res => {
+        loading.close()
+        this.dialogClose()
+        this.loadReport()
+      }).catch((e) => {
+        loading.close()
+        this.$message({
+          message: '保存失败!',
+          type: 'warning'
+        })
+      })
+    },
     switchChangeEvent(val, data) {
       if (val) {
-        console.log(val)
+        this.dialogAutoReportVisible = true
+        this.reportId = data.id
+        getReportTableById(data.id).then(res => {
+          this.cronVal = res.data.cron
+        }).catch((e) => {
+          console.log(e)
+        })
+        return
       }
       const loading = showLoading(this, '请稍候···')
       let params = {
         'id': data.id,
-        'isAutoReport': val ? '1' : '0'
+        'isAutoReport': '0'
       }
       tableExchangeTypeById(params).then(res => {
         loading.close()
@@ -856,6 +942,21 @@ export default {
       });
       return objRowColumn;
     },
+    cronNodeEvent(obj) {
+      this.cronVal = obj.value
+    },
+    initCronList() {
+      this.cronList = []
+      this.cronList.push({ value: '0/2 * * * * ?', label: '表示每2秒钟执行一次任务' })
+      this.cronList.push({ value: '0 0/2 * * * ?', label: '表示每2分钟执行一次任务' })
+      this.cronList.push({ value: '0 0 0/2 * * ?', label: '表示每2小时执行一次任务' })
+      this.cronList.push({ value: '0 15 10 * * ?', label: '表示在每天上午10点15分执行一次任务' })
+      this.cronList.push({ value: '0 0 10,14,16 * * ?', label: '表示在每天的上午10点、下午2点、下午4点分别执行一次任务' })
+      this.cronList.push({ value: '0 0/30 9-17 * * ?', label: '表示在每天的上午9点到下午5点的范围内每30分钟执行一次任务' })
+      this.cronList.push({ value: '0 0 12 ? * WED', label: '表示在每周星期三中午12点执行一次任务' })
+      this.cronList.push({ value: '0 0 2 1 * ?', label: '表示在每月的1日的凌晨2点执行一次任务' })
+      this.cronList.push({ value: '0 15 10 * * ? 2023', label: '表示在2023年每天上午10点15分执行一次任务' })
+    },
     /** 取消保存报表 */
     cancelSaveReport() {
       luckysheet.destroy()
@@ -865,11 +966,14 @@ export default {
     },
     /** 弹出层关闭事件 */
     dialogClose(done) {
+      this.cronVal = ''
+      this.reportId = null
       if (typeof (done) === 'function') {
         done()
       } else {
         this.dialogReportTemplateVisible = false
         this.dialogDownloadReportTypeVisible = false
+        this.dialogAutoReportVisible = false
       }
     }
   }
@@ -968,4 +1072,15 @@ export default {
     line-height: 28px;
   }
 }
+
+.cron-txt {
+  color: blue;
+  margin-right: 20px;
+  cursor: pointer;
+  font-weight: bold;
+  font-size: 16px;
+}
+
+.cron-txt-desc {
+}
 </style>