y595705120 3 years ago
parent
commit
6a628f859a
3 changed files with 254 additions and 57 deletions
  1. 138 0
      src/components/UploadExcel/index.vue
  2. 63 0
      src/view/exam/components/IUploadAnswer.vue
  3. 53 57
      src/view/exam/train.vue

+ 138 - 0
src/components/UploadExcel/index.vue

@@ -0,0 +1,138 @@
+<template>
+  <div>
+    <input ref="excel-upload-input" class="excel-upload-input" type="file" accept=".xlsx, .xls" @change="handleClick">
+    <div class="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
+      拉入文件 或
+      <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">
+        选择文件
+      </el-button>
+    </div>
+  </div>
+</template>
+
+<script>
+import XLSX from 'xlsx'
+
+export default {
+  props: {
+    beforeUpload: Function, // eslint-disable-line
+    onSuccess: Function// eslint-disable-line
+  },
+  data() {
+    return {
+      loading: false,
+      excelData: {
+        header: null,
+        results: null
+      }
+    }
+  },
+  methods: {
+    generateData({ header, results }) {
+      this.excelData.header = header
+      this.excelData.results = results
+      this.onSuccess && this.onSuccess(this.excelData)
+    },
+    handleDrop(e) {
+      e.stopPropagation()
+      e.preventDefault()
+      if (this.loading) return
+      const files = e.dataTransfer.files
+      if (files.length !== 1) {
+        this.$message.error('Only support uploading one file!')
+        return
+      }
+      const rawFile = files[0] // only use files[0]
+
+      if (!this.isExcel(rawFile)) {
+        this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
+        return false
+      }
+      this.upload(rawFile)
+      e.stopPropagation()
+      e.preventDefault()
+    },
+    handleDragover(e) {
+      e.stopPropagation()
+      e.preventDefault()
+      e.dataTransfer.dropEffect = 'copy'
+    },
+    handleUpload() {
+      this.$refs['excel-upload-input'].click()
+    },
+    handleClick(e) {
+      const files = e.target.files
+      const rawFile = files[0] // only use files[0]
+      if (!rawFile) return
+      this.upload(rawFile)
+    },
+    upload(rawFile) {
+      this.$refs['excel-upload-input'].value = null // fix can't select the same excel
+
+      if (!this.beforeUpload) {
+        this.readerData(rawFile)
+        return
+      }
+      const before = this.beforeUpload(rawFile)
+      if (before) {
+        this.readerData(rawFile)
+      }
+    },
+    readerData(rawFile) {
+      this.loading = true
+      return new Promise((resolve, reject) => {
+        const reader = new FileReader()
+        reader.onload = e => {
+          const data = e.target.result
+          const workbook = XLSX.read(data, { type: 'array' })
+          const firstSheetName = workbook.SheetNames[0]
+          const worksheet = workbook.Sheets[firstSheetName]
+          const header = this.getHeaderRow(worksheet)
+          const results = XLSX.utils.sheet_to_json(worksheet)
+          this.generateData({ header, results })
+          this.loading = false
+          resolve()
+        }
+        reader.readAsArrayBuffer(rawFile)
+      })
+    },
+    getHeaderRow(sheet) {
+      const headers = []
+      const range = XLSX.utils.decode_range(sheet['!ref'])
+      let C
+      const R = range.s.r
+      /* start in the first row */
+      for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
+        const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
+        /* find the cell in the first row */
+        let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
+        if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
+        headers.push(hdr)
+      }
+      return headers
+    },
+    isExcel(file) {
+      return /\.(xlsx|xls|csv)$/.test(file.name)
+    }
+  }
+}
+</script>
+
+<style scoped>
+.excel-upload-input{
+  display: none;
+  z-index: -9999;
+}
+.drop{
+  border: 2px dashed #bbb;
+  width: 600px;
+  height: 160px;
+  line-height: 160px;
+  margin: 0 auto;
+  font-size: 24px;
+  border-radius: 5px;
+  text-align: center;
+  color: #bbb;
+  position: relative;
+}
+</style>

+ 63 - 0
src/view/exam/components/IUploadAnswer.vue

@@ -0,0 +1,63 @@
+<template>
+  <div class="app-container">
+    <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
+	<div class="p20 tc">
+		<el-button @click="onSubmit" type="primary">确认提交</el-button>
+	</div>
+	
+    <el-table :data="tableData" border highlight-current-row 
+	style="width: 100%;margin-top:20px;">
+      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
+    </el-table>
+  </div>
+</template>
+
+<script>
+import UploadExcelComponent from '@/components/UploadExcel/index.vue'
+
+export default {
+  name: 'UploadExcel',
+  components: { UploadExcelComponent },
+  data() {
+    return {
+      tableData: [],
+      tableHeader: []
+    }
+  },
+  methods: {
+    beforeUpload(file) {
+      const isLt1M = file.size / 1024 / 1024 < 1
+      if (isLt1M) {
+        return true
+      }
+
+      this.$message({
+        message: 'Please do not upload files larger than 1m in size.',
+        type: 'warning'
+      })
+      return false
+    },
+	onSubmit(){
+		let list = []
+		this.tableData.forEach( v=>{
+			let item = Object.assign( {}, v)
+			item.answerId = +v.answer_id;
+			item.type = +v.type;
+			delete item["answer_id"];
+			if( item.answerId ){
+				list.push( item )
+			}
+		})
+		if( list.length <1){
+			this.$message.errorMsg("无内容", 2)
+			return 
+		}
+		this.$emit("submit", list )
+	},
+    handleSuccess({ results, header }) {
+      this.tableData = results
+      this.tableHeader = header
+    }
+  }
+}
+</script>

+ 53 - 57
src/view/exam/train.vue

@@ -6,11 +6,24 @@
 
     <el-table :data="tableData"  border stripe>
       <el-table-column label="题组Id"  prop="examId"></el-table-column>
-      <el-table-column label="题组名字"  prop="name"></el-table-column>
+      <el-table-column label="题组名字"  prop="name">
+		<template slot-scope="{ row }">
+		  <el-input
+		    v-if="row.edit"
+		    class="edit-input"
+		    size="small"
+		    v-model="row.name"
+		    clearable
+		  />
+		  <span v-else>{{ row.name }}</span>
+		</template>
+		  
+		  
+	  </el-table-column>
 
       <el-table-column
         align="center"
-        width="160px"
+        width="260px"
         label="操作"
         class-name="small-padding fixed-width"
       >
@@ -37,6 +50,13 @@
             icon="el-icon-edit"
             @click="initEdit(row, $index)"
           >编辑</el-button>
+		 <el-button
+		   v-if="!row.edit"
+		   type="warning"
+		   size="small"
+		   icon="el-icon-edit"
+		   @click="importData(row, $index)"
+		 >导入题库</el-button>
 		 
 		  
         </template>
@@ -55,45 +75,20 @@
 
     <el-dialog title="新增题库" center :visible.sync="dialogFormVisible"  >
       <el-form :model="form" :rules="rules" label-width="120px" ref="elForm">
-        <el-form-item  label="训练题库" prop="title" >
-          <el-input  v-model="form.title" type="text" class="user-form-item" clearable></el-input>
-        </el-form-item>
-		
-        <el-form-item  label="考题数" prop="counter">
-          <el-input  v-model="form.counter" type="number" class="user-form-item" clearable></el-input>
+        <el-form-item  label="题库名称" prop="name" >
+          <el-input  v-model="form.name" type="text" class="user-form-item" clearable></el-input>
         </el-form-item>
-		
-		<el-form-item  label="时长[秒]" prop="duration">
-		  <el-input  v-model="form.duration" type="number" class="user-form-item" clearable></el-input>
-		</el-form-item>
-		
-		<el-form-item  label="开始时间" prop="fromDate">
-		  <el-date-picker 
-		  			v-model="form.fromDate"
-		  			value-format="yyyy-MM-dd"
-		  			type="date"
-		  			align="left"
-		  ></el-date-picker>
-
-		</el-form-item>
-		
-		<el-form-item  label="结束时间" prop="toDate">
-		  <el-date-picker 
-		  	v-model="form.toDate"
-		  	value-format="yyyy-MM-dd"
-		  	type="date"
-		  	align="left"
-		  ></el-date-picker>
-		
-		</el-form-item>
-		
-		
       </el-form>
       <div class="dialog-footer" slot="footer">
         <el-button @click="dialogFormVisible=false">取 消</el-button>
         <el-button @click="addGroup" type="primary">确 定</el-button>
       </div>
     </el-dialog>
+	
+	
+	<el-dialog title="导入题库" center :visible.sync="dialogImport"  width="1024px">
+		<IUploadAnswer @submit="onSubmit"></IUploadAnswer>
+	</el-dialog>
   </div>
 </template>
 
@@ -102,21 +97,17 @@ import { getWxGroupList, postData} from "@/api/exam.js";
 import {toDatetime} from "@/utils/date"
 import { time2str, str2time} from "@/utils/index.js";
 import DropSearch from '@/components/select/DropSearch.vue';
+import IUploadAnswer from './components/IUploadAnswer.vue';
 
 export default {
   name: "Api",
   data() {
     return {
       dialogFormVisible: false,
-      dialogTitle: "新增广告",
-      boolVal:[0,1],
+	  dialogImport: false,
+	  examId: 0,
       form: {
-		id: 0,
-        title:'',
-		fromDate:'',
-		counter:10,
-		toDate:'',
-		duration: 300,
+        name:''
       },
       searchInfo:{},
       page: 1,
@@ -124,22 +115,26 @@ export default {
       pageSize: 10,
       tableData: [],
       rules: {
-        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
-		counter: [{ required: true, message: "请输入题数", trigger: "blur" }],
-        fromDate: [ { required: true, message: "请输入开始时间", trigger: "blur" }],
-		toDate: [ { required: true, message: "请输入结束时间", trigger: "blur" }],
-		duration: [ { required: true, message: "请输入时长", trigger: "blur" }],
+        name: [{ required: true, message: "请输入题库", trigger: "blur" }]
       }
     };
   },
-  components:{DropSearch},
+  components:{DropSearch,IUploadAnswer},
   methods: {
     //条件搜索前端看此方法
-    onSubmit() {
-      this.page = 1;
-      this.pageSize = 10;
-      this.getTableData();
-    },
+    onSubmit( list ) {
+		let examId = this.examId;
+		postData( "addAnswerList", {examId, list}).then( res=>{
+			if( res.code == 200){
+				this.$message.successMsg("导入成功", 2);
+				this.dialogImport = false
+			}
+		})
+	},
+	importData(row){
+		this.examId = row.examId;
+		this.dialogImport = true;
+	},
     cancelEdit(row, index) {
       row = Object.assign(row, row.temp);
       row.edit = false;
@@ -184,7 +179,10 @@ export default {
 	getTableData(){
 		postData("GetAllExamList", {}).then( res=>{
 			if( res.code == 200){
-				this.tableData = res.data||[]
+				this.tableData = res.data.map( v=>{
+					v.edit = false
+					return v
+				})
 			}
 		})
 	},
@@ -200,9 +198,7 @@ export default {
 	  this.$refs["elForm"].validate((valid) => {
 		if (!valid) return;
 		let param = Object.assign({}, this.form);
-		param.duration = +param.duration
-		param.counter = +param.counter
-		postData('addWxGroup', param).then((res) => {
+		postData('addExam', param).then((res) => {
 		  if( res.code == 200){
 			this.dialogFormVisible = false
 			this.$message.successMsg("新增成功", 1);