|
@@ -2,10 +2,7 @@ package com.liangjian11.wx.mp.controller;
|
|
|
|
|
|
import com.liangjian11.wx.mp.modle.Material;
|
|
|
import com.liangjian11.wx.mp.service.MediaMaterialService;
|
|
|
-import com.liangjian11.wx.mp.utils.FileUtil;
|
|
|
-import com.liangjian11.wx.mp.utils.FiltersData;
|
|
|
-import com.liangjian11.wx.mp.utils.ResultInfo;
|
|
|
-import com.liangjian11.wx.mp.utils.ResultUtil;
|
|
|
+import com.liangjian11.wx.mp.utils.*;
|
|
|
import me.chanjar.weixin.common.api.WxConsts;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
@@ -29,37 +26,46 @@ import java.util.Map;
|
|
|
@RequestMapping("/wx/material/{appid}")
|
|
|
public class WxMaterialController {
|
|
|
|
|
|
- private Map<Integer,String> mediaTypeMap;
|
|
|
+
|
|
|
private final MediaMaterialService mediaMaterialService;
|
|
|
private final WxMpService wxMpService;
|
|
|
|
|
|
-
|
|
|
@Autowired
|
|
|
public WxMaterialController(MediaMaterialService mediaMaterialService,WxMpService wxMpService){
|
|
|
this.mediaMaterialService=mediaMaterialService;
|
|
|
this.wxMpService=wxMpService;
|
|
|
- mediaTypeMap=new HashMap<>();
|
|
|
- mediaTypeMap.put(1,"image");
|
|
|
- mediaTypeMap.put(2,"voice");
|
|
|
- mediaTypeMap.put(3,"video");
|
|
|
- mediaTypeMap.put(4,"thumb");
|
|
|
}
|
|
|
|
|
|
|
|
|
@CrossOrigin
|
|
|
@PostMapping("/getPageList")
|
|
|
- public ResultInfo getMaterialListOfPage(@RequestBody FiltersData filtersData) {
|
|
|
+ public ResultInfo getMaterialListOfPage(@PathVariable String appid,@RequestBody FiltersData filtersData) throws WxErrorException {
|
|
|
String filterSql = " Where 1=1";
|
|
|
- Integer start = 0;
|
|
|
- Integer end = 20;
|
|
|
if (filtersData != null && !StringUtils.isEmpty(filtersData.getFilterString())) {
|
|
|
filterSql = filtersData.getFilterString();
|
|
|
}
|
|
|
- if (filtersData.getPageIndex() > 0) {
|
|
|
- start = (filtersData.getPageIndex() - 1) * filtersData.getPageSize();
|
|
|
+ if (filtersData.getPageIndex() < 1) {
|
|
|
+ return ResultUtil.createFail(new Exception("缺少pageIndex!"));
|
|
|
}
|
|
|
- if (filtersData.getPageSize() > 0) {
|
|
|
- end = filtersData.getPageIndex() * filtersData.getPageSize() + 1;
|
|
|
+ if (filtersData.getPageSize() < 1) {
|
|
|
+ return ResultUtil.createFail(new Exception("缺少pageSize!"));
|
|
|
+ }
|
|
|
+ Integer start = (filtersData.getPageIndex() - 1) * filtersData.getPageSize();;
|
|
|
+ Integer end = filtersData.getPageIndex() * filtersData.getPageSize() + 1;
|
|
|
+
|
|
|
+ boolean hasType=false;
|
|
|
+ int type=0;
|
|
|
+ if(filtersData.getPageIndex()==1 && filtersData.getFilters()!=null){
|
|
|
+ for(FiltersUtils filtersUtils:filtersData.getFilters()){
|
|
|
+ if(filtersUtils.getField().equals("type")){
|
|
|
+ hasType=true;
|
|
|
+ type=Integer.valueOf(filtersUtils.getValue()).intValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!hasType){
|
|
|
+ return ResultUtil.createFail(new Exception("缺少素材类型!"));
|
|
|
+ }
|
|
|
+ mediaMaterialService.refreshMaterialFileBatch(appid,type);
|
|
|
}
|
|
|
List<Material> materialList = mediaMaterialService.getMaterialListOfPage(filterSql, start, end);
|
|
|
int totalCount = 0;
|
|
@@ -75,20 +81,21 @@ public class WxMaterialController {
|
|
|
@CrossOrigin
|
|
|
@PostMapping("/addMaterial")
|
|
|
public ResultInfo addMaterial(@PathVariable String appid, @RequestPart("file") MultipartFile multipartFile,
|
|
|
- @RequestParam("mediaType") Integer mediaType , @RequestParam("title") String title) throws IOException, WxErrorException {
|
|
|
+ @RequestParam("mediaType") Short mediaType , @RequestParam(value = "title",required=false)
|
|
|
+ String title) throws IOException, WxErrorException {
|
|
|
WxMpMaterial wxMaterial = new WxMpMaterial();
|
|
|
wxMaterial.setFile(FileUtil.multipartFileToFile(multipartFile));
|
|
|
wxMaterial.setName(StringUtils.isEmpty(multipartFile.getOriginalFilename())?multipartFile.getName():multipartFile.getOriginalFilename());
|
|
|
- if (WxConsts.MediaFileType.VIDEO.equals(mediaTypeMap.get(mediaType))) {
|
|
|
+ if (WxConsts.MediaFileType.VIDEO.equals(mediaMaterialService.getMediaTypeMap().get(mediaType))) {
|
|
|
wxMaterial.setVideoTitle(title);
|
|
|
wxMaterial.setVideoIntroduction(null);
|
|
|
}
|
|
|
- WxMpMaterialUploadResult wxResult= this.wxMpService.switchoverTo(appid).getMaterialService().materialFileUpload(mediaTypeMap.get(mediaType), wxMaterial);
|
|
|
+ WxMpMaterialUploadResult wxResult= this.wxMpService.switchoverTo(appid).getMaterialService().materialFileUpload(mediaMaterialService.getMediaTypeMap().get(mediaType), wxMaterial);
|
|
|
if(wxResult.getErrCode()!=null&&wxResult.getErrCode()>200){
|
|
|
return new ResultInfo(0,wxResult.getErrCode(),wxResult.getErrMsg());
|
|
|
}
|
|
|
Material material=new Material();
|
|
|
- material.setType(mediaType);
|
|
|
+ material.setMaterialType(mediaType);
|
|
|
material.setMediaId(wxResult.getMediaId());
|
|
|
String fileName= StringUtils.isEmpty(multipartFile.getOriginalFilename())?multipartFile.getName():multipartFile.getOriginalFilename();
|
|
|
String[] names=fileName.split("\\.");
|
|
@@ -98,7 +105,7 @@ public class WxMaterialController {
|
|
|
material.setCreateTime(new Date());
|
|
|
material.setAppId(appid);
|
|
|
material.setUrl(wxResult.getUrl());
|
|
|
- mediaMaterialService.insert(material);
|
|
|
+ mediaMaterialService.insertItem(material);
|
|
|
return ResultUtil.createSuccess(200,null);
|
|
|
}
|
|
|
|