mp:完成的公众号的管理界面

This commit is contained in:
YunaiV 2023-01-07 18:31:04 +08:00
parent 71beeabe9c
commit 39b2afd506
17 changed files with 294 additions and 206 deletions

View File

@ -9,9 +9,12 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
*/ */
public interface ErrorCodeConstants { public interface ErrorCodeConstants {
// ========== 用户相关 1004001000============ // ========== 公众号账号 1006000000============
ErrorCode WX_ACCOUNT_NOT_EXISTS = new ErrorCode(1006001000, "公众号账户不存在"); ErrorCode ACCOUNT_NOT_EXISTS = new ErrorCode(1006000000, "公众号账号不存在");
ErrorCode WX_ACCOUNT_FANS_NOT_EXISTS = new ErrorCode(1006001001, "粉丝账号不存在"); ErrorCode ACCOUNT_GENERATE_QR_CODE_FAIL = new ErrorCode(1006000001, "生成公众号二维码失败,原因:{}");
ErrorCode ACCOUNT_CLEAR_QUOTA_FAIL = new ErrorCode(1006000001, "清空公众号的 API 配额失败,原因:{}");
// TODO 要处理下
ErrorCode COMMON_NOT_EXISTS = new ErrorCode(1006001002, "用户不存在"); ErrorCode COMMON_NOT_EXISTS = new ErrorCode(1006001002, "用户不存在");
} }

View File

@ -2,7 +2,10 @@ package cn.iocoder.yudao.module.mp.controller.admin.account;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.*; import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountCreateReqVO;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountPageReqVO;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountRespVO;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountUpdateReqVO;
import cn.iocoder.yudao.module.mp.convert.account.MpAccountConvert; import cn.iocoder.yudao.module.mp.convert.account.MpAccountConvert;
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO; import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import cn.iocoder.yudao.module.mp.service.account.MpAccountService; import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
@ -18,54 +21,72 @@ import javax.validation.Valid;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Api(tags = "管理后台 - 公众号账") @Api(tags = "管理后台 - 公众号账")
@RestController @RestController
@RequestMapping("/mp/account") @RequestMapping("/mp/account")
@Validated @Validated
public class MpAccountController { public class MpAccountController {
@Resource @Resource
private MpAccountService wxAccountService; private MpAccountService mpAccountService;
@PostMapping("/create") @PostMapping("/create")
@ApiOperation("创建公众号账") @ApiOperation("创建公众号账")
@PreAuthorize("@ss.hasPermission('mp:account:create')") @PreAuthorize("@ss.hasPermission('mp:account:create')")
public CommonResult<Long> createWxAccount(@Valid @RequestBody MpAccountCreateReqVO createReqVO) { public CommonResult<Long> createWxAccount(@Valid @RequestBody MpAccountCreateReqVO createReqVO) {
return success(wxAccountService.createAccount(createReqVO)); return success(mpAccountService.createAccount(createReqVO));
} }
@PutMapping("/update") @PutMapping("/update")
@ApiOperation("更新公众号账") @ApiOperation("更新公众号账")
@PreAuthorize("@ss.hasPermission('mp:account:update')") @PreAuthorize("@ss.hasPermission('mp:account:update')")
public CommonResult<Boolean> updateWxAccount(@Valid @RequestBody MpAccountUpdateReqVO updateReqVO) { public CommonResult<Boolean> updateWxAccount(@Valid @RequestBody MpAccountUpdateReqVO updateReqVO) {
wxAccountService.updateAccount(updateReqVO); mpAccountService.updateAccount(updateReqVO);
return success(true); return success(true);
} }
@DeleteMapping("/delete") @DeleteMapping("/delete")
@ApiOperation("删除公众号账") @ApiOperation("删除公众号账")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:delete')") @PreAuthorize("@ss.hasPermission('mp:account:delete')")
public CommonResult<Boolean> deleteWxAccount(@RequestParam("id") Long id) { public CommonResult<Boolean> deleteWxAccount(@RequestParam("id") Long id) {
wxAccountService.deleteAccount(id); mpAccountService.deleteAccount(id);
return success(true); return success(true);
} }
@GetMapping("/get") @GetMapping("/get")
@ApiOperation("获得公众号账") @ApiOperation("获得公众号账")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:query')") @PreAuthorize("@ss.hasPermission('mp:account:query')")
public CommonResult<MpAccountRespVO> getWxAccount(@RequestParam("id") Long id) { public CommonResult<MpAccountRespVO> getWxAccount(@RequestParam("id") Long id) {
MpAccountDO wxAccount = wxAccountService.getAccount(id); MpAccountDO wxAccount = mpAccountService.getAccount(id);
return success(MpAccountConvert.INSTANCE.convert(wxAccount)); return success(MpAccountConvert.INSTANCE.convert(wxAccount));
} }
@GetMapping("/page") @GetMapping("/page")
@ApiOperation("获得公众号账分页") @ApiOperation("获得公众号账分页")
@PreAuthorize("@ss.hasPermission('mp:account:query')") @PreAuthorize("@ss.hasPermission('mp:account:query')")
public CommonResult<PageResult<MpAccountRespVO>> getWxAccountPage(@Valid MpAccountPageReqVO pageVO) { public CommonResult<PageResult<MpAccountRespVO>> getWxAccountPage(@Valid MpAccountPageReqVO pageVO) {
PageResult<MpAccountDO> pageResult = wxAccountService.getAccountPage(pageVO); PageResult<MpAccountDO> pageResult = mpAccountService.getAccountPage(pageVO);
return success(MpAccountConvert.INSTANCE.convertPage(pageResult)); return success(MpAccountConvert.INSTANCE.convertPage(pageResult));
} }
@PutMapping("/generate-qr-code")
@ApiOperation("生成公众号二维码")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:qr-code')")
public CommonResult<Boolean> generateAccountQrCode(@RequestParam("id") Long id) {
mpAccountService.generateAccountQrCode(id);
return success(true);
}
@PutMapping("/clear-quota")
@ApiOperation("清空公众号 API 配额")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:clear-quota')")
public CommonResult<Boolean> clearAccountQuota(@RequestParam("id") Long id) {
mpAccountService.clearAccountQuota(id);
return success(true);
}
} }

View File

@ -3,10 +3,10 @@ package cn.iocoder.yudao.module.mp.controller.admin.account.vo;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotEmpty;
/** /**
* 公众号账 Base VO提供给添加修改详细的子 VO 使用 * 公众号账 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成 * 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
* *
* @author fengdan * @author fengdan
@ -14,29 +14,30 @@ import javax.validation.constraints.NotNull;
@Data @Data
public class MpAccountBaseVO { public class MpAccountBaseVO {
@ApiModelProperty(value = "公众号名称", required = true) @ApiModelProperty(value = "公众号名称", required = true, example = "芋道源码")
@NotNull(message = "公众号名称不能为空") @NotEmpty(message = "公众号名称不能为空")
private String name; private String name;
@ApiModelProperty(value = "公众号账户", required = true) @ApiModelProperty(value = "公众号微信号", required = true, example = "yudaoyuanma")
@NotNull(message = "公众号账户不能为空") @NotEmpty(message = "公众号微信号不能为空")
private String account; private String account;
@ApiModelProperty(value = "公众号 appId", required = true, example = "wx5b23ba7a5589ecbb") @ApiModelProperty(value = "公众号 appId", required = true, example = "wx5b23ba7a5589ecbb")
@NotNull(message = "公众号 appId 不能为空") @NotEmpty(message = "公众号 appId 不能为空")
private String appId; private String appId;
@ApiModelProperty(value = "公众号密钥", required = true) @ApiModelProperty(value = "公众号密钥", required = true, example = "3a7b3b20c537e52e74afd395eb85f61f")
@NotNull(message = "公众号密钥不能为空") @NotEmpty(message = "公众号密钥不能为空")
private String appSecret; private String appSecret;
@ApiModelProperty(value = "公众号 token", required = true) @ApiModelProperty(value = "公众号 token", required = true, example = "kangdayuzhen")
@NotEmpty(message = "公众号 token 不能为空")
private String token; private String token;
@ApiModelProperty(value = "加密密钥") @ApiModelProperty(value = "加密密钥", example = "gjN+Ksei")
private String aesKey; private String aesKey;
@ApiModelProperty(value = "备注") @ApiModelProperty(value = "备注", example = "请关注芋道源码,学习技术")
private String remark; private String remark;
} }

View File

@ -5,7 +5,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
@ApiModel("管理后台 - 公众号账创建 Request VO") @ApiModel("管理后台 - 公众号账创建 Request VO")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)

View File

@ -4,7 +4,7 @@ import lombok.*;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
@ApiModel("管理后台 - 公众号账分页 Request VO") @ApiModel("管理后台 - 公众号账分页 Request VO")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
@ -13,7 +13,7 @@ public class MpAccountPageReqVO extends PageParam {
@ApiModelProperty(value = "公众号名称", notes = "模糊匹配") @ApiModelProperty(value = "公众号名称", notes = "模糊匹配")
private String name; private String name;
@ApiModelProperty(value = "公众号账", notes = "模糊匹配") @ApiModelProperty(value = "公众号账", notes = "模糊匹配")
private String account; private String account;
@ApiModelProperty(value = "公众号 appid", notes = "模糊匹配") @ApiModelProperty(value = "公众号 appid", notes = "模糊匹配")

View File

@ -8,25 +8,19 @@ import lombok.ToString;
import java.util.Date; import java.util.Date;
/** @ApiModel("管理后台 - 公众号账号 Response VO")
* @author fengdan
*/
@ApiModel("管理后台 - 公众号账户 Response VO")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
public class MpAccountRespVO extends MpAccountBaseVO { public class MpAccountRespVO extends MpAccountBaseVO {
@ApiModelProperty(value = "编号", required = true) @ApiModelProperty(value = "编号", required = true, example = "1024")
private Long id; private Long id;
@ApiModelProperty(value = "二维码图片URL") @ApiModelProperty(value = "二维码图片URL", example = "https://www.iocoder.cn/1024.png")
private String qrCodeUrl; private String qrCodeUrl;
@ApiModelProperty(value = "创建时间", required = true) @ApiModelProperty(value = "创建时间", required = true)
private Date createTime; private Date createTime;
@ApiModelProperty(value = "公众号密钥", required = true)
private String appSecret;
} }

View File

@ -7,7 +7,7 @@ import javax.validation.constraints.*;
/** /**
* @author fengdan * @author fengdan
*/ */
@ApiModel("管理后台 - 公众号账更新 Request VO") @ApiModel("管理后台 - 公众号账更新 Request VO")
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)

View File

@ -1,18 +1,13 @@
package cn.iocoder.yudao.module.mp.dal.dataobject.account; package cn.iocoder.yudao.module.mp.dal.dataobject.account;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import lombok.*; import lombok.*;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
/** /**
* 公众号账 DO * 公众号账号 DO
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@ -36,7 +31,7 @@ public class MpAccountDO extends TenantBaseDO {
*/ */
private String name; private String name;
/** /**
* 公众号账 * 公众号账
*/ */
private String account; private String account;
/** /**
@ -52,7 +47,7 @@ public class MpAccountDO extends TenantBaseDO {
*/ */
private String token; private String token;
/** /**
* 加密密钥 * 消息密密钥
*/ */
private String aesKey; private String aesKey;
/** /**

View File

@ -6,9 +6,6 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountPageReqVO; import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountPageReqVO;
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO; import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
@Mapper @Mapper
public interface MpAccountMapper extends BaseMapperX<MpAccountDO> { public interface MpAccountMapper extends BaseMapperX<MpAccountDO> {
@ -21,7 +18,8 @@ public interface MpAccountMapper extends BaseMapperX<MpAccountDO> {
.orderByDesc(MpAccountDO::getId)); .orderByDesc(MpAccountDO::getId));
} }
@Select("SELECT COUNT(*) FROM mp_account WHERE update_time > #{maxUpdateTime}") default MpAccountDO selectByAppId(String appId) {
Long selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime); return selectOne(MpAccountDO::getAppId, appId);
}
} }

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.mp.mq.consumer;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.module.mp.mq.message.MpAccountRefreshMessage;
import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 针对 {@link MpAccountRefreshMessage} 的消费者
*
* @author 芋道源码
*/
@Component
@Slf4j
public class MpAccountRefreshConsumer extends AbstractChannelMessageListener<MpAccountRefreshMessage> {
@Resource
private MpAccountService mpAccountService;
@Override
public void onMessage(MpAccountRefreshMessage message) {
log.info("[onMessage][收到 Account 刷新消息]");
mpAccountService.initLocalCache();
}
}

View File

@ -1,29 +0,0 @@
package cn.iocoder.yudao.module.mp.mq.consumer;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.module.mp.mq.message.MpConfigRefreshMessage;
import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 针对 {@link MpConfigRefreshMessage} 的消费者
*
* @author lyz
*/
@Component
@Slf4j
public class MpConfigRefreshConsumer extends AbstractChannelMessageListener<MpConfigRefreshMessage> {
@Resource
private MpAccountService wxAccountService;
@Override
public void onMessage(MpConfigRefreshMessage message) {
log.info("[onMessage][收到 MpConfig 刷新消息]");
wxAccountService.initLocalCache();
}
}

View File

@ -5,15 +5,17 @@ import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
/** /**
* 微信配置数据刷新 Message * 公众号账号刷新 Message
*
* @author 芋道源码
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class MpConfigRefreshMessage extends AbstractChannelMessage { public class MpAccountRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {
return "mp.config-data.refresh"; return "mp.account.refresh";
} }
} }

View File

@ -1,25 +1,27 @@
package cn.iocoder.yudao.module.mp.mq.producer; package cn.iocoder.yudao.module.mp.mq.producer;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.module.mp.mq.message.MpConfigRefreshMessage; import cn.iocoder.yudao.module.mp.mq.message.MpAccountRefreshMessage;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
* 微信配置数据相关消息的 Producer * 公众号账号 Producer
*
* @author 芋道源码
*/ */
@Component @Component
public class MpConfigProducer { public class MpAccountProducer {
@Resource @Resource
private RedisMQTemplate redisMQTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* 发送 {@link MpConfigRefreshMessage} 消息 * 发送 {@link MpAccountRefreshMessage} 消息
*/ */
public void sendDictDataRefreshMessage() { public void sendAccountRefreshMessage() {
MpConfigRefreshMessage message = new MpConfigRefreshMessage(); MpAccountRefreshMessage message = new MpAccountRefreshMessage();
redisMQTemplate.send(message); redisMQTemplate.send(message);
} }

View File

@ -5,12 +5,11 @@ import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountCreateReq
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountPageReqVO; import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountPageReqVO;
import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountUpdateReqVO; import cn.iocoder.yudao.module.mp.controller.admin.account.vo.MpAccountUpdateReqVO;
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO; import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import javax.validation.Valid; import javax.validation.Valid;
/** /**
* 公众号账 Service 接口 * 公众号账 Service 接口
* *
* @author 芋道源码 * @author 芋道源码
*/ */
@ -22,7 +21,7 @@ public interface MpAccountService {
void initLocalCache(); void initLocalCache();
/** /**
* 创建公众号账 * 创建公众号账
* *
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
@ -30,51 +29,57 @@ public interface MpAccountService {
Long createAccount(@Valid MpAccountCreateReqVO createReqVO); Long createAccount(@Valid MpAccountCreateReqVO createReqVO);
/** /**
* 更新公众号账 * 更新公众号账
* *
* @param updateReqVO 更新信息 * @param updateReqVO 更新信息
*/ */
void updateAccount(@Valid MpAccountUpdateReqVO updateReqVO); void updateAccount(@Valid MpAccountUpdateReqVO updateReqVO);
/** /**
* 删除公众号账 * 删除公众号账
* *
* @param id 编号 * @param id 编号
*/ */
void deleteAccount(Long id); void deleteAccount(Long id);
/** /**
* 获得公众号账 * 获得公众号账
* *
* @param id 编号 * @param id 编号
* @return 公众号账 * @return 公众号账
*/ */
MpAccountDO getAccount(Long id); MpAccountDO getAccount(Long id);
/** /**
* 从缓存中获得公众号账 * 从缓存中获得公众号账
* *
* @param appId 微信公众号 appId * @param appId 微信公众号 appId
* @return 公众号账 * @return 公众号账
*/ */
MpAccountDO getAccountFromCache(String appId); MpAccountDO getAccountFromCache(String appId);
/** /**
* 获得公众号账分页 * 获得公众号账分页
* *
* @param pageReqVO 分页查询 * @param pageReqVO 分页查询
* @return 公众号账分页 * @return 公众号账分页
*/ */
PageResult<MpAccountDO> getAccountPage(MpAccountPageReqVO pageReqVO); PageResult<MpAccountDO> getAccountPage(MpAccountPageReqVO pageReqVO);
// TODO 芋艿去除这样的方法
/** /**
* 查询账户 * 生成公众号账号的二维码
* *
* @param field * @param id 编号
* @param val
* @return
*/ */
MpAccountDO findBy(SFunction<MpAccountDO, ?> field, Object val); void generateAccountQrCode(Long id);
/**
* 清空公众号账号的 API 配额
*
* 参考文档<a href="https://developers.weixin.qq.com/doc/offiaccount/Message_Management/API_Call_Limits.html">接口调用频次限制说明</a>
*
* @param id 编号
*/
void clearAccountQuota(Long id);
} }

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.mp.service.account; package cn.iocoder.yudao.module.mp.service.account;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
@ -12,24 +13,27 @@ import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
import cn.iocoder.yudao.module.mp.dal.mysql.account.MpAccountMapper; import cn.iocoder.yudao.module.mp.dal.mysql.account.MpAccountMapper;
import cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory; import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory;
import cn.iocoder.yudao.module.mp.mq.producer.MpConfigProducer; import cn.iocoder.yudao.module.mp.mq.producer.MpAccountProducer;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.google.common.annotations.VisibleForTesting;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_USERNAME_EXISTS;
/** /**
* 公众号账 Service 实现类 * 公众号账 Service 实现类
* *
* @author fengdan * @author fengdan
*/ */
@ -38,12 +42,6 @@ import java.util.Map;
@Validated @Validated
public class MpAccountServiceImpl implements MpAccountService { public class MpAccountServiceImpl implements MpAccountService {
/**
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
* 因为已经通过 Redis Pub/Sub 机制所以频率不需要高
*/
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
/** /**
* 账号缓存 * 账号缓存
* key账号编号 {@link MpAccountDO#getAppId()} * key账号编号 {@link MpAccountDO#getAppId()}
@ -52,11 +50,6 @@ public class MpAccountServiceImpl implements MpAccountService {
*/ */
@Getter @Getter
private volatile Map<String, MpAccountDO> accountCache; private volatile Map<String, MpAccountDO> accountCache;
/**
* 缓存菜单的最大更新时间用于后续的增量轮询判断是否有更新
*/
@Getter
private volatile LocalDateTime maxUpdateTime;
@Resource @Resource
private MpAccountMapper mpAccountMapper; private MpAccountMapper mpAccountMapper;
@ -66,89 +59,85 @@ public class MpAccountServiceImpl implements MpAccountService {
private MpServiceFactory mpServiceFactory; private MpServiceFactory mpServiceFactory;
@Resource @Resource
private MpConfigProducer mpConfigDataProducer; private MpAccountProducer mpAccountProducer;
@Override @Override
@PostConstruct @PostConstruct
public void initLocalCache() { public void initLocalCache() {
initLocalCacheIfUpdate(null);
}
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
public void schedulePeriodicRefresh() {
initLocalCacheIfUpdate(this.maxUpdateTime);
}
/**
* 刷新本地缓存
*
* @param maxUpdateTime 最大更新时间
* 1. 如果 maxUpdateTime null强制刷新缓存
* 2. 如果 maxUpdateTime 不为 null判断自 maxUpdateTime 是否有数据发生变化有的情况下才刷新缓存
*/
private void initLocalCacheIfUpdate(LocalDateTime maxUpdateTime) {
// 注意忽略自动多租户因为要全局初始化缓存 // 注意忽略自动多租户因为要全局初始化缓存
TenantUtils.executeIgnore(() -> { TenantUtils.executeIgnore(() -> {
// 第一步基于 maxUpdateTime 判断缓存是否刷新 // 第一步查询数据
// 如果没有增量的数据变化则不进行本地缓存的刷新
if (maxUpdateTime != null
&& mpAccountMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
log.info("[initLocalCacheIfUpdate][数据未发生变化({}),本地缓存不刷新]", maxUpdateTime);
return;
}
List<MpAccountDO> accounts = mpAccountMapper.selectList(); List<MpAccountDO> accounts = mpAccountMapper.selectList();
log.info("[initLocalCacheIfUpdate][缓存公众号账号,数量为:{}]", accounts.size()); log.info("[initLocalCacheIfUpdate][缓存公众号账号,数量为:{}]", accounts.size());
// 第二步构建缓存创建或更新支付 Client // 第二步构建缓存创建或更新支付 Client
mpServiceFactory.init(accounts); mpServiceFactory.init(accounts);
accountCache = CollectionUtils.convertMap(accounts, MpAccountDO::getAppId); accountCache = CollectionUtils.convertMap(accounts, MpAccountDO::getAppId);
// 第三步设置最新的 maxUpdateTime用于下次的增量判断
this.maxUpdateTime = CollectionUtils.getMaxValue(accounts, MpAccountDO::getUpdateTime);
}); });
} }
@Override @Override
public Long createAccount(MpAccountCreateReqVO createReqVO) { public Long createAccount(MpAccountCreateReqVO createReqVO) {
// TODO 芋艿校验唯一性 // 校验 appId 唯一
// 插入 validateAppIdUnique(null, createReqVO.getAppId());
MpAccountDO wxAccount = MpAccountConvert.INSTANCE.convert(createReqVO);
mpAccountMapper.insert(wxAccount);
// TODO 芋艿刷新的方式 // 插入
mpConfigDataProducer.sendDictDataRefreshMessage(); MpAccountDO account = MpAccountConvert.INSTANCE.convert(createReqVO);
// 返回 mpAccountMapper.insert(account);
return wxAccount.getId();
// 发送刷新消息
mpAccountProducer.sendAccountRefreshMessage();
return account.getId();
} }
@Override @Override
public void updateAccount(MpAccountUpdateReqVO updateReqVO) { public void updateAccount(MpAccountUpdateReqVO updateReqVO) {
// TODO 芋艿校验唯一性
// 校验存在 // 校验存在
validateWxAccountExists(updateReqVO.getId()); validateAccountExists(updateReqVO.getId());
// 校验 appId 唯一
validateAppIdUnique(updateReqVO.getId(), updateReqVO.getAppId());
// 更新 // 更新
MpAccountDO updateObj = MpAccountConvert.INSTANCE.convert(updateReqVO); MpAccountDO updateObj = MpAccountConvert.INSTANCE.convert(updateReqVO);
mpAccountMapper.updateById(updateObj); mpAccountMapper.updateById(updateObj);
// TODO 芋艿刷新的方式 // 发送刷新消息
mpConfigDataProducer.sendDictDataRefreshMessage(); mpAccountProducer.sendAccountRefreshMessage();
} }
@Override @Override
public void deleteAccount(Long id) { public void deleteAccount(Long id) {
// 校验存在 // 校验存在
validateWxAccountExists(id); validateAccountExists(id);
// 删除 // 删除
mpAccountMapper.deleteById(id); mpAccountMapper.deleteById(id);
// TODO 芋艿刷新的方式 // 发送刷新消息
mpConfigDataProducer.sendDictDataRefreshMessage(); mpAccountProducer.sendAccountRefreshMessage();
} }
private void validateWxAccountExists(Long id) { private MpAccountDO validateAccountExists(Long id) {
if (mpAccountMapper.selectById(id) == null) { MpAccountDO account = mpAccountMapper.selectById(id);
throw ServiceExceptionUtil.exception(ErrorCodeConstants.WX_ACCOUNT_NOT_EXISTS); if (account == null) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.ACCOUNT_NOT_EXISTS);
} }
return account;
}
@VisibleForTesting
public void validateAppIdUnique(Long id, String appId) {
// 多个租户appId 是不能重复否则公众号回调会无法识别
TenantUtils.executeIgnore(() -> {
MpAccountDO account = mpAccountMapper.selectByAppId(appId);
if (account == null) {
return;
}
// 存在 account 记录的情况下
if (id == null // 新增时说明重复
|| ObjUtil.notEqual(id, account.getId())) { // 更新时如果 id 不一致说明重复
throw exception(USER_USERNAME_EXISTS);
}
});
} }
@Override @Override
@ -167,8 +156,36 @@ public class MpAccountServiceImpl implements MpAccountService {
} }
@Override @Override
public MpAccountDO findBy(SFunction<MpAccountDO, ?> field, Object val) { public void generateAccountQrCode(Long id) {
return mpAccountMapper.selectOne(field, val); // 校验存在
MpAccountDO account = validateAccountExists(id);
// 生成二维码
WxMpService mpService = mpServiceFactory.getRequiredMpService(account.getAppId());
String qrCodeUrl;
try {
WxMpQrCodeTicket qrCodeTicket = mpService.getQrcodeService().qrCodeCreateLastTicket("default");
qrCodeUrl = mpService.getQrcodeService().qrCodePictureUrl(qrCodeTicket.getTicket());
} catch (WxErrorException e) {
throw exception(ErrorCodeConstants.ACCOUNT_GENERATE_QR_CODE_FAIL, e.getError().getErrorMsg());
}
// 保存二维码
mpAccountMapper.updateById(new MpAccountDO().setId(id).setQrCodeUrl(qrCodeUrl));
}
@Override
public void clearAccountQuota(Long id) {
// 校验存在
MpAccountDO account = validateAccountExists(id);
// 生成二维码
WxMpService mpService = mpServiceFactory.getRequiredMpService(account.getAppId());
try {
mpService.clearQuota(account.getAppId());
} catch (WxErrorException e) {
throw exception(ErrorCodeConstants.ACCOUNT_CLEAR_QUOTA_FAIL, e.getError().getErrorMsg());
}
} }
} }

View File

@ -1,6 +1,6 @@
import request from '@/utils/request' import request from '@/utils/request'
// 创建公众号账 // 创建公众号账
export function createAccount(data) { export function createAccount(data) {
return request({ return request({
url: '/mp/account/create', url: '/mp/account/create',
@ -9,7 +9,7 @@ export function createAccount(data) {
}) })
} }
// 更新公众号账 // 更新公众号账
export function updateAccount(data) { export function updateAccount(data) {
return request({ return request({
url: '/mp/account/update', url: '/mp/account/update',
@ -18,7 +18,7 @@ export function updateAccount(data) {
}) })
} }
// 删除公众号账 // 删除公众号账
export function deleteAccount(id) { export function deleteAccount(id) {
return request({ return request({
url: '/mp/account/delete?id=' + id, url: '/mp/account/delete?id=' + id,
@ -26,7 +26,7 @@ export function deleteAccount(id) {
}) })
} }
// 获得公众号账 // 获得公众号账
export function getAccount(id) { export function getAccount(id) {
return request({ return request({
url: '/mp/account/get?id=' + id, url: '/mp/account/get?id=' + id,
@ -34,7 +34,7 @@ export function getAccount(id) {
}) })
} }
// 获得公众号账分页 // 获得公众号账分页
export function getAccountPage(query) { export function getAccountPage(query) {
return request({ return request({
url: '/mp/account/page', url: '/mp/account/page',
@ -43,12 +43,18 @@ export function getAccountPage(query) {
}) })
} }
// 导出公众号账户 Excel // 生成公众号二维码
export function exportAccountExcel(query) { export function generateAccountQrCode(id) {
return request({ return request({
url: '/mp/account/export-excel', url: '/mp/account/generate-qr-code?id=' + id,
method: 'get', method: 'put'
params: query, })
responseType: 'blob' }
// 清空公众号 API 配额
export function clearAccountQuota(id) {
return request({
url: '/mp/account/clear-quota?id=' + id,
method: 'put'
}) })
} }

View File

@ -28,16 +28,25 @@
<el-table-column label="名称" align="center" prop="name"/> <el-table-column label="名称" align="center" prop="name"/>
<el-table-column label="微信号" align="center" prop="account" width="180"/> <el-table-column label="微信号" align="center" prop="account" width="180"/>
<el-table-column label="appId" align="center" prop="appId" width="180"/> <el-table-column label="appId" align="center" prop="appId" width="180"/>
<el-table-column label="appSecret" align="center" prop="appSecret" width="180"/> <!-- <el-table-column label="appSecret" align="center" prop="appSecret" width="180"/>-->
<el-table-column label="Token" align="center" prop="token"/> <!-- <el-table-column label="token" align="center" prop="token"/>-->
<el-table-column label="密钥" align="center" prop="aesKey"/> <!-- <el-table-column label="消息加解密密钥" align="center" prop="aesKey"/>-->
<el-table-column label="二维码" align="center" prop="qrCodeUrl"/> <el-table-column label="服务器地址(URL)" align="center" prop="appId" width="360">
<el-table-column label="备注" align="center" prop="remark"/> <template v-slot="scope">
<el-table-column label="创建时间" align="center" prop="createTime" width="180"> {{ 'http://服务端地址/mp/open/' + scope.row.appId }}
<template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime) }}</span>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column label="二维码" align="center" prop="qrCodeUrl">
<template v-slot="scope">
<img v-if="scope.row.qrCodeUrl" :src="scope.row.qrCodeUrl" alt="二维码" style="height: 100px;" />
</template>
</el-table-column>
<el-table-column label="备注" align="center" prop="remark"/>
<!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!-- <template slot-scope="scope">-->
<!-- <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!-- </template>-->
<!-- </el-table-column>-->
<el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
@ -46,6 +55,12 @@
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
v-hasPermi="['mp:account:delete']">删除 v-hasPermi="['mp:account:delete']">删除
</el-button> </el-button>
<el-button size="mini" type="text" icon="el-icon-refresh" @click="handleGenerateQrCode(scope.row)"
v-hasPermi="['mp:account:qr-code']">生成二维码
</el-button>
<el-button size="mini" type="text" icon="el-icon-share" @click="handleCleanQuota(scope.row)"
v-hasPermi="['mp:account:clear-quota']">清空 API 配额
</el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
@ -55,13 +70,13 @@
<!-- 对话框(添加 / 修改) --> <!-- 对话框(添加 / 修改) -->
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="100px"> <el-form ref="form" :model="form" :rules="rules" label-width="120px">
<el-form-item label="名称" prop="name"> <el-form-item label="名称" prop="name">
<el-input v-model="form.name" placeholder="请输入名称"/> <el-input v-model="form.name" placeholder="请输入名称"/>
</el-form-item> </el-form-item>
<el-form-item label="微信号" prop="account"> <el-form-item label="微信号" prop="account">
<span slot="label"> <span slot="label">
<el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单【设置】-【公众号设置】-【帐号详情】中能找到原始ID" placement="top"> <el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单 [设置与开发 - 公众号设置 - 账号详情] 中能找到「微信号」" placement="top">
<i class="el-icon-question" /> <i class="el-icon-question" />
</el-tooltip> </el-tooltip>
微信号 微信号
@ -70,30 +85,30 @@
</el-form-item> </el-form-item>
<el-form-item label="appId" prop="appId"> <el-form-item label="appId" prop="appId">
<span slot="label"> <span slot="label">
<el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单【开发】-【基本配置】中能找到 appId" placement="top"> <el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单 [设置与开发 - 公众号设置 - 基本设置] 中能找到「开发者ID(AppID)」" placement="top">
<i class="el-icon-question" /> <i class="el-icon-question" />
</el-tooltip> </el-tooltip>
appId appId
</span> </span>
<el-input v-model="form.appId" placeholder="请输入公众号 appId"/> <el-input v-model="form.appId" placeholder="请输入公众号 appId"/>
</el-form-item> </el-form-item>
<el-form-item label="密钥" prop="appSecret"> <el-form-item label="appSecret" prop="appSecret">
<span slot="label"> <span slot="label">
<el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单【开发】-【基本配置】中能找到密钥" placement="top"> <el-tooltip content="在微信公众平台mp.weixin.qq.com的菜单 [设置与开发 - 公众号设置 - 基本设置] 中能找到「开发者密码(AppSecret)」" placement="top">
<i class="el-icon-question" /> <i class="el-icon-question" />
</el-tooltip> </el-tooltip>
密钥 appSecret
</span> </span>
<el-input v-model="form.appSecret" placeholder="请输入公众号 appSecret"/> <el-input v-model="form.appSecret" placeholder="请输入公众号 appSecret"/>
</el-form-item> </el-form-item>
<el-form-item label="token" prop="token"> <el-form-item label="token" prop="token">
<el-input v-model="form.token" placeholder="请输入公众号token"/> <el-input v-model="form.token" placeholder="请输入公众号token"/>
</el-form-item> </el-form-item>
<el-form-item label="加密密钥" prop="aesKey"> <el-form-item label="消息密密钥" prop="aesKey">
<el-input v-model="form.aesKey" placeholder="请输入加密密钥"/> <el-input v-model="form.aesKey" placeholder="请输入消息密密钥"/>
</el-form-item> </el-form-item>
<el-form-item label="备注" prop="remark"> <el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" placeholder="请输入备注"/> <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
</el-form-item> </el-form-item>
</el-form> </el-form>
<div slot="footer" class="dialog-footer"> <div slot="footer" class="dialog-footer">
@ -105,7 +120,15 @@
</template> </template>
<script> <script>
import { createAccount, deleteAccount, getAccount, getAccountPage, updateAccount} from '@/api/mp/account' import {
clearAccountQuota,
createAccount,
deleteAccount,
generateAccountQrCode,
getAccount,
getAccountPage,
updateAccount
} from '@/api/mp/account'
export default { export default {
name: 'mpAccount', name: 'mpAccount',
@ -120,7 +143,7 @@ export default {
showSearch: true, showSearch: true,
// //
total: 0, total: 0,
// //
list: [], list: [],
// //
title: '', title: '',
@ -139,7 +162,7 @@ export default {
// //
rules: { rules: {
name: [{required: true, message: '名称不能为空', trigger: 'blur'}], name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
account: [{required: true, message: '公众号账不能为空', trigger: 'blur'}], account: [{required: true, message: '公众号账不能为空', trigger: 'blur'}],
appId: [{required: true, message: '公众号 appId 不能为空', trigger: 'blur'}], appId: [{required: true, message: '公众号 appId 不能为空', trigger: 'blur'}],
appSecret: [{required: true, message: '公众号密钥不能为空', trigger: 'blur'}], appSecret: [{required: true, message: '公众号密钥不能为空', trigger: 'blur'}],
token: [{required: true, message: '公众号 token 不能为空', trigger: 'blur'}], token: [{required: true, message: '公众号 token 不能为空', trigger: 'blur'}],
@ -199,7 +222,7 @@ export default {
handleAdd() { handleAdd() {
this.reset() this.reset()
this.open = true this.open = true
this.title = '添加公众号账' this.title = '添加公众号账'
}, },
/** 修改按钮操作 */ /** 修改按钮操作 */
handleUpdate(row) { handleUpdate(row) {
@ -208,7 +231,7 @@ export default {
getAccount(id).then(response => { getAccount(id).then(response => {
this.form = response.data this.form = response.data
this.open = true this.open = true
this.title = '修改公众号账' this.title = '修改公众号账'
this.disabled = true this.disabled = true
}) })
}, },
@ -238,7 +261,7 @@ export default {
/** 删除按钮操作 */ /** 删除按钮操作 */
handleDelete(row) { handleDelete(row) {
const id = row.id const id = row.id
this.$modal.confirm('是否确认删除公众号账编号为"' + row.name + '"的数据项?').then(function () { this.$modal.confirm('是否确认删除公众号账编号为"' + row.name + '"的数据项?').then(function () {
return deleteAccount(id) return deleteAccount(id)
}).then(() => { }).then(() => {
this.getList() this.getList()
@ -246,6 +269,27 @@ export default {
}).catch(() => { }).catch(() => {
}) })
}, },
/** 生成二维码的按钮操作 */
handleGenerateQrCode(row) {
const id = row.id
this.$modal.confirm('是否确认生成公众号账号编号为"' + row.name + '"的二维码?').then(function () {
return generateAccountQrCode(id)
}).then(() => {
this.getList()
this.$modal.msgSuccess('生成二维码成功')
}).catch(() => {
})
},
/** 清空二维码 API 配额的按钮操作 */
handleCleanQuota(row) {
const id = row.id
this.$modal.confirm('是否清空生成公众号账号编号为"' + row.name + '"的 API 配额?').then(function () {
return clearAccountQuota(id)
}).then(() => {
this.$modal.msgSuccess('清空 API 配额成功')
}).catch(() => {
})
},
} }
} }
</script> </script>