增加课程资源管理,对应数据表train_resource

This commit is contained in:
buliping committed 2026-10-08 11:14:20 +08:00
1 parent 1162f26e7d
commit abb042ff1a
8 files changed
+1051

No files matched your search

@@ -0,0 +1,93 @@
package com.ruoyi.web.controller.train;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.train.domain.TrainResource;
import com.ruoyi.train.service.ITrainResourceService;
/**
* 教学资源Controller
*
* @author ruoyi
*/
@RestController
@RequestMapping("/train/resource")
public class TrainResourceController extends BaseController
{
@Autowired
private ITrainResourceService trainResourceService;
/**
* 查询教学资源列表
*/
@PreAuthorize("@ss.hasPermi('train:resource:list')")
@GetMapping("/list")
public TableDataInfo list(TrainResource trainResource)
{
startPage();
List<TrainResource> list = trainResourceService.selectTrainResourceList(trainResource);
return getDataTable(list);
}
/**
* 获取教学资源详细信息
*/
@PreAuthorize("@ss.hasPermi('train:resource:list')")
@GetMapping(value = "/{resourceId}")
public AjaxResult getInfo(@PathVariable("resourceId") String resourceId)
{
return success(trainResourceService.selectTrainResourceByResourceId(resourceId));
}
/**
* 新增教学资源
*/
@PreAuthorize("@ss.hasPermi('train:resource:edit')")
@Log(title = "教学资源", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody TrainResource trainResource)
{
trainResource.setCreateBy(SecurityUtils.getUsername());
return toAjax(trainResourceService.insertTrainResource(trainResource));
}
/**
* 删除教学资源 (RESTful DELETE /train/resource/{resourceIds})
*/
@PreAuthorize("@ss.hasPermi('train:resource:edit')")
@Log(title = "教学资源", businessType = BusinessType.DELETE)
@DeleteMapping("/{resourceIds}")
public AjaxResult remove(@PathVariable String[] resourceIds)
{
return toAjax(trainResourceService.deleteTrainResourceByResourceIds(resourceIds));
}
/**
* 修改教学资源 (RESTful PUT /train/resource/{resourceId})
*/
@PreAuthorize("@ss.hasPermi('train:resource:edit')")
@Log(title = "教学资源", businessType = BusinessType.UPDATE)
@PutMapping("/{resourceId}")
public AjaxResult edit(@PathVariable("resourceId") String resourceId, @Validated @RequestBody TrainResource trainResource)
{
trainResource.setResourceId(resourceId);
trainResource.setUpdateBy(SecurityUtils.getUsername());
return toAjax(trainResourceService.updateTrainResource(trainResource));
}
}
@@ -0,0 +1,149 @@
package com.ruoyi.train.domain;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
/**
* 教学资源对象 train_resource
*
* <p>统一教学资源:文档、视频、3D模型、VR资源、外部教学链接、其他。
* 该表被 train_course_resource、train_study_duration 外键引用,删除一律走逻辑删除。</p>
*
* @author ruoyi
*/
public class TrainResource extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 资源唯一ID */
private String resourceId;
/** 资源名称 */
@NotBlank(message = "资源名称不能为空")
@Size(max = 150, message = "资源名称长度不能超过150个字符")
@Excel(name = "资源名称")
private String resourceName;
/** 资源类型 (1:文档, 2:视频, 3:3D模型, 4:VR资源, 5:外部教学链接, 6:其他) */
@NotNull(message = "资源类型不能为空")
@Min(value = 1, message = "资源类型取值必须为1-6")
@Max(value = 6, message = "资源类型取值必须为1-6")
@Excel(name = "资源类型", readConverterExp = "1=文档,2=视频,3=3D模型,4=VR资源,5=外部教学链接,6=其他")
private Integer resourceType;
/** 资源存储地址/在线链接 */
@NotBlank(message = "资源地址不能为空")
@Size(max = 500, message = "资源地址长度不能超过500个字符")
@Excel(name = "资源地址")
private String resourceUrl;
/** 资源描述/使用说明 */
@Size(max = 500, message = "资源描述长度不能超过500个字符")
@Excel(name = "资源描述")
private String description;
/** 状态 (1:正常, 0:停用) */
@Min(value = 0, message = "状态取值必须为0或1")
@Max(value = 1, message = "状态取值必须为0或1")
@Excel(name = "状态", readConverterExp = "0=停用,1=正常")
private Integer status;
/** 删除标志 (0代表存在 2代表删除) */
private String delFlag;
public void setResourceId(String resourceId)
{
this.resourceId = resourceId;
}
public String getResourceId()
{
return resourceId;
}
public void setResourceName(String resourceName)
{
this.resourceName = resourceName;
}
public String getResourceName()
{
return resourceName;
}
public void setResourceType(Integer resourceType)
{
this.resourceType = resourceType;
}
public Integer getResourceType()
{
return resourceType;
}
public void setResourceUrl(String resourceUrl)
{
this.resourceUrl = resourceUrl;
}
public String getResourceUrl()
{
return resourceUrl;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setStatus(Integer status)
{
this.status = status;
}
public Integer getStatus()
{
return status;
}
public void setDelFlag(String delFlag)
{
this.delFlag = delFlag;
}
public String getDelFlag()
{
return delFlag;
}
@Override
public String toString()
{
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("resourceId", getResourceId())
.append("resourceName", getResourceName())
.append("resourceType", getResourceType())
.append("resourceUrl", getResourceUrl())
.append("description", getDescription())
.append("status", getStatus())
.append("delFlag", getDelFlag())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.toString();
}
}
@@ -0,0 +1,60 @@
package com.ruoyi.train.mapper;
import java.util.List;
import com.ruoyi.train.domain.TrainResource;
/**
* 教学资源Mapper接口
*
* @author ruoyi
*/
public interface TrainResourceMapper
{
/**
* 查询教学资源
*
* @param resourceId 教学资源主键
* @return 教学资源
*/
public TrainResource selectTrainResourceByResourceId(String resourceId);
/**
* 查询教学资源列表
*
* @param trainResource 教学资源查询条件
* @return 教学资源集合
*/
public List<TrainResource> selectTrainResourceList(TrainResource trainResource);
/**
* 新增教学资源
*
* @param trainResource 教学资源
* @return 结果
*/
public int insertTrainResource(TrainResource trainResource);
/**
* 修改教学资源
*
* @param trainResource 教学资源
* @return 结果
*/
public int updateTrainResource(TrainResource trainResource);
/**
* 删除教学资源(逻辑删除)
*
* @param resourceId 教学资源主键
* @return 结果
*/
public int deleteTrainResourceByResourceId(String resourceId);
/**
* 批量删除教学资源(逻辑删除)
*
* @param resourceIds 需要删除的数据主键集合
* @return 结果
*/
public int deleteTrainResourceByResourceIds(String[] resourceIds);
}
@@ -0,0 +1,60 @@
package com.ruoyi.train.service;
import java.util.List;
import com.ruoyi.train.domain.TrainResource;
/**
* 教学资源Service接口
*
* @author ruoyi
*/
public interface ITrainResourceService
{
/**
* 查询教学资源
*
* @param resourceId 教学资源主键
* @return 教学资源
*/
public TrainResource selectTrainResourceByResourceId(String resourceId);
/**
* 查询教学资源列表
*
* @param trainResource 教学资源查询条件
* @return 教学资源集合
*/
public List<TrainResource> selectTrainResourceList(TrainResource trainResource);
/**
* 新增教学资源
*
* @param trainResource 教学资源
* @return 结果
*/
public int insertTrainResource(TrainResource trainResource);
/**
* 修改教学资源
*
* @param trainResource 教学资源
* @return 结果
*/
public int updateTrainResource(TrainResource trainResource);
/**
* 批量删除教学资源
*
* @param resourceIds 需要删除的教学资源主键集合
* @return 结果
*/
public int deleteTrainResourceByResourceIds(String[] resourceIds);
/**
* 删除教学资源信息
*
* @param resourceId 教学资源主键
* @return 结果
*/
public int deleteTrainResourceByResourceId(String resourceId);
}
@@ -0,0 +1,159 @@
package com.ruoyi.train.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.train.domain.TrainResource;
import com.ruoyi.train.mapper.TrainResourceMapper;
import com.ruoyi.train.service.ITrainResourceService;
/**
* 教学资源Service业务层处理
*
* <p>资源表被 train_course_resource、train_study_duration 外键引用,
* 因此删除统一使用逻辑删除(del_flag = '2'),禁止物理删除。</p>
*
* @author ruoyi
*/
@Service
public class TrainResourceServiceImpl implements ITrainResourceService
{
@Autowired
private TrainResourceMapper trainResourceMapper;
/**
* 查询教学资源
*
* @param resourceId 教学资源主键
* @return 教学资源
*/
@Override
public TrainResource selectTrainResourceByResourceId(String resourceId)
{
return trainResourceMapper.selectTrainResourceByResourceId(resourceId);
}
/**
* 查询教学资源列表
*
* @param trainResource 教学资源查询条件
* @return 教学资源集合
*/
@Override
public List<TrainResource> selectTrainResourceList(TrainResource trainResource)
{
return trainResourceMapper.selectTrainResourceList(trainResource);
}
/**
* 新增教学资源
*
* @param trainResource 教学资源
* @return 结果
*/
@Override
public int insertTrainResource(TrainResource trainResource)
{
validateResourceType(trainResource.getResourceType());
validateStatus(trainResource.getStatus());
// 未指定主键时自动生成32位UUID,与 resource_id VARCHAR(32) 对齐
if (StringUtils.isEmpty(trainResource.getResourceId()))
{
trainResource.setResourceId(IdUtils.fastSimpleUUID());
}
if (trainResource.getStatus() == null)
{
trainResource.setStatus(1); // 默认正常
}
trainResource.setDelFlag("0");
trainResource.setCreateTime(DateUtils.getNowDate());
return trainResourceMapper.insertTrainResource(trainResource);
}
/**
* 修改教学资源
*
* @param trainResource 教学资源(必须携带 resourceId)
* @return 结果
*/
@Override
public int updateTrainResource(TrainResource trainResource)
{
if (StringUtils.isEmpty(trainResource.getResourceId()))
{
throw new ServiceException("资源ID不能为空");
}
if (trainResource.getResourceType() != null)
{
validateResourceType(trainResource.getResourceType());
}
if (trainResource.getStatus() != null)
{
validateStatus(trainResource.getStatus());
}
trainResource.setUpdateTime(DateUtils.getNowDate());
return trainResourceMapper.updateTrainResource(trainResource);
}
/**
* 批量删除教学资源
*
* @param resourceIds 需要删除的教学资源主键集合
* @return 结果
*/
@Override
public int deleteTrainResourceByResourceIds(String[] resourceIds)
{
if (resourceIds == null || resourceIds.length == 0)
{
throw new ServiceException("请选择要删除的资源");
}
return trainResourceMapper.deleteTrainResourceByResourceIds(resourceIds);
}
/**
* 删除教学资源信息
*
* @param resourceId 教学资源主键
* @return 结果
*/
@Override
public int deleteTrainResourceByResourceId(String resourceId)
{
if (StringUtils.isEmpty(resourceId))
{
throw new ServiceException("资源ID不能为空");
}
return trainResourceMapper.deleteTrainResourceByResourceId(resourceId);
}
/**
* 校验资源类型取值:1文档 2视频 33D模型 4VR资源 5外部教学链接 6其他
*/
private void validateResourceType(Integer resourceType)
{
if (resourceType == null)
{
throw new ServiceException("资源类型不能为空");
}
if (resourceType < 1 || resourceType > 6)
{
throw new ServiceException("资源类型取值必须为1-6");
}
}
/**
* 校验状态取值:0停用 1正常
*/
private void validateStatus(Integer status)
{
if (status != null && status != 0 && status != 1)
{
throw new ServiceException("资源状态取值必须为0或1");
}
}
}
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.train.mapper.TrainResourceMapper">
<resultMap type="TrainResource" id="TrainResourceResult">
<result property="resourceId" column="resource_id" />
<result property="resourceName" column="resource_name" />
<result property="resourceType" column="resource_type" />
<result property="resourceUrl" column="resource_url" />
<result property="description" column="description" />
<result property="status" column="status" />
<result property="delFlag" column="del_flag" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectTrainResourceVo">
select resource_id, resource_name, resource_type, resource_url, description, status, del_flag, create_by, create_time, update_by, update_time, remark from train_resource
</sql>
<select id="selectTrainResourceList" parameterType="TrainResource" resultMap="TrainResourceResult">
<include refid="selectTrainResourceVo"/>
where del_flag = '0'
<if test="resourceName != null and resourceName != ''"> and resource_name like concat('%', #{resourceName}, '%')</if>
<if test="resourceType != null"> and resource_type = #{resourceType}</if>
<if test="resourceUrl != null and resourceUrl != ''"> and resource_url like concat('%', #{resourceUrl}, '%')</if>
<if test="description != null and description != ''"> and description like concat('%', #{description}, '%')</if>
<if test="status != null"> and status = #{status}</if>
<if test="createBy != null and createBy != ''"> and create_by = #{createBy}</if>
<if test="params.beginTime != null and params.beginTime != ''"> and create_time &gt;= #{params.beginTime}</if>
<if test="params.endTime != null and params.endTime != ''"> and create_time &lt;= #{params.endTime}</if>
order by create_time desc
</select>
<select id="selectTrainResourceByResourceId" parameterType="String" resultMap="TrainResourceResult">
<include refid="selectTrainResourceVo"/>
where resource_id = #{resourceId} and del_flag = '0'
</select>
<insert id="insertTrainResource" parameterType="TrainResource">
insert into train_resource
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="resourceId != null">resource_id,</if>
<if test="resourceName != null and resourceName != ''">resource_name,</if>
<if test="resourceType != null">resource_type,</if>
<if test="resourceUrl != null and resourceUrl != ''">resource_url,</if>
<if test="description != null">description,</if>
<if test="status != null">status,</if>
<if test="delFlag != null and delFlag != ''">del_flag,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null and updateBy != ''">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="resourceId != null">#{resourceId},</if>
<if test="resourceName != null and resourceName != ''">#{resourceName},</if>
<if test="resourceType != null">#{resourceType},</if>
<if test="resourceUrl != null and resourceUrl != ''">#{resourceUrl},</if>
<if test="description != null">#{description},</if>
<if test="status != null">#{status},</if>
<if test="delFlag != null and delFlag != ''">#{delFlag},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null and updateBy != ''">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateTrainResource" parameterType="TrainResource">
update train_resource
<trim prefix="SET" suffixOverrides=",">
<if test="resourceName != null and resourceName != ''">resource_name = #{resourceName},</if>
<if test="resourceType != null">resource_type = #{resourceType},</if>
<if test="resourceUrl != null and resourceUrl != ''">resource_url = #{resourceUrl},</if>
<if test="description != null">description = #{description},</if>
<if test="status != null">status = #{status},</if>
<if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where resource_id = #{resourceId} and del_flag = '0'
</update>
<update id="deleteTrainResourceByResourceId" parameterType="String">
update train_resource set del_flag = '2' where resource_id = #{resourceId}
</update>
<update id="deleteTrainResourceByResourceIds" parameterType="String">
update train_resource set del_flag = '2' where resource_id in
<foreach item="resourceId" collection="array" open="(" separator="," close=")">
#{resourceId}
</foreach>
</update>
</mapper>
+44
View File
@@ -0,0 +1,44 @@
import request from '@/utils/request'
// 查询教学资源列表
export function listResource(query) {
return request({
url: '/train/resource/list',
method: 'get',
params: query
})
}
// 查询教学资源详细
export function getResource(resourceId) {
return request({
url: '/train/resource/' + resourceId,
method: 'get'
})
}
// 新增教学资源
export function addResource(data) {
return request({
url: '/train/resource',
method: 'post',
data: data
})
}
// 修改教学资源
export function updateResource(data) {
return request({
url: '/train/resource/' + data.resourceId,
method: 'put',
data: data
})
}
// 删除教学资源
export function delResource(resourceId) {
return request({
url: '/train/resource/' + resourceId,
method: 'delete'
})
}
+383
View File
@@ -0,0 +1,383 @@
<template>
<div class="app-container">
<el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
<el-form-item label="资源名称" prop="resourceName">
<el-input
v-model="queryParams.resourceName"
placeholder="请输入资源名称"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="资源类型" prop="resourceType">
<el-select v-model="queryParams.resourceType" placeholder="资源类型" clearable>
<el-option
v-for="dict in resourceTypeOptions"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="状态" prop="status">
<el-select v-model="queryParams.status" placeholder="资源状态" clearable>
<el-option label="正常" :value="1" />
<el-option label="停用" :value="0" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
v-hasPermi="['train:resource:edit']"
type="primary"
plain
icon="el-icon-plus"
size="mini"
@click="handleAdd"
>新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button
v-hasPermi="['train:resource:edit']"
type="success"
plain
icon="el-icon-edit"
size="mini"
:disabled="single"
@click="handleUpdate"
>修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button
v-hasPermi="['train:resource:edit']"
type="danger"
plain
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
>删除</el-button>
</el-col>
<right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
</el-row>
<el-table v-loading="loading" :data="resourceList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="资源名称" align="center" prop="resourceName" min-width="160" :show-overflow-tooltip="true" />
<el-table-column label="资源类型" align="center" prop="resourceType">
<template slot-scope="scope">
<dict-tag :options="dict.type.train_resource_type" :value="scope.row.resourceType" />
</template>
</el-table-column>
<el-table-column label="资源文件/地址" align="center" prop="resourceUrl" min-width="200" :show-overflow-tooltip="true">
<template slot-scope="scope">
<el-link
v-if="scope.row.resourceType === 5"
:href="scope.row.resourceUrl"
type="primary"
target="_blank"
:underline="false"
>{{ scope.row.resourceUrl }}</el-link>
<el-link
v-else
:href="fileUrl(scope.row.resourceUrl)"
type="primary"
target="_blank"
:underline="false"
>{{ fileName(scope.row.resourceUrl) }}</el-link>
</template>
</el-table-column>
<el-table-column label="资源描述" align="center" prop="description" :show-overflow-tooltip="true" />
<el-table-column label="状态" align="center" prop="status">
<template slot-scope="scope">
<el-tag v-if="scope.row.status === 1" type="success">正常</el-tag>
<el-tag v-else type="danger">停用</el-tag>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="160">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime) }}</span>
</template>
</el-table-column>
<el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button
v-hasPermi="['train:resource:edit']"
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
>修改</el-button>
<el-button
v-hasPermi="['train:resource:edit']"
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
>删除</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"
/>
<!-- 添加或修改教学资源对话框 -->
<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
<el-form-item label="资源名称" prop="resourceName">
<el-input v-model="form.resourceName" placeholder="请输入资源名称" maxlength="150" />
</el-form-item>
<el-form-item label="资源类型" prop="resourceType">
<el-select v-model="form.resourceType" placeholder="请选择资源类型" style="width: 100%">
<el-option
v-for="dict in resourceTypeOptions"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item v-if="isExternalLink" label="外部链接" prop="resourceUrl">
<el-input v-model="form.resourceUrl" placeholder="请输入外部教学链接,以 http(s):// 开头" maxlength="500" />
</el-form-item>
<el-form-item v-else label="资源文件" prop="resourceUrl">
<file-upload
v-model="form.resourceUrl"
:limit="1"
:file-size="100"
:file-type="fileTypes"
/>
</el-form-item>
<el-form-item label="资源描述" prop="description">
<el-input v-model="form.description" type="textarea" :rows="3" maxlength="500" placeholder="请输入资源描述/使用说明" />
</el-form-item>
<el-form-item label="状态" prop="status">
<el-radio-group v-model="form.status">
<el-radio :label="1">正常</el-radio>
<el-radio :label="0">停用</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="500" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { listResource, getResource, addResource, updateResource, delResource } from '@/api/train/resource'
export default {
name: 'TrainResource',
dicts: ['train_resource_type'],
data() {
return {
// 遮罩层
loading: true,
// 选中数组
ids: [],
// 非单个禁用
single: true,
// 非多个禁用
multiple: true,
// 显示搜索条件
showSearch: true,
// 总条数
total: 0,
// 教学资源表格数据
resourceList: [],
// 弹出层标题
title: '',
// 是否显示弹出层
open: false,
// 允许上传的资源文件格式
fileTypes: ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt', 'zip', 'rar', '7z', 'mp4', 'avi', 'mov', 'wmv', 'mp3', 'png', 'jpg', 'jpeg', 'gif', 'glb', 'gltf', 'obj', 'fbx'],
// 查询参数
queryParams: {
pageNum: 1,
pageSize: 10,
resourceName: null,
resourceType: null,
status: null
},
// 表单参数
form: {},
// 表单校验
rules: {
resourceName: [
{ required: true, message: '资源名称不能为空', trigger: 'blur' }
],
resourceType: [
{ required: true, message: '资源类型不能为空', trigger: 'change' }
],
resourceUrl: [
{ required: true, message: '资源文件或外部链接不能为空', trigger: ['blur', 'change'] }
]
}
}
},
computed: {
// 资源类型下拉项:字典值为字符串,数据表字段为整型,需转成数字
resourceTypeOptions() {
return (this.dict.type.train_resource_type || []).map(item => ({
label: item.label,
value: /^\d+$/.test(String(item.value)) ? Number(item.value) : item.value
}))
},
// 资源类型为「外部教学链接」时不走上传,改为填写地址
isExternalLink() {
return Number(this.form.resourceType) === 5
}
},
watch: {
// 切换资源类型时清空已选资源地址,避免类型与地址不匹配
'form.resourceType'(newVal, oldVal) {
if (oldVal !== undefined && newVal !== oldVal && this.open) {
this.$set(this.form, 'resourceUrl', null)
this.$nextTick(() => {
if (this.$refs['form']) {
this.$refs['form'].clearValidate('resourceUrl')
}
})
}
}
},
created() {
this.getList()
},
methods: {
/** 查询教学资源列表 */
getList() {
this.loading = true
listResource(this.queryParams).then(response => {
this.resourceList = response.rows
this.total = response.total
this.loading = false
})
},
// 取消按钮
cancel() {
this.open = false
this.reset()
},
// 表单重置
reset() {
this.form = {
resourceId: null,
resourceName: null,
resourceType: 1,
resourceUrl: null,
description: null,
status: 1,
remark: null
}
this.resetForm('form')
},
/** 搜索按钮操作 */
handleQuery() {
this.queryParams.pageNum = 1
this.getList()
},
/** 重置按钮操作 */
resetQuery() {
this.resetForm('queryForm')
this.handleQuery()
},
// 多选框选中数据
handleSelectionChange(selection) {
this.ids = selection.map(item => item.resourceId)
this.single = selection.length !== 1
this.multiple = !selection.length
},
/** 新增按钮操作 */
handleAdd() {
this.reset()
this.open = true
this.title = '添加教学资源'
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset()
const resourceId = row.resourceId || this.ids
getResource(resourceId).then(response => {
// 资源类型为整型,防止后端返回字符串导致下拉/单选回显失败
this.form = {
...response.data,
resourceType: response.data.resourceType == null ? null : Number(response.data.resourceType),
status: response.data.status == null ? 1 : Number(response.data.status)
}
this.open = true
this.title = '修改教学资源'
})
},
/** 提交按钮 */
submitForm() {
this.$refs['form'].validate(valid => {
if (valid) {
if (this.form.resourceId != null) {
updateResource(this.form).then(response => {
this.$modal.msgSuccess('修改成功')
this.open = false
this.getList()
})
} else {
addResource(this.form).then(response => {
this.$modal.msgSuccess('新增成功')
this.open = false
this.getList()
})
}
}
})
},
/** 删除按钮操作 */
handleDelete(row) {
const ids = row.resourceId || this.ids
if (!ids || (Array.isArray(ids) && ids.length === 0)) {
this.$modal.msgWarning('请选择要删除的数据')
return
}
this.$modal.confirm('是否确认删除资源编号为"' + ids + '"的数据项?').then(function() {
return delResource(ids)
}).then(() => {
this.getList()
this.$modal.msgSuccess('删除成功')
}).catch(() => {})
},
// 资源文件访问地址
fileUrl(url) {
if (!url) {
return ''
}
if (/^(http|https):\/\//.test(url)) {
return url
}
return process.env.VUE_APP_BASE_API + url
},
// 资源文件名
fileName(url) {
if (!url) {
return ''
}
return url.substring(url.lastIndexOf('/') + 1)
}
}
}
</script>