提交 22b3d120 编写于 作者: 1 18356073052

素材管理

上级 c7c176c6
package com.linkwechat.web.controller.wecom;
import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.wecom.domain.WeMaterial;
import com.linkwechat.wecom.domain.vo.WeMaterialFileVO;
import com.linkwechat.wecom.service.IWeMaterialService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 企业微信素材Controller
*
* @author KEWEN
* @date 2020-10-08
*/
public class WeMaterialController extends BaseController {
@Autowired
private IWeMaterialService materialService;
/**
* 查询素材列表
*/
@PreAuthorize("@ss.hasPermi('wecom:material:list')")
@GetMapping("/list")
public TableDataInfo list(@RequestParam(value = "categoryId", required = false) String categoryId, @RequestParam(value = "search", required = false) String search) {
startPage();
List<WeMaterial> list = materialService.findWeMaterials(categoryId, search);
return getDataTable(list);
}
/**
* 查询素材详细信息
*/
@PreAuthorize("@ss.hasPermi('wechat:material:query')")
@GetMapping(value = "/{id}")
@ApiOperation("查询素材详细信息")
public AjaxResult getInfo(@PathVariable("id") Long id) {
return AjaxResult.success(materialService.findWeMaterialById(id));
}
/**
* 添加素材信息
*/
@PreAuthorize("@ss.hasPermi('wechat:material:add')")
@Log(title = "添加素材信息", businessType = BusinessType.INSERT)
@PostMapping
@ApiOperation("添加素材信息")
public AjaxResult add(@RequestBody WeMaterial material) {
return toAjax(materialService.insertWeMaterial(material));
}
/**
* 更新素材信息
*/
@PreAuthorize("@ss.hasPermi('wechat:material:edit')")
@Log(title = "更新素材信息", businessType = BusinessType.UPDATE)
@PutMapping
@ApiOperation("更新素材信息")
public AjaxResult edit(@RequestBody WeMaterial material) {
return toAjax(materialService.updateWeMaterial(material));
}
/**
* 删除素材信息
*/
@PreAuthorize("@ss.hasPermi('wechat:material:remove')")
@Log(title = "删除素材信息", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
@ApiOperation("删除素材信息")
public AjaxResult remove(@PathVariable Long[] ids) {
return toAjax(materialService.deleteWeMaterialByIds(ids));
}
/**
* 上传素材信息
*/
@PreAuthorize("@ss.hasPermi('wechat:material:upload')")
@Log(title = "上传素材信息", businessType = BusinessType.OTHER)
@PostMapping
@ApiOperation("上传素材信息")
public AjaxResult upload(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "type") String type) {
WeMaterialFileVO weMaterialFileVO = materialService.uploadWeMaterialFile(file, type);
return AjaxResult.success(weMaterialFileVO);
}
}
......@@ -22,6 +22,10 @@
<groupId>com.linkwechat</groupId>
<artifactId>linkwe-common</artifactId>
</dependency>
<dependency>
<groupId>com.linkwechat</groupId>
<artifactId>linkwe-framework</artifactId>
</dependency>
</dependencies>
......
package com.linkwechat.wecom.domain;
import com.linkwechat.common.core.domain.BaseEntity;
import com.linkwechat.common.utils.SnowFlakeUtil;
import lombok.Data;
/**
* @description: 企业微信上传临时素材实体
* @author: KEWEN
* @create: 2020-09-21 21:17
**/
@Data
public class WeMaterial extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long id = SnowFlakeUtil.nextId();
/**
* 分类id
*/
private Long categoryId;
/**
* 本地资源文件地址
*/
private String materialUrl;
/**
* 文本内容、图片文案
*/
private String content;
/**
* 图片名称
*/
private String materialName;
/**
* 摘要
*/
private String digest;
/**
* 媒体id
*/
private String materialMediaId;
/**
* 媒体文件上传时间戳
*/
private String materialCreatedAt;
/**
* 视频封面媒体id
*/
private String coverMediaId;
/**
* 视频封面媒体文件上传时间戳
*/
private String coverCreatedAt;
/**
* 封面本地资源文件
*/
private String coverUrl;
}
package com.linkwechat.wecom.domain.dto;
import lombok.Data;
/**
* @description: 企业微信上传临时素材传输实体
* @author: KEWEN
* @create: 2020-09-21 21:17
**/
@Data
public class WeMaterialDto extends WeResultDto {
/**
* 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
*/
private String type;
/**
* 媒体文件上传后获取的唯一标识,3天内有效
*/
private String media_id;
/**
* 媒体文件上传时间戳
*/
private String created_at;
}
package com.linkwechat.wecom.domain.vo;
import lombok.Builder;
import lombok.Data;
/**
* 素材文件信息
*/
@Builder
@Data
public class WeMaterialFileVO {
/**
* 素材名称
*/
private String materialName;
/**
* 本地资源文件地址
*/
private String materialUrl;
/**
* 媒体id
*/
private String materialMediaId;
/**
* 媒体文件上传时间戳
*/
private Long materialCreatedAt;
}
package com.linkwechat.wecom.mapper;
import com.linkwechat.wecom.domain.WeMaterial;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 素材mapper
*
* @author KEWEN
* @date 2020-10-09
*/
public interface WeMaterialMapper {
/**
* 添加素材信息
*
* @param WeMaterial 素材信息
* @return
*/
int insertWeMaterial(WeMaterial WeMaterial);
/**
* 删除素材信息
*
* @param id 主键id
* @return {@link int}
*/
int deleteWeMaterialById(Long id);
/**
* 批量删除
*
* @param ids id列表
* @return {@link int}
*/
int deleteWeMaterialByIds(Long[] ids);
/**
* 更新素材信息
*
* @param WeMaterial
* @return
*/
int updateWeMaterial(WeMaterial WeMaterial);
/**
* 查询素材详细信息
*
* @param id id
* @return {@link WeMaterial}
*/
WeMaterial findWeMaterialById(Long id);
/**
* 查询素材列表
*
* @param categoryId 类目id
* @param search 搜索值
* @return {@link WeMaterial}s
*/
List<WeMaterial> findWeMaterials(@Param("categoryId") String categoryId, @Param("search") String search);
}
package com.linkwechat.wecom.service;
import com.linkwechat.wecom.domain.WeMaterial;
import com.linkwechat.wecom.domain.vo.WeMaterialFileVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 素材service
*
* @author KEWEN
* @date 2020-10-08
*/
public interface IWeMaterialService {
/**
* 上传素材信息
*
* @param file 文件
* @param type 0 图片(image)、1 语音(voice)、2 视频(video),3 普通文件(file)
* @return {@link WeMaterialFileVO}
*/
WeMaterialFileVO uploadWeMaterialFile(MultipartFile file, String type);
/**
* 添加素材信息
*
* @param WeMaterial 素材信息
* @return
*/
int insertWeMaterial(WeMaterial WeMaterial);
/**
* 删除素材信息
*
* @param id 主键id
* @return {@link int}
*/
int deleteWeMaterialById(Long id);
/**
* 批量删除
*
* @param ids id列表
* @return {@link int}
*/
int deleteWeMaterialByIds(Long[] ids);
/**
* 更新素材信息
*
* @param WeMaterial
* @return
*/
int updateWeMaterial(WeMaterial WeMaterial);
/**
* 查询素材详细信息
*
* @param id id
* @return {@link WeMaterial}
*/
WeMaterial findWeMaterialById(Long id);
/**
* 查询素材列表
*
* @param categoryId 类目id
* @param search 搜索值
* @return {@link WeMaterial}s
*/
List<WeMaterial> findWeMaterials(String categoryId, String search);
}
package com.linkwechat.wecom.service.impl;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.MediaType;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.file.FileUploadUtils;
import com.linkwechat.framework.config.ServerConfig;
import com.linkwechat.wecom.client.WeMediaClient;
import com.linkwechat.wecom.domain.WeMaterial;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.vo.WeMaterialFileVO;
import com.linkwechat.wecom.mapper.WeMaterialMapper;
import com.linkwechat.wecom.service.IWeMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Optional;
/**
* 素材service
*
* @author KEWEN
* @date 2020-10-08
*/
@Slf4j
@Service
public class WeMaterialServiceImpl implements IWeMaterialService {
@Autowired
private WeMediaClient weMediaClient;
@Autowired
private WeMaterialMapper weMaterialMapper;
@Autowired
private ServerConfig serverConfig;
@Override
public WeMaterialFileVO uploadWeMaterialFile(MultipartFile file, String type) {
if (null == file) {
throw new WeComException("文件为空!");
}
try {
//上传文件到文件服务器
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
//上传临时素材
Optional<MediaType> mediaType = MediaType.of(type);
if (!mediaType.isPresent()) {
throw new WeComException("媒体类型出错!");
}
WeMediaDto mediaDto = weMediaClient.upload(file, mediaType.get().getMediaType());
if (!WeConstans.WE_SUCCESS_CODE.equals(mediaDto.getErrcode())) {
throw new WeComException("临时素材上传失败!");
}
//构造返回结果
WeMaterialFileVO weMaterialFileVO
= WeMaterialFileVO.builder().materialUrl(url).materialMediaId(mediaDto.getMedia_id())
.materialCreatedAt(mediaDto.getCreated_at()).materialName(file.getOriginalFilename()).build();
return weMaterialFileVO;
} catch (Exception e) {
throw new WeComException("临时素材上传失败!");
}
}
@Override
public int insertWeMaterial(WeMaterial weMaterial) {
return weMaterialMapper.insertWeMaterial(weMaterial);
}
@Override
public int deleteWeMaterialById(Long id) {
return weMaterialMapper.deleteWeMaterialById(id);
}
@Override
public int deleteWeMaterialByIds(Long[] ids) {
return weMaterialMapper.deleteWeMaterialByIds(ids);
}
@Override
public int updateWeMaterial(WeMaterial weMaterial) {
return weMaterialMapper.updateWeMaterial(weMaterial);
}
@Override
public WeMaterial findWeMaterialById(Long id) {
return weMaterialMapper.findWeMaterialById(id);
}
@Override
public List<WeMaterial> findWeMaterials(String categoryId, String search) {
return weMaterialMapper.findWeMaterials(categoryId, search);
}
}
<?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.linkwechat.wecom.mapper.WeMaterialMapper">
<resultMap id="materialMap" type="com.linkwechat.wecom.domain.WeMaterial">
<id property="id" column="id"/>
<result property="categoryId" column="category_id"/>
<result property="materialUrl" column="material_url"/>
<result property="content" column="content"/>
<result property="materialName" column="material_name"/>
<result property="digest" column="digest"/>
<result property="materialMediaId" column="material_media_id"/>
<result property="materialCreatedAt" column="material_created_at"/>
<result property="coverMediaId" column="cover_media_id"/>
<result property="coverCreatedAt" column="cover_created_at"/>
<result property="coverUrl" column="cover_url"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
</resultMap>
<insert id="insertWeMaterial">
INSERT INTO
we_material
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">
id,
</if>
<if test="categoryId!=null">
category_id,
</if>
<if test="materialUrl!=null">
material_url,
</if>
<if test="content!=null">
content,
</if>
<if test="materialName!=null">
material_name,
</if>
<if test="digest!=null">
digest,
</if>
<if test="materialMediaId!=null">
material_media_id,
</if>
<if test="materialCreatedAt!=null">
material_created_at,
</if>
<if test="coverMediaId!=null">
cover_media_id,
</if>
<if test="coverCreatedAt!=null">
cover_created_at,
</if>
<if test="coverUrl!=null">
cover_url,
</if>
<if test="createBy!=null">
create_by,
</if>
<if test="createTime!=null">
create_time,
</if>
<if test="updateBy!=null">
update_by,
</if>
<if test="updateTime!=null">
update_time
</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="categoryId!=null">
#{categoryId},
</if>
<if test="materialUrl!=null">
#{materialUrl},
</if>
<if test="content!=null">
#{content},
</if>
<if test="materialName!=null">
#{materialName},
</if>
<if test="digest!=null">
#{digest},
</if>
<if test="materialMediaId!=null">
#{materialMediaId},
</if>
<if test="materialCreatedAt!=null">
#{materialCreatedAt},
</if>
<if test="coverMediaId!=null">
#{coverMediaId},
</if>
<if test="coverCreatedAt!=null">
#{coverCreatedAt},
</if>
<if test="coverUrl!=null">
#{coverUrl},
</if>
<if test="createBy!=null">
#{createBy},
</if>
<if test="createTime!=null">
#{createTime},
</if>
<if test="updateBy!=null">
#{updateBy},
</if>
<if test="updateTime!=null">
#{updateTime}
</if>
</trim>
</insert>
<update id="updateWeMaterial">
update we_material set
<if test="id!=null">
id=#{id},
</if>
<if test="categoryId!=null">
category_id=#{categoryId},
</if>
<if test="materialUrl!=null">
material_url=#{materialUrl},
</if>
<if test="content!=null">
content=#{content},
</if>
<if test="materialName!=null">
material_name=#{materialName},
</if>
<if test="digest!=null">
digest=#{digest},
</if>
<if test="materialMediaId!=null">
material_media_id=#{materialMediaId},
</if>
<if test="materialCreatedAt!=null">
material_created_at=#{materialCreatedAt},
</if>
<if test="coverMediaId!=null">
cover_media_id=#{coverMediaId},
</if>
<if test="coverCreatedAt!=null">
cover_created_at=#{coverCreatedAt},
</if>
<if test="coverUrl!=null">
cover_url=#{coverUrl},
</if>
<if test="createBy!=null">
create_by=#{createBy},
</if>
<if test="createTime!=null">
create_time=#{createTime},
</if>
<if test="updateBy!=null">
update_by=#{updateBy},
</if>
<if test="updateTime!=null">
update_time=#{updateTime}
</if>
</update>
<delete id="deleteWeMaterialById">
UPDATE we_material
set del_flag=1
<where>
id=#{id}
</where>
</delete>
<delete id="deleteWeMaterialByIds">
UPDATE we_material
set del_flag=1
<where>
id in
<foreach collection="array" open="(" close=")" item="item" separator=",">
#{item}
</foreach>
</where>
</delete>
<select id="findWeMaterialById" resultMap="materialMap">
SELECT
<include refid="material"/>
FROM we_material
<where>
del_flag=0 and id=#{id}
</where>
</select>
<select id="findWeMaterials" resultMap="materialMap">
SELECT
<include refid="material"/>
FROM we_material
<where>
del_flag=0
<if test="categoryId!=null and categoryId!=''">
AND category_id=#{categoryId}
</if>
<if test="search!=null and search!=''">
AND (material_name LIKE CONCAT('%',#{search},'%') OR content LIKE CONCAT('%',#{search},'%'))
</if>
</where>
</select>
<sql id="material">
id,category_id,material_url,content,material_name,digest,material_media_id,material_created_at,cover_media_id,cover_created_at,cover_url,create_by,create_time,update_by,update_time
</sql>
</mapper>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册