xyc
2026-08-14 1f4a6cc6045f47861c3e8d49a782afc3e11e3843
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<template>
  <el-container>
    <el-header>交通统计报表审核系统 - 数据导入</el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu router default-active="/import">
          <el-menu-item index="/dashboard">工作台</el-menu-item>
          <el-menu-item index="/import">数据导入</el-menu-item>
          <el-menu-item index="/data">数据查看</el-menu-item>
          <el-menu-item index="/explain">企业解释</el-menu-item>
          <el-menu-item index="/audit">审核结果</el-menu-item>
          <el-menu-item index="/report">报表生成</el-menu-item>
          <el-menu-item index="/rules">规则管理</el-menu-item>
          <el-menu-item index="/users">用户管理</el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <div class="page-scroll">
        <h3>导入业务数据</h3>
        <el-card style="max-width:640px">
          <el-form label-width="100px">
            <el-form-item label="导入类型">
              <el-radio-group v-model="importType">
                <el-radio label="h2032">H203-2月报</el-radio>
                <el-radio label="transportAuth">运政车辆</el-radio>
                <el-radio label="trackMileage">轨迹里程</el-radio>
                <el-radio label="scaleSplit">规上规下拆分</el-radio>
                <el-radio label="freightTurnover">货运量周转量</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="报表期">
              <el-date-picker v-model="period" type="month" placeholder="选择月份" value-format="yyyy-MM" style="width:100%"></el-date-picker>
            </el-form-item>
            <el-form-item label="选择文件">
              <el-upload ref="upload" :auto-upload="false" :limit="1" accept=".xlsx,.xls"
                         :on-change="handleFileChange" :file-list="fileList">
                <el-button slot="trigger" type="primary">选择 Excel 文件</el-button>
              </el-upload>
              <div class="tip">请使用对应模板:{{ templateName }}</div>
            </el-form-item>
            <el-form-item>
              <el-button type="success" @click="importData" :loading="loading">开始导入</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-alert v-if="result" type="success" style="margin-top:15px" closable @close="result=''">
          <div style="white-space: pre-line">{{ result }}</div>
        </el-alert>
        <el-alert v-if="error" :title="error" type="error" style="margin-top:15px" closable @close="error=''"></el-alert>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import api from '@/api'
export default {
  name: 'DataImport',
  data() {
    return {
      importType: 'h2032',
      period: '',
      file: null,
      fileList: [],
      loading: false,
      result: '',
      error: ''
    }
  },
  computed: {
    templateName() {
      const map = {
        h2032: '模板_交企统H203-2表_道路货物运输月度生产情况.xlsx',
        transportAuth: '模板_运政车辆.xlsx',
        trackMileage: '模板_轨迹里程.xlsx',
        scaleSplit: '模板_规上规下拆分运输量.xlsx',
        freightTurnover: '模板_货运量周转量.xlsx'
      }
      return map[this.importType] || ''
    },
    endpoint() {
      const map = {
        h2032: '/import/h2032',
        transportAuth: '/import/transportAuth',
        trackMileage: '/import/trackMileage',
        scaleSplit: '/import/scaleSplit',
        freightTurnover: '/import/freightTurnover'
      }
      return map[this.importType]
    }
  },
  methods: {
    handleFileChange(file) { this.file = file.raw },
    importData() {
      if (!this.file) { this.error = '请选择文件'; return }
      if (!this.period) { this.error = '请选择报表期'; return }
      this.loading = true; this.error = ''; this.result = ''
      let fd = new FormData()
      fd.append('file', this.file)
      fd.append('period', this.period)
      api.post(this.endpoint, fd, { headers: { 'Content-Type': 'multipart/form-data' } })
        .then(res => {
          this.result = res.data || '导入成功'
          this.fileList = []
          this.file = null
        })
        .catch(e => { this.error = (e.response && e.response.data && e.response.data.message) || '导入失败' })
        .finally(() => { this.loading = false })
    }
  }
}
</script>
<style scoped>
.el-header { background: #409EFF; color: white; line-height: 60px; font-size: 18px; }
.tip { font-size: 12px; color: #909399; margin-top: 6px; }
</style>