All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mingsoft.mweixin.action.FileAction Maven / Gradle / Ivy

There is a newer version: 2.1.19
Show newest version
package net.mingsoft.mweixin.action;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mweixin.bean.CategoryFileBean;
import net.mingsoft.mweixin.bean.FileBean;
import net.mingsoft.mweixin.biz.ICategoryBiz;
import net.mingsoft.mweixin.biz.IFileBiz;
import net.mingsoft.mweixin.constant.SessionConst;
import net.mingsoft.mweixin.entity.CategoryEntity;
import net.mingsoft.mweixin.entity.FileEntity;
import net.mingsoft.mweixin.entity.WeixinEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 微信文件表管理控制层
 * @author 铭飞开发团队
 * 创建日期:2018-12-29 9:18:11
* 历史修订:
*/ @Api(value = "微信文件图片接口") @Controller("wxFileAction") @RequestMapping("/${ms.manager.path}/mweixin/file") public class FileAction extends net.mingsoft.mweixin.action.BaseAction{ /** * 注入微信文件表业务层 */ @Autowired private IFileBiz fileBiz; /** * 注入微信文件表业务层 */ @Autowired private ICategoryBiz categoryBiz; /** * 查询微信文件表列表 * @param fileBean 微信文件表实体 * file参数包含字段信息参考:
* fileId 文件编号
* weixinId 微信编号
* isSync 是否同步至微信
*
返回

*
[
* {
* fileId: 文件编号
* weixinId: 微信编号
* isSync: 是否同步至微信
* }
* ]

*/ @ApiOperation(value="微信文章素材列表接口") @ApiImplicitParams({ @ApiImplicitParam(name = "categoryId", value = "文件分类编号", required =false,paramType="query"), @ApiImplicitParam(name = "fileName", value = "文件名称", required = false,paramType="query"), @ApiImplicitParam(name = "fileUrl", value = "文件链接", required =false,paramType="query"), @ApiImplicitParam(name = "fileSize", value = "文件大小", required =false,paramType="query"), @ApiImplicitParam(name = "fileType", value = "文件类型:图片、音频、视频等", required = false,paramType="query"), @ApiImplicitParam(name = "isChild", value = "子业务", required = false,paramType="query"), @ApiImplicitParam(name = "weixinId", value = "微信编号", required = false,paramType="query"), @ApiImplicitParam(name = "isSync", value = "是否同步至微信", required = false,paramType="query") }) @GetMapping("/list") @ResponseBody public ResultData list(@ModelAttribute FileBean fileBean, HttpServletResponse response, HttpServletRequest request) { WeixinEntity weixin = (WeixinEntity) BasicUtil.getSession(SessionConst.WEIXIN_SESSION); fileBean.setWeixinId(weixin.getWeixinId()); BasicUtil.startPage(); List fileList = fileBiz.query(fileBean); return ResultData.build().success(new EUListBean(fileList,(int)BasicUtil.endPage(fileList).getTotal())); } @ApiOperation(value="微信文章素材列表接口") @GetMapping("/categoryFile") @ResponseBody public ResultData categoryFile(FileBean fileBean,HttpServletResponse response, HttpServletRequest request) { //取出微信实体 WeixinEntity weixin = this.getWeixinSession(request); //若微信或者微信相关数据不存在 if(weixin == null || weixin.getWeixinId()<=0){ return ResultData.build().error(this.getResString("weixin.not.found")); } fileBean.setWeixinId(weixin.getWeixinId()); CategoryEntity category = new CategoryEntity(); category.setAppId(BasicUtil.getAppId()); //获取分组集合 List categorys = categoryBiz.query(category); //获取文件集合 List fileList = fileBiz.query(fileBean); List categoryFiles = new ArrayList(); for (CategoryEntity categoryEntity : categorys) { CategoryFileBean categoryFile = new CategoryFileBean(); List files = new ArrayList(); for (FileEntity wxFile : fileList) { if(ObjectUtil.isNotNull(wxFile) && ObjectUtil.isNotNull(wxFile.getCategoryId()) && StringUtils.isNotBlank(categoryEntity.getId())){ //判断文件分组 if(wxFile.getCategoryId().equals(Integer.parseInt(categoryEntity.getId()))){ files.add(wxFile); } } } //设置分组文件总数 categoryFile.setTotal(files.size()); categoryFile.setCategory(categoryEntity); categoryFile.setFiles(files); categoryFiles.add(categoryFile); } return ResultData.build().success(categoryFiles); } /** * 保存微信文件表实体 * @param file 微信文件表实体 * file参数包含字段信息参考:
* fileId 文件编号
* weixinId 微信编号
* isSync 是否同步至微信
*
返回

*
{
* fileId: 文件编号
* weixinId: 微信编号
* isSync: 是否同步至微信
* }

*/ @ApiOperation(value="微信文章素材保存接口") @ApiImplicitParams({ @ApiImplicitParam(name = "fileName", value = "文件名称", required = true,paramType="query"), @ApiImplicitParam(name = "weixinId", value = "微信编号", required = true,paramType="query"), @ApiImplicitParam(name = "categoryId", value = "文件分类编号", required =false,paramType="query"), @ApiImplicitParam(name = "fileUrl", value = "文件链接", required =false,paramType="query"), @ApiImplicitParam(name = "fileSize", value = "文件大小", required =false,paramType="query"), @ApiImplicitParam(name = "fileType", value = "文件类型:图片、音频、视频等", required = true,paramType="query"), @ApiImplicitParam(name = "isChild", value = "子业务", required = false,paramType="query"), }) @LogAnn(title = "微信文章素材保存接口",businessType= BusinessTypeEnum.INSERT) @PostMapping("/save") @ResponseBody @RequiresPermissions("picture:saveupdate") public ResultData save(@ModelAttribute FileEntity file, HttpServletResponse response, HttpServletRequest request,BindingResult result) { //取出微信实体 WeixinEntity weixin = this.getWeixinSession(request); //若微信或者微信相关数据不存在 if(weixin == null || weixin.getWeixinId()<=0){ return ResultData.build().error(this.getResString("weixin.not.found")); } file.setWeixinId(weixin.getWeixinId()); file.setCreateDate(new Date()); fileBiz.saveFile(file); return ResultData.build().success(file); } /** * 同步微信图片 * @param weixinNo * @param request * @param response */ @ApiOperation(value = "同步微信图片") @GetMapping("/syncImages") @RequiresPermissions("picture:sync") public ResultData weiXinNewsSyncLocal(@ApiIgnore String weixinNo,HttpServletRequest request,HttpServletResponse response){ try { //取出微信实体 WeixinEntity weixin = this.getWeixinSession(request); //若微信或者微信相关数据不存在 if(weixin == null || weixin.getWeixinId()<=0){ return ResultData.build().error(this.getResString("weixin.not.found")); } for(int offset = 0;;) { int count =offset+20; WxMpMaterialFileBatchGetResult imageBatchGetResult = this.builderWeixinService(weixin.getWeixinNo()).getMaterialService().materialFileBatchGet("image", offset, count); fileBiz.weiXinFileSyncLocal(imageBatchGetResult, weixin.getWeixinId()); offset = count; if (imageBatchGetResult.getItems().size()<=0){ break; } } } catch (WxErrorException e) { e.printStackTrace(); } return ResultData.build().success(); } /** * @param files 基础文件表实体 * file参数包含字段信息参考:
* id:多个id直接用逗号隔开,例如id=1,2,3,4 * 批量删除基础文件表 *
返回

*
{code:"错误编码",
* result:"true|false",
* resultMsg:"错误信息"
* }
*/ @LogAnn(title = "基础文件表实体",businessType= BusinessTypeEnum.DELETE) @PostMapping("/delete") @ResponseBody @RequiresPermissions("picture:del") public ResultData delete(@RequestBody List files,HttpServletResponse response, HttpServletRequest request) { int[] ids = new int[files.size()]; for(int i = 0;i0){ fileBiz.delete(ids); } return ResultData.build().success(); } /** * 更新基础文件表信息基础文件表 * @param file 基础文件表实体 * file参数包含字段信息参考:
* id 文件编号
* categoryId 文件分类编号
* appId APP编号
* fileName 文件名称
* fileUrl 文件链接
* fileSize 文件大小
* fileJson 文件详情Json数据
* fileType 文件类型:图片、音频、视频等
* isChild 子业务
* updateDate 更新时间
* updateBy 更新者
* createBy 创建者
* createDate 创建时间
* del 删除标记
*
返回

*
{
* id: 文件编号
* categoryId: 文件分类编号
* appId: APP编号
* fileName: 文件名称
* fileUrl: 文件链接
* fileSize: 文件大小
* fileJson: 文件详情Json数据
* fileType: 文件类型:图片、音频、视频等
* isChild: 子业务
* updateDate: 更新时间
* updateBy: 更新者
* createBy: 创建者
* createDate: 创建时间
* del: 删除标记
* }

*/ @ApiOperation(value="微信文章素材保存接口") @ApiImplicitParams({ @ApiImplicitParam(name = "fileName", value = "文件名称", required = true,paramType="query"), @ApiImplicitParam(name = "weixinId", value = "微信编号", required = true,paramType="query"), @ApiImplicitParam(name = "isSync", value = "是否同步至微信", required = true,paramType="query"), @ApiImplicitParam(name = "categoryId", value = "文件分类编号", required =false,paramType="query"), @ApiImplicitParam(name = "fileUrl", value = "文件链接", required =false,paramType="query"), @ApiImplicitParam(name = "fileSize", value = "文件大小", required =false,paramType="query"), @ApiImplicitParam(name = "fileType", value = "文件类型:图片、音频、视频等", required = true,paramType="query"), @ApiImplicitParam(name = "isChild", value = "子业务", required = false,paramType="query"), }) @LogAnn(title = "微信文章素材保存接口",businessType= BusinessTypeEnum.UPDATE) @PostMapping("/update") @ResponseBody @RequiresPermissions("picture:saveupdate") public ResultData update(@ModelAttribute FileEntity file, HttpServletResponse response, HttpServletRequest request) { //验证文件编号的值是否合法 if(StringUtil.isBlank(file.getId())){ return ResultData.build().success(getResString("err.empty", this.getResString("file.id"))); } if(!StringUtil.checkLength(file.getId()+"", 1, 11)){ return ResultData.build().error(getResString("err.length", this.getResString("file.id"), "1", "11")); } if(!StringUtil.checkLength(file.getCategoryId()+"", 1, 11)){ return ResultData.build().error(getResString("err.length", this.getResString("category.id"), "1", "11")); } //验证文件名称的值是否合法 if(StringUtil.isBlank(file.getFileName())){ return ResultData.build().success(getResString("err.empty", this.getResString("file.name"))); } if(!StringUtil.checkLength(file.getFileName()+"", 1, 200)){ return ResultData.build().error(getResString("err.length", this.getResString("file.name"), "1", "200")); } if(!StringUtil.checkLength(file.getFileSize()+"", 1, 11)){ return ResultData.build().error(getResString("err.length", this.getResString("file.size"), "1", "11")); } file.setUpdateDate(new Date()); fileBiz.updateFile(file); return ResultData.build().success(file); } /** * 移动分组 * @param files 微信文件表实体 * file参数包含字段信息参考:
* fileId 文件编号
* weixinId 微信编号
* isSync 是否同步至微信
*
返回

*
{
* fileId: 文件编号
* weixinId: 微信编号
* isSync: 是否同步至微信
* }

*/ @ApiOperation(value="微信文章素材保存接口") @ApiImplicitParam(name = "categoryId", value = "文件分类编号", required =true,paramType="path") @PostMapping("/{categoryId}/mobileCategory") @ResponseBody @RequiresPermissions("file:mobileCategory") public ResultData mobileCategory(@RequestBody List files,@PathVariable @ApiIgnore int categoryId, HttpServletResponse response, HttpServletRequest request,BindingResult result) { for (FileEntity file : files) { file.setCategoryId(categoryId); fileBiz.updateFile(file); } return ResultData.build().success(); } /** * 根据素材Id获取素材实体 * @param response * @param mode */ @ApiOperation(value = "根据素材实体") @GetMapping("/get") @ResponseBody public ResultData get(@ModelAttribute FileEntity file,HttpServletResponse response,@ApiIgnore ModelMap mode){ if(ObjectUtil.isNull(file)){ return ResultData.build().error(); } if(file.getFileId()<=0){ return ResultData.build().error(); } //根据素材ID获取相应素材 file = (FileEntity)fileBiz.getEntity(file.getFileId()); return ResultData.build().success(file); } /** * 同步微信语音 * @param request * @param response */ @ApiOperation(value = "同步微信语音") @GetMapping("/syncVoice") @ResponseBody public ResultData weiXinVoiceSyncLocal(@ModelAttribute FileEntity fileBean,HttpServletResponse response, HttpServletRequest request) { //取出微信实体 WeixinEntity weixin = this.getWeixinSession(request); //若微信或者微信相关数据不存在 if(weixin == null || weixin.getWeixinId()<=0){ return ResultData.build().error(this.getResString("weixin.not.found")); } WxMpMaterialFileBatchGetResult voiceBatchGetResult = null; try { voiceBatchGetResult = this.builderWeixinService(weixin.getWeixinNo()).getMaterialService().materialFileBatchGet("voice", 0, 20); } catch (WxErrorException e) { e.printStackTrace(); } fileBiz.weiXinVoiceVideoSyncLocal(voiceBatchGetResult,fileBean,weixin); return ResultData.build().success(); } /** * 同步微信视频 * @param request * @param response */ @ApiOperation(value = "同步微信视频") @GetMapping("/syncVideo") @ResponseBody public ResultData weiXinVideoSyncLocal(@ModelAttribute FileEntity fileBean,HttpServletResponse response, HttpServletRequest request) { //取出微信实体 WeixinEntity weixin = this.getWeixinSession(request); //若微信或者微信相关数据不存在 if(weixin == null || weixin.getWeixinId()<=0){ return ResultData.build().error(this.getResString("weixin.not.found")); } WxMpMaterialFileBatchGetResult voiceBatchGetResult = null; try { voiceBatchGetResult = this.builderWeixinService(weixin.getWeixinNo()).getMaterialService().materialFileBatchGet("video", 0, 20); } catch (WxErrorException e) { e.printStackTrace(); } fileBiz.weiXinVoiceVideoSyncLocal(voiceBatchGetResult,fileBean,weixin); return ResultData.build().success(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy