index.vue 12.8 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1 2 3 4 5 6 7 8
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
9
      <el-form-item label="培训计划" prop="courseType">
耿迪迪's avatar
耿迪迪 committed
10 11
        <el-select
          v-model="queryParams.courseType"
12
          placeholder="请选择培训计划"
耿迪迪's avatar
耿迪迪 committed
13 14 15 16 17 18 19 20 21 22 23
          clearable
          size="small"
        >
          <el-option
            v-for="course in courseOptions"
            :key="course.planId"
            :label="course.planName"
            :value="course.planId"
          />
        </el-select>
      </el-form-item>
24
      <el-form-item label="课程名称" prop="courseName">
耿迪迪's avatar
耿迪迪 committed
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
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="releaseTime">
        <el-date-picker
          v-model="queryParams.releaseTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择日期时间"
          default-time="12:00:00"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="search"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:book:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="lessonsList">
      <el-table-column label="课程标题" align="center" prop="courseName" />
纪泽龙's avatar
纪泽龙 committed
73 74 75 76 77 78 79 80 81 82 83
      <!-- <el-table-column label="课程类别" align="center" prop="planName">
        <template v-slot="scope">
          <div>
            {{
              courseOptions.filter(
                (item) => item.planId == scope.row.courseType
              )[0].planName
            }}
          </div>
        </template>
      </el-table-column> -->
84
      <el-table-column label="培训计划" align="center" prop="courseType">
85 86 87
        <template v-slot="scope">
          <div>
            {{
纪泽龙's avatar
纪泽龙 committed
88 89
              scope.row.courseType &&
              courseOptions.filter(
纪泽龙's avatar
纪泽龙 committed
90
                (item) => item.planId == scope.row.courseType
纪泽龙's avatar
纪泽龙 committed
91 92
              )[0] &&
              courseOptions.filter(
93 94 95 96 97 98
                (item) => item.planId == scope.row.courseType
              )[0].planName
            }}
          </div>
        </template>
      </el-table-column>
耿迪迪's avatar
耿迪迪 committed
99 100 101 102 103 104 105
      <el-table-column label="课程状态" align="center" prop="status">
        <template v-slot="scope">
          <div>{{ ["未发布", "已发布"][scope.row.status] }}</div>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="enclosure">
        <template v-slot="{ row: { enclosure } }">
纪泽龙's avatar
纪泽龙 committed
106 107 108 109 110 111
          <a
            v-if="enclosure && enclosure.indexOf('.txt') >= 0"
            @click="downloadText(enclosure)"
            class="down-load"
            >下载附件</a
          >
112
          <a v-else :href="enclosure" class="down-load">下载附件</a>
耿迪迪's avatar
耿迪迪 committed
113 114
        </template>
      </el-table-column>
115 116 117 118 119 120 121
      <!--<el-table-column label="视频" align="center" prop="video">-->
        <!--<template v-slot="{ row: { courseName, video } }">-->
          <!--<a @click="downLoadVideo(video, courseName)" class="down-load"-->
            <!--&gt;下载视频</a-->
          <!--&gt;-->
        <!--</template>-->
      <!--</el-table-column>-->
耿迪迪's avatar
耿迪迪 committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      <el-table-column
        label="发布时间"
        align="center"
        prop="releaseTime"
        :formatter="formatter"
      />
      <el-table-column
        label="考试题"
        align="center"
        prop="topicNum"
        width="180"
      >
        <template v-slot="{ row: { topicNum, courseId } }">
          <div @click="checkQuestion(courseId)" class="timuNum">
            <div v-if="topicNum > 0">{{ `已录入${topicNum}题` }}</div>
            <div v-else>未录入</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="{ row: { status, courseId } }">
          <!-- <div>{{status}}</div> -->
          <el-button
            v-if="status == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="changeLesson(courseId)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="deletLesson(courseId)"
            >删除</el-button
          >
          <el-button
            v-if="status == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="issueLesson(courseId)"
            >发布</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Dia
      ref="Dia"
      :componentsNum.sync="componentsNum"
      :courseId.sync="courseId"
      :visible.sync="dilogFlag"
    />
  </div>
</template>

<script>
import {
  getLessons,
  getLessonById,
  issue,
  deleteLesson,
} from "@/api/educationPlanExam/lessonsProgram.js";
198
// 获取培训计划
耿迪迪's avatar
耿迪迪 committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
import { getPlanList } from "@/api/educationPlanExam/trainingProgram";
import { mapGetters, mapMutations } from "vuex";

import Dia from "./components/Dia";

export default {
  name: "Book",
  components: {
    Dia,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // courseOptions: [],
      lessonsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseType: null,
        courseName: null,
        releaseTime: "",
      },
      // 表单参数
      form: {},
      // 表单校验

      dilogFlag: false,
      componentsNum: 1,
      // 点击的id,如果是新增为空
      courseId: null,
    };
  },
  computed: {
    ...mapGetters(["courseOptions"]),
  },
  created() {
    this.getPlanList();
    this.getList();
  },

  methods: {
    ...mapMutations({ setOptions: "SET_COURSE_OPTIONS" }),
    // 获取课程类别,也就是计划名称
    getPlanList() {
      getPlanList().then((res) => {
        const courseOptions = res.data.map((item) => {
          return {
            planId: item.planId,
            planName: item.planName,
          };
        });
        // this.$store.commit("SET_COURSE_OPTIONS");
        this.setOptions(courseOptions);
      });
    },

    /** 查询课程列表 */
    getList() {
      this.loading = true;
      getLessons(this.queryParams)
        .then((res) => {
纪泽龙's avatar
纪泽龙 committed
264
          console.log(res);
耿迪迪's avatar
耿迪迪 committed
265 266 267 268 269 270 271 272 273 274 275 276 277
          this.lessonsList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    search() {
      // console.log(this.queryParams);
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
278
      this.$refs.Dia.title = "新增培训课程";
耿迪迪's avatar
耿迪迪 committed
279 280 281 282 283
      this.componentsNum = 1;
      this.courseId = null;
      this.dilogFlag = true;
    },
    changeLesson(courseId) {
284
      this.$refs.Dia.title = "修改培训课程";
耿迪迪's avatar
耿迪迪 committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      this.componentsNum = 1;
      this.courseId = courseId;
      this.dilogFlag = true;
    },

    // 直接查看考题
    checkQuestion(courseId) {
      // 要查看考题的id
      this.courseId = courseId;
      console.log(this.courseId);
      // 2代表列表组件
      this.componentsNum = 2;
      this.dilogFlag = true;
    },
    // 重置
    resetClick() {
      this.reset();
      this.getList();
    },
    // 复位
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        courseType: null,
        courseName: null,
        releaseTime: "",
      };
    },
    deletLesson(courseId) {
      this.$confirm("请确定删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteLesson(courseId);
        })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
          }
          this.getList();
        })
        .catch(() => {});
    },
    // 发布
    issueLesson(courseId) {
      this.$confirm("请确定发布", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 判断是否录入答题合格数
          return getLessonById(courseId);
        })
        .then((res) => {
          if (res.data.qualifiedNum > 0) {
            return true;
          }
        })
        .then((res) => {
          if (res) {
            // 成功就发布
            return issue({ courseId });
          } else {
            this.$message({
              message: "请先录入答题合格数",
              type: "warning",
            });
          }
        })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: "发布成功",
              type: "success",
            });
            this.getList();
          }
        })
        .catch(() => {});
    },
    formatter(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index);
      if (!cellValue) return "-";
      else return cellValue;
    },
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    downloadText(url) {
      // url = url.replace(/\\/g, "/");
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      //xhr.setRequestHeader('Authorization', 'Basic a2VybWl0Omtlcm1pdA==');
      xhr.onload = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          let blob = this.response;
          console.log(blob);
          // 转换一个blob链接
          // 注: URL.createObjectURL() 静态方法会创建一个 DOMString(DOMString 是一个UTF-16字符串),
          // 其中包含一个表示参数中给出的对象的URL。这个URL的生命周期和创建它的窗口中的document绑定
          let downLoadUrl = window.URL.createObjectURL(
            new Blob([blob], {
              type: "txt",
            })
          );
          // 视频的type是video/mp4,图片是image/jpeg
          // 01.创建a标签
          let a = document.createElement("a");
          // 02.给a标签的属性download设定名称
          a.download = name;
          // 03.设置下载的文件名
          a.href = downLoadUrl;
          // 04.对a标签做一个隐藏处理
          a.style.display = "none";
          // 05.向文档中添加a标签
          document.body.appendChild(a);
          // 06.启动点击事件
          a.click();
          // 07.下载完毕删除此标签
          a.remove();
        }
      };

      xhr.send();
    },
    downLoadVideo(url, name) {
耿迪迪's avatar
耿迪迪 committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
      var xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "arraybuffer"; // 返回类型blob
      xhr.onload = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          let blob = this.response;
          console.log(blob);
          // 转换一个blob链接
          // 注: URL.createObjectURL() 静态方法会创建一个 DOMString(DOMString 是一个UTF-16字符串),
          // 其中包含一个表示参数中给出的对象的URL。这个URL的生命周期和创建它的窗口中的document绑定
          let downLoadUrl = window.URL.createObjectURL(
            new Blob([blob], {
              type: "video/mp4",
            })
          );
          // 视频的type是video/mp4,图片是image/jpeg
          // 01.创建a标签
          let a = document.createElement("a");
          // 02.给a标签的属性download设定名称
          a.download = name;
          // 03.设置下载的文件名
          a.href = downLoadUrl;
          // 04.对a标签做一个隐藏处理
          a.style.display = "none";
          // 05.向文档中添加a标签
          document.body.appendChild(a);
          // 06.启动点击事件
          a.click();
          // 07.下载完毕删除此标签
          a.remove();
        }
      };
      xhr.send();
    },
  },
};
</script>
<style lang="scss" scoped>
.down-load {
  color: #0bab0c;
}
.timuNum {
  color: #1d84ff;
  cursor: pointer;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-dialog {
  margin-top: 15vh !important;
}
</style>