11 changed files with 394 additions and 6 deletions
@ -0,0 +1,48 @@ |
|||
package com.bnyer.common.core.domain; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.IdType; |
|||
import com.baomidou.mybatisplus.annotation.TableField; |
|||
import com.baomidou.mybatisplus.annotation.TableId; |
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.*; |
|||
|
|||
@ApiModel(value="com-bnyer-common-core-domain-CommonImgs") |
|||
@Getter |
|||
@Setter |
|||
@ToString |
|||
@AllArgsConstructor |
|||
@NoArgsConstructor |
|||
@TableName(value = "img_common_imgs") |
|||
public class CommonImgs extends BaseDomain { |
|||
/** |
|||
* 主键id |
|||
*/ |
|||
@TableId(value = "id", type = IdType.INPUT) |
|||
@ApiModelProperty(value="主键id") |
|||
private Long id; |
|||
|
|||
/** |
|||
* 图片地址 |
|||
*/ |
|||
@TableField(value = "img_url") |
|||
@ApiModelProperty(value="图片地址") |
|||
private String imgUrl; |
|||
|
|||
/** |
|||
* 是否显示 (0->隐藏;1->显示) |
|||
*/ |
|||
@TableField(value = "is_show") |
|||
@ApiModelProperty(value="是否显示 (0->隐藏;1->显示)") |
|||
private String isShow; |
|||
|
|||
/** |
|||
* 图片类型(0->头像;1->壁纸;2->插画) |
|||
*/ |
|||
@TableField(value = "type") |
|||
@ApiModelProperty(value="图片类型(0->头像;1->壁纸;2->插画)") |
|||
private String type; |
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.bnyer.common.core.dto; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Getter; |
|||
import lombok.Setter; |
|||
|
|||
import javax.validation.constraints.NotEmpty; |
|||
import java.io.Serializable; |
|||
|
|||
|
|||
@Getter |
|||
@Setter |
|||
@ApiModel("公共素材库接收类") |
|||
public class CommonImgsDto implements Serializable { |
|||
|
|||
@NotEmpty(message = "类型不能为空!") |
|||
@ApiModelProperty(value="类型") |
|||
private String type; |
|||
|
|||
@ApiModelProperty(value="第几页") |
|||
private String page; |
|||
|
|||
@ApiModelProperty(value="每页条数") |
|||
private String pageSize; |
|||
|
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
package com.bnyer.common.core.enums; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.Getter; |
|||
|
|||
/** |
|||
* @author chengkun |
|||
* @date 2023/03/11 15:18 |
|||
*/ |
|||
@Getter |
|||
@AllArgsConstructor |
|||
public enum CommonImgsTypeEnum { |
|||
|
|||
HEAD("0","头像"), |
|||
BACK("1","壁纸"), |
|||
ILLUSTRATION("2","插画"); |
|||
|
|||
private String type; |
|||
|
|||
private String msg; |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
package com.bnyer.system.config; |
|||
|
|||
import lombok.Getter; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.cloud.context.config.annotation.RefreshScope; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
/** |
|||
* pixaBay配置类 |
|||
* @author chengkun |
|||
* @date 2023/03/10 17:43 |
|||
*/ |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "bnyer.system.pixabay") |
|||
@Getter |
|||
@RefreshScope |
|||
public class PixaBayConfig { |
|||
|
|||
@Value("${bnyer.system.pixabay.apiKey}") |
|||
private String apiKey; |
|||
|
|||
@Value("${bnyer.system.pixabay.apiUrl}") |
|||
private String apiUrl; |
|||
|
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
package com.bnyer.system.controller; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.bnyer.common.core.dto.CommonImgsDto; |
|||
import com.bnyer.common.core.web.controller.BaseController; |
|||
import com.bnyer.common.core.web.domain.AjaxResult; |
|||
import com.bnyer.system.service.ICommonImgsService; |
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import io.swagger.annotations.ApiParam; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
@Api(value = "【图文平台】公共素材库接口",tags = "【图文平台】公共素材库接口") |
|||
@RestController |
|||
@RequestMapping("/img/commonImgs") |
|||
@Slf4j |
|||
public class CommonImgsController extends BaseController { |
|||
|
|||
@Autowired |
|||
private ICommonImgsService commonImgsService; |
|||
|
|||
//@RequiresPermissions("system:config:list")
|
|||
// @ApiOperation(value="查询commonImgs分页")
|
|||
// @PostMapping("/page")
|
|||
// public TableDataInfo pageCommonImgs(@RequestBody @ApiParam("分页对象") CommonImgsPageDto dto){
|
|||
// PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
|
|||
// List<CommonImgs> commonImgss = commonImgsService.queryPage(dto);
|
|||
// return getDataTable(commonImgss);
|
|||
// }
|
|||
|
|||
//@RequiresPermissions("system:config:list")
|
|||
@ApiOperation(value="新增commonImgs") |
|||
@PostMapping(value = "/importCommonImgs") |
|||
public AjaxResult importCommonImgs(@Validated @RequestBody @ApiParam("commonImgs对象")CommonImgsDto dto){ |
|||
log.debug("【图文平台后台】新增commonImgs参数为:{}", JSON.toJSONString(dto)); |
|||
return AjaxResult.success(commonImgsService.importCommonImgs(dto.getType(), dto.getPage(), dto.getPageSize())); |
|||
} |
|||
|
|||
//@RequiresPermissions("system:config:list")
|
|||
// @ApiOperation(value="修改commonImgs")
|
|||
// @PostMapping(value = "/update")
|
|||
// public AjaxResult updateCommonImgs(@Validated @RequestBody @ApiParam("commonImgs对象") CommonImgsDto dto){
|
|||
// log.debug("【图文平台后台】修改commonImgs参数为:{}", JSON.toJSONString(dto));
|
|||
// return AjaxResult.success(commonImgsService.update(dto.extractParam()));
|
|||
// }
|
|||
//
|
|||
// //@RequiresPermissions("system:config:list")
|
|||
// @ApiOperation(value="删除commonImgs")
|
|||
// @DeleteMapping(value = "/delete/{ids}")
|
|||
// public AjaxResult deleteCommonImgs(@PathVariable @ApiParam("主键ids") List<Long> ids){
|
|||
// log.debug("【图文平台后台】删除commonImgs参数为:{}", JSON.toJSONString(ids));
|
|||
// return AjaxResult.success(commonImgsService.delete(ids));
|
|||
// }
|
|||
//
|
|||
// //@RequiresPermissions("system:config:list")
|
|||
// @ApiOperation(value="查询commonImgs详情")
|
|||
// @GetMapping(value = "/details/{id}")
|
|||
// public AjaxResult detailsCommonImgs(@PathVariable @ApiParam("主键id") Long id){
|
|||
// log.debug("【图文平台后台】查询commonImgs详情参数为:{}", id);
|
|||
// return AjaxResult.success(commonImgsService.queryDetails(id));
|
|||
// }
|
|||
//
|
|||
// //@RequiresPermissions("system:config:list")
|
|||
// @ApiOperation(value="变更type显示状态")
|
|||
// @PostMapping(value = "/changeStatus")
|
|||
// public AjaxResult changeStatus(@Validated @RequestBody @ApiParam("type状态对象") StatusDto dto){
|
|||
// log.debug("【图文平台后台】变更type参数为:{}", JSON.toJSONString(dto));
|
|||
// return AjaxResult.success(commonImgsService.changeStatus(dto.getId(),dto.getStatus()));
|
|||
// }
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
package com.bnyer.system.mapper; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.bnyer.common.core.domain.CommonImgs; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.apache.ibatis.annotations.Param; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface CommonImgsMapper extends BaseMapper<CommonImgs> { |
|||
|
|||
/** |
|||
* 批量插入公开素材库 |
|||
* @param imgList 素材集合 |
|||
* @param type 素材类型 |
|||
* @return - |
|||
*/ |
|||
int insertBatch(@Param("imgList") List<String> imgList,@Param("type") String type); |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
package com.bnyer.system.service; |
|||
|
|||
import com.bnyer.common.core.domain.CommonImgs; |
|||
|
|||
import java.util.List; |
|||
|
|||
public interface ICommonImgsService { |
|||
|
|||
/** |
|||
* 新增commonImgs |
|||
* @param type 图片类型 |
|||
* @param page 第几页 |
|||
* @param pageSize 每页条数 |
|||
* @return - |
|||
*/ |
|||
int importCommonImgs(String type,String page,String pageSize); |
|||
|
|||
/** |
|||
* 批量删除commonImgs |
|||
* @param ids ids |
|||
* @return - |
|||
*/ |
|||
int delete(List<Long> ids); |
|||
|
|||
/** |
|||
* 查询commonImgs分页 |
|||
* @param dto 分页对象 |
|||
* @return - |
|||
*/ |
|||
List<CommonImgs> queryPage(); |
|||
|
|||
/** |
|||
* 查询commonImgs详情 |
|||
* @param id 主键id |
|||
* @return - |
|||
*/ |
|||
CommonImgs queryDetails(Long id); |
|||
|
|||
/** |
|||
* 变更显示状态 |
|||
* @param id 主键id |
|||
* @param status 状态 |
|||
* @return - |
|||
*/ |
|||
int changeStatus(Long id, String status); |
|||
|
|||
} |
|||
@ -0,0 +1,100 @@ |
|||
package com.bnyer.system.service.impl; |
|||
|
|||
import com.alibaba.fastjson.JSONArray; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|||
import com.bnyer.common.core.domain.CommonImgs; |
|||
import com.bnyer.common.core.enums.CommonImgsTypeEnum; |
|||
import com.bnyer.common.core.exception.ServiceException; |
|||
import com.bnyer.system.config.PixaBayConfig; |
|||
import com.bnyer.system.mapper.CommonImgsMapper; |
|||
import com.bnyer.system.service.ICommonImgsService; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
|
|||
@Service |
|||
@Slf4j |
|||
public class CommonImgsServiceImpl implements ICommonImgsService { |
|||
|
|||
@Autowired |
|||
private CommonImgsMapper commonImgsMapper; |
|||
|
|||
@Autowired |
|||
private PixaBayConfig pixaBayConfig; |
|||
|
|||
@Autowired |
|||
private RestTemplate restTemplate; |
|||
|
|||
@Override |
|||
@Transactional(rollbackFor = Exception.class) |
|||
public int importCommonImgs(String type,String page,String pageSize) { |
|||
//获取数据源
|
|||
String apiKey = pixaBayConfig.getApiKey(); |
|||
String apiUrl = pixaBayConfig.getApiUrl(); |
|||
String url = apiUrl+"?key="+apiKey+"&image_type="+type+"&page="+page+"&per_page="+pageSize; |
|||
|
|||
JSONObject resultObject = restTemplate.getForObject(url, JSONObject.class); |
|||
if(resultObject != null && resultObject.getJSONArray("hits") != null &&resultObject.getJSONArray("hits").size() < 0){ |
|||
throw new ServiceException("获取pixabay数据失败!"); |
|||
} |
|||
//获取数据成功
|
|||
JSONArray hits = resultObject.getJSONArray("hits"); |
|||
List<String> headUrlList = new ArrayList<>(); |
|||
List<String> backUrlList = new ArrayList<>(); |
|||
for (int i = 0; i < hits.size(); i++) { |
|||
JSONObject jsonObject = hits.getJSONObject(i); |
|||
if(jsonObject != null){ |
|||
//获取头像
|
|||
headUrlList.add(jsonObject.getString("userImageURL")); |
|||
//获取图片
|
|||
backUrlList.add(jsonObject.getString("webformatURL")); |
|||
} |
|||
} |
|||
if(type.equals(CommonImgsTypeEnum.HEAD.getType())){ |
|||
//批量入库头像
|
|||
return commonImgsMapper.insertBatch(headUrlList,CommonImgsTypeEnum.HEAD.getType()); |
|||
}else if(type.equals(CommonImgsTypeEnum.BACK.getType())){ |
|||
//批量入库头像
|
|||
return commonImgsMapper.insertBatch(headUrlList,CommonImgsTypeEnum.BACK.getType()); |
|||
}else if(type.equals(CommonImgsTypeEnum.ILLUSTRATION.getType())){ |
|||
//批量入库头像
|
|||
return commonImgsMapper.insertBatch(headUrlList,CommonImgsTypeEnum.ILLUSTRATION.getType()); |
|||
}else{ |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
|
|||
@Override |
|||
@Transactional(rollbackFor = Exception.class) |
|||
public int delete(List<Long> ids) { |
|||
return commonImgsMapper.deleteBatchIds(ids); |
|||
} |
|||
|
|||
@Override |
|||
public List<CommonImgs> queryPage() { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public CommonImgs queryDetails(Long id) { |
|||
return commonImgsMapper.selectById(id); |
|||
} |
|||
|
|||
@Override |
|||
@Transactional(rollbackFor = Exception.class) |
|||
public int changeStatus(Long id, String status) { |
|||
LambdaUpdateWrapper<CommonImgs> wrapper = new LambdaUpdateWrapper<>(); |
|||
wrapper.eq(CommonImgs::getId, id); |
|||
CommonImgs commonImgs = new CommonImgs(); |
|||
commonImgs.setIsShow(status); |
|||
return commonImgsMapper.update(commonImgs,wrapper); |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
<?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.bnyer.system.mapper.CommonImgsMapper"> |
|||
<resultMap id="BaseResultMap" type="com.bnyer.common.core.domain.CommonImgs"> |
|||
<!--@mbg.generated--> |
|||
<!--@Table img_common_imgs--> |
|||
<id column="id" jdbcType="BIGINT" property="id" /> |
|||
<result column="img_url" jdbcType="VARCHAR" property="imgUrl" /> |
|||
<result column="is_show" jdbcType="CHAR" property="isShow" /> |
|||
<result column="type" jdbcType="CHAR" property="type" /> |
|||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> |
|||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> |
|||
<result column="sort" jdbcType="INTEGER" property="sort" /> |
|||
</resultMap> |
|||
<sql id="Base_Column_List"> |
|||
<!--@mbg.generated--> |
|||
id, img_url, is_show, `type`, create_time, update_time, sort |
|||
</sql> |
|||
<insert id="insertBatch"> |
|||
insert into img_common_imgs(img_url,type) |
|||
<foreach collection="imgList" item="item" index="index" separator=","> |
|||
(#{item}, #{type}) |
|||
</foreach> |
|||
</insert> |
|||
</mapper> |
|||
Loading…
Reference in new issue