瀏覽代碼

修改一次性导出

zero 1 月之前
父節點
當前提交
a6edd2481b
共有 3 個文件被更改,包括 220 次插入16 次删除
  1. 2 2
      src/services/api.ts
  2. 8 14
      src/views/news/NewsList.vue
  3. 210 0
      src/views/news/components/dialog/oneClickExportReport.vue

+ 2 - 2
src/services/api.ts

@@ -263,10 +263,10 @@ export const reportApi = {
   },
 
   //一键导出上月资讯报告
-  oneClickExportReport: async (): Promise<unknown> => {
+  oneClickExportReport: async (params: Record<string, any>): Promise<unknown> => {
     try {
       const url = `/xiaoshi/ppa/report/oneClickExport`
-      const response = await apiClient.get<unknown>(url)
+      const response = await apiClient.post<unknown>(url, params)
       return response
     } catch (error) {
       console.error(`Error from one click export report:`, error)

+ 8 - 14
src/views/news/NewsList.vue

@@ -201,6 +201,7 @@
         </div>
       </div>
     </el-card>
+    <oneClickExportReportTemplateDialog ref="exportReportTemplateRef"></oneClickExportReportTemplateDialog>
 
     <!-- Floating selection indicator -->
     <div
@@ -298,8 +299,11 @@ import { Collection, Refresh } from '@element-plus/icons-vue'
 import type { NewsItem, Category, Report } from '@/types'
 import { format } from 'date-fns'
 import type { TableInstance } from 'element-plus'
+import oneClickExportReportTemplateDialog from './components/dialog/oneClickExportReport.vue'
+const exportReportTemplateRef = ref<InstanceType<typeof oneClickExportReportTemplateDialog>>()
 
-// Extend NewsItem type to include editing state
+
+// Extend NewsItem type to include editing stat
 interface EditableNewsItem extends NewsItem {
   isEditing?: boolean
   originalData?: {
@@ -620,21 +624,11 @@ const formatDate = (date: Date, formatStr = 'yyyy-MM-dd') => {
 
 const isExporting = ref(false);
 
+
 const oneClickExportReport = async () => {
-  isExporting.value = true;
-  try {
-    const response = await reportApi.oneClickExportReport()
-    ElMessage.success('一键导出上月资讯报告成功')
-    if (response.code == 200) {
-      const guid = response.data
-      //执行下载
-      downLoad2(guid)
+    if (exportReportTemplateRef.value) {
+      exportReportTemplateRef.value.open({})
     }
-  } catch (error) {
-    ElMessage.error('一键导出上月资讯报告失败')
-  } finally {
-    isExporting.value = false;
-  }
 }
 
 const downLoad2 = (guid: string, fileName = 'download') => {

+ 210 - 0
src/views/news/components/dialog/oneClickExportReport.vue

@@ -0,0 +1,210 @@
+<template>
+  <div>
+    <el-dialog v-model="visible" title="一次性导出报告" width="500" :close-on-click-modal="false" :before-close="handleClose">
+
+       <el-form
+          ref="ruleFormRef"
+          style="max-width: 600px"
+          :model="ruleForm"
+          :rules="rules"
+          label-width="auto"
+        >
+          <el-form-item label="日期范围:" prop="startToEnd">
+            <el-date-picker
+              v-model="ruleForm.startToEnd"
+              type="daterange"
+              unlink-panels
+              range-separator="To"
+              start-placeholder="开始日期"
+              end-placeholder="结束日期"
+              :shortcuts="shortcuts"
+              format="YYYY-MM-DD"
+              value-format="YYYY-MM-DD"
+            />
+          </el-form-item>
+          <el-form-item label="模板:" prop="templateId">
+              <el-select v-model="ruleForm.templateId" placeholder="请选择模板" style="width: 100%">
+                <el-option
+                  v-for="item in tableData"
+                  :key="item.id"
+                  :label="item.templateName"
+                  :value="item.id"
+                />
+              </el-select>
+          </el-form-item>
+        </el-form>
+        
+      <template #footer>
+        <div class="dialog-footer">
+          <el-button @click="handleClose">取 消</el-button>
+          <el-button type="primary" @click="submit(ruleFormRef)" :loading="btnLoading"> 确 认 </el-button>
+        </div>
+      </template>
+    </el-dialog>
+  </div>
+</template>
+<script setup lang="ts">
+import { ref,reactive } from 'vue'
+import { ElMessage } from 'element-plus'
+import { reportApi } from '@/services/api'
+
+import type { FormInstance, FormRules } from 'element-plus'
+const ruleFormRef = ref<FormInstance>()
+interface ruleFormTypes {
+ templateId:number | null
+ beginTime:string
+ endTime:string
+ startToEnd:string[]
+}
+const ruleForm = ref<ruleFormTypes>(
+  {
+    templateId: null,
+    beginTime:'',
+    endTime:'',
+    startToEnd: []
+  }
+)
+const rules = reactive<FormRules<ruleFormTypes>>({
+  templateId: [{ required: true, message: '请选择模板', trigger: 'change' }],
+  startToEnd: [{ required: true, message: '请选择时间范围', trigger: 'change' }],
+})
+
+const shortcuts = [
+  {
+    text: '上周',
+    value: () => {
+      const end = new Date()
+      const start = new Date()
+      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
+      return [start, end]
+    },
+  },
+  {
+    text: '上个月',
+    value: () => {
+      const end = new Date()
+      const start = new Date()
+      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
+      return [start, end]
+    },
+  },
+]
+
+const visible = ref(false)
+interface reportTemplate {
+  id: number
+  templateName: string
+  templatePath: string
+  createId: number
+  createTime: Date
+}
+const tableData = ref<reportTemplate[]>([])
+const btnLoading = ref(false)
+
+const handleClose = () => {
+  ruleForm.value = {
+    templateId: null,
+    beginTime:'',
+    endTime:'',
+    startToEnd: []
+  }
+  visible.value = false
+}
+
+const parentParams = ref({})
+
+
+const open = (obj: object | null) => {
+  parentParams.value = obj || {}
+  if (!tableData.value.length) {
+    getTemplateList()
+  }
+  btnLoading.value = false
+  visible.value = true
+}
+
+const downLoad2 = (guid: string, fileName = 'download') => {
+  //获取下载地址
+  const href = getDownloadPath(guid)
+  if (!href) {
+    return
+  }
+  const anchor = document.createElement('a')
+  if ('download' in anchor) {
+    anchor.href = href
+    anchor.setAttribute('download', fileName)
+    anchor.className = 'download-js-link'
+    anchor.innerHTML = 'downloading...'
+    anchor.style.display = 'none'
+    document.body.appendChild(anchor)
+    setTimeout(function () {
+      anchor.click()
+      document.body.removeChild(anchor)
+    }, 66)
+    return true
+  }
+}
+
+//获取下载地址
+const getDownloadPath = (guid: string) => {
+  if (!guid) {
+    return ''
+  }
+  return `/api/fileManager/downloadFile?fileId=${guid}`
+}
+
+const submit = async (formEl:FormInstance | undefined ) => {
+
+  try {
+    // 1. 检验表单
+    if (!formEl) return
+    await formEl.validate(async (valid, fields) => {
+      if (valid) {
+        // 2. 获取开始与结束日期
+        var params = {
+          templateId:ruleForm.value.templateId,
+          beginTime:ruleForm.value.startToEnd[0],
+          endTime:ruleForm.value.startToEnd[1]
+        }
+        // 3. 请求
+        btnLoading.value = true;
+        try {
+          const response = await reportApi.oneClickExportReport(params)
+          ElMessage.success('一键导出上月资讯报告成功')
+          if (response.code == 200) {
+            const guid = response.data
+            //执行下载
+            downLoad2(guid)
+            handleClose()
+          }
+        } catch (error) {
+          ElMessage.error('一键导出上月资讯报告失败')
+        } finally {
+          btnLoading.value = false;
+        }
+      } else {
+        ElMessage.error('请检查参数')
+      }
+    })
+  
+  } catch (error) {
+    ElMessage.error('导出报告失败')
+    btnLoading.value = false
+  }
+}
+
+const getTemplateList = async () => {
+  try {
+    const response = await reportApi.getReportTemplate()
+    if (response.code == 200) {
+      tableData.value = response.data || []
+    }
+  } catch (error) {
+    ElMessage.error('获取报告模板失败')
+  }
+}
+
+defineExpose({
+  open,
+})
+</script>