|
- package com.iformall.controller;
-
- import cn.binarywang.wx.miniapp.api.WxMaService;
- import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
- import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
- import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
- import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
- import com.iformall.common.ErrorCode;
- import com.iformall.common.Result;
- import com.iformall.common.ResultData;
- import com.iformall.domain.po.WxAppinfo;
- import com.iformall.domain.po.WxAuthorizerInfo;
- import com.iformall.domain.po.WxTemplateMsg;
- import com.iformall.enums.EnumWxAuthorizationStatus;
- import com.iformall.service.WxAppinfoService;
- import com.iformall.service.WxAuthorizerInfoService;
- import com.iformall.service.WxTemplateMsgService;
- import com.iformall.service.wechat.FmOpenService;
- 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 org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
-
- /**
- * Stormeye Wu
- */
- @RestController
- @RequestMapping("/weappMsgTemplate")
- @Api(description = "微信第三方开发平台-小程序-消息模板设置")
- public class WechatWeappMsgTemplateController {
- private final Logger logger = LoggerFactory.getLogger(getClass());
- @Autowired
- private FmOpenService openService;
-
- @Autowired
- private WxAuthorizerInfoService authorizerInfoService;
-
- @Autowired
- private WxAppinfoService appinfoService;
-
- @Autowired
- private WxTemplateMsgService templateMsgService;
-
- @ApiOperation("获取小程序模板库标题列表")
- @GetMapping("getTmpLibList")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
- public ResultData getTmpLibList(String appId, Integer pageNum, Integer pageSize) {
- try {
- WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
- WxMaTemplateLibraryListResult ret = maService.getTemplateService().findTemplateLibraryList(pageNum, pageSize);
- return new ResultData(ret);
- } catch (WxErrorException e) {
- logger.error(e.getMessage());
- return new ResultData(Result.ERROR, e.getMessage());
- }
- }
-
- @ApiOperation("获取模板库某个模板标题下关键词库")
- @GetMapping("getTemplateLibraryKeywordList")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
- @ApiImplicitParam(name = "id", value = "模板标题id", dataType = "String", paramType = "query", required = true)})
- public ResultData findTemplateLibraryKeywordList(String appId, String id) {
- try {
- WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
- WxMaTemplateLibraryGetResult ret = maService.getTemplateService().findTemplateLibraryKeywordList(id);
- return new ResultData(ret);
- } catch (WxErrorException e) {
- logger.error(e.getMessage());
- return new ResultData(Result.ERROR, e.getMessage());
- }
-
- }
-
- @ApiOperation("组合模板并添加至帐号下的个人模板库")
- @PostMapping("addTemplate")
- public ResultData addTemplate(String appId, String id, List<Integer> keywordIdList) {
- WxAuthorizerInfo authorizerInfo = authorizerInfoService.getByAppId(appId);
- if(authorizerInfo == null) {
- return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
- }
- if(!authorizerInfo.getAuthorizationStatus().equals(EnumWxAuthorizationStatus.AUTHORIZED.getCode())) {
- return new ResultData(ErrorCode.WEAPP_APPID_NOT_AUTH);
- }
- try {
- WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
- WxMaTemplateAddResult ret = maService.getTemplateService().addTemplate(id, keywordIdList);
- authorizerInfo.setTemplateStatus(0);
- authorizerInfo.setTemplateTime(new Date());
- authorizerInfo.setUpdateTime(new Date());
- authorizerInfoService.updateTemplate(authorizerInfo);
- return new ResultData(ret);
- } catch (WxErrorException e) {
- logger.error(e.getMessage());
- authorizerInfo.setTemplateStatus(1);
- authorizerInfo.setTemplateTime(new Date());
- authorizerInfo.setUpdateTime(new Date());
- authorizerInfoService.updateTemplate(authorizerInfo);
- return new ResultData(Result.ERROR, e.getMessage());
- }
-
- }
-
- @ApiOperation("获取帐号下已存在的模板列表")
- @GetMapping("getTemplateList")
- @ApiImplicitParams({
- @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
- @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
- public ResultData findTemplateList(String appId, Integer pageNum, Integer pageSize) {
- try {
- WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
- if(pageNum > 0) {
- pageNum = pageNum - 1;
- }
- WxMaTemplateListResult ret = maService.getTemplateService().findTemplateList(pageNum, pageSize);
- logger.info(ret.toString());
- return new ResultData(ret);
- } catch (WxErrorException e) {
- logger.error(e.getMessage());
- return new ResultData(Result.ERROR, e.getMessage());
- }
-
- }
-
- @ApiOperation("删除帐号下的某个模板")
- @PostMapping("delTemplate")
- public ResultData delTemplate(@RequestBody Map<String, String> params) {
- String appId = params.get("appId");
- String templateId = params.get("templateId");
- if(StringUtils.isBlank(appId)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
- }
- if(StringUtils.isBlank(templateId)) {
- return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
- }
- // check app
- WxAppinfo appinfo = appinfoService.getByAppId(appId);
- if(appinfo == null) {
- String msg = "此appId未纳入管理,请联系管理员";
- logger.error(msg);
- return new ResultData(Result.ERROR, msg);
- }
- WxTemplateMsg msgQ = new WxTemplateMsg();
- msgQ.setTemplateId(templateId);
- msgQ.setTenantId(appinfo.getTenantId());
- WxTemplateMsg msg = templateMsgService.getByObj(msgQ);
- if(msg != null) {
- String msgStr = "此模板正被使用,请不要删除";
- logger.error(msgStr);
- return new ResultData(Result.ERROR, msgStr);
- }
- try {
- WxMaService maService = openService.getWxOpenComponentService().getWxMaServiceByAppid(appId);
- maService.getTemplateService().delTemplate(templateId);
- return new ResultData();
- } catch (WxErrorException e) {
- logger.error(e.getMessage());
- return new ResultData(Result.ERROR, e.getMessage());
- }
- }
- }
|