mp:完成公众号统计的用户增减数据

This commit is contained in:
YunaiV
2023-01-07 22:35:30 +08:00
parent b9246d1543
commit 09e200c364
15 changed files with 591 additions and 12 deletions

View File

@ -33,14 +33,14 @@ public class MpAccountController {
@PostMapping("/create")
@ApiOperation("创建公众号账号")
@PreAuthorize("@ss.hasPermission('mp:account:create')")
public CommonResult<Long> createWxAccount(@Valid @RequestBody MpAccountCreateReqVO createReqVO) {
public CommonResult<Long> createAccount(@Valid @RequestBody MpAccountCreateReqVO createReqVO) {
return success(mpAccountService.createAccount(createReqVO));
}
@PutMapping("/update")
@ApiOperation("更新公众号账号")
@PreAuthorize("@ss.hasPermission('mp:account:update')")
public CommonResult<Boolean> updateWxAccount(@Valid @RequestBody MpAccountUpdateReqVO updateReqVO) {
public CommonResult<Boolean> updateAccount(@Valid @RequestBody MpAccountUpdateReqVO updateReqVO) {
mpAccountService.updateAccount(updateReqVO);
return success(true);
}
@ -49,7 +49,7 @@ public class MpAccountController {
@ApiOperation("删除公众号账号")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:delete')")
public CommonResult<Boolean> deleteWxAccount(@RequestParam("id") Long id) {
public CommonResult<Boolean> deleteAccount(@RequestParam("id") Long id) {
mpAccountService.deleteAccount(id);
return success(true);
}
@ -58,7 +58,7 @@ public class MpAccountController {
@ApiOperation("获得公众号账号")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('mp:account:query')")
public CommonResult<MpAccountRespVO> getWxAccount(@RequestParam("id") Long id) {
public CommonResult<MpAccountRespVO> getAccount(@RequestParam("id") Long id) {
MpAccountDO wxAccount = mpAccountService.getAccount(id);
return success(MpAccountConvert.INSTANCE.convert(wxAccount));
}
@ -66,7 +66,7 @@ public class MpAccountController {
@GetMapping("/page")
@ApiOperation("获得公众号账号分页")
@PreAuthorize("@ss.hasPermission('mp:account:query')")
public CommonResult<PageResult<MpAccountRespVO>> getWxAccountPage(@Valid MpAccountPageReqVO pageVO) {
public CommonResult<PageResult<MpAccountRespVO>> getAccountPage(@Valid MpAccountPageReqVO pageVO) {
PageResult<MpAccountDO> pageResult = mpAccountService.getAccountPage(pageVO);
return success(MpAccountConvert.INSTANCE.convertPage(pageResult));
}

View File

@ -13,7 +13,7 @@ import javax.validation.constraints.*;
@ToString(callSuper = true)
public class MpAccountUpdateReqVO extends MpAccountBaseVO {
@ApiModelProperty(value = "编号", required = true)
@ApiModelProperty(value = "编号", required = true, example = "1024")
@NotNull(message = "编号不能为空")
private Long id;

View File

@ -0,0 +1,38 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mp.convert.statistics.MpStatisticsConvert;
import cn.iocoder.yudao.module.mp.service.statistics.MpStatisticsService;
import cn.iocoder.yudao.module.mp.controller.admin.statistics.vo.MpStatisticsGetReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Api(tags = "管理后台 - 公众号统计")
@RestController
@RequestMapping("/mp/statistics")
@Validated
public class MpStatisticsController {
@Resource
private MpStatisticsService mpStatisticsService;
@GetMapping("/user-summary")
@ApiOperation("获得用户增减数据")
@PreAuthorize("@ss.hasPermission('mp:statistics:query')")
public CommonResult<List<WxDataCubeUserSummary>> getAccount(MpStatisticsGetReqVO getReqVO) {
List<WxDataCubeUserSummary> list = mpStatisticsService.getUserSummary(getReqVO.getId(), getReqVO.getDate());
return success(MpStatisticsConvert.INSTANCE.convertList01(list));
}
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel("管理后台 - 获得统计数据 Request VO")
@Data
public class MpStatisticsGetReqVO {
@ApiModelProperty(value = "公众号账号的编号", required = true, example = "1024")
@NotNull(message = "公众号账号的编号不能为空")
private Long id;
@ApiModelProperty(value = "查询时间范围")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@NotNull(message = "查询时间范围不能为空")
private LocalDateTime[] date;
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.mp.controller.admin.statistics.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@ApiModel("管理后台 - 某一天的用户增减数据 Response VO")
@Data
public class MpStatisticsUserSummaryRespVO {
@ApiModelProperty(value = "日期", required = true)
private Date refDate;
@ApiModelProperty(value = "用户来源", required = true, example = "0")
private Integer userSource;
@ApiModelProperty(value = "新关注的用户数量", required = true, example = "10")
private Integer newUser;
@ApiModelProperty(value = "取消关注的用户数量", required = true, example = "20")
private Integer cancelUser;
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.mp.convert.statistics;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface MpStatisticsConvert {
MpStatisticsConvert INSTANCE = Mappers.getMapper(MpStatisticsConvert.class);
List<WxDataCubeUserSummary> convertList01(List<WxDataCubeUserSummary> list);
}

View File

@ -38,7 +38,11 @@ public class DefaultMpServiceFactory implements MpServiceFactory {
/**
* 微信 appId 与 WxMpService 的映射
*/
private volatile Map<String, WxMpService> mpServices;
private volatile Map<String, WxMpService> appId2MpServices;
/**
* 公众号账号 id 与 WxMpService 的映射
*/
private volatile Map<Long, WxMpService> id2MpServices;
/**
* 微信 appId 与 WxMpMessageRouter 的映射
*/
@ -62,26 +66,34 @@ public class DefaultMpServiceFactory implements MpServiceFactory {
@Override
public void init(List<MpAccountDO> list) {
Map<String, WxMpService> mpServices = Maps.newHashMap();
Map<String, WxMpService> appId2MpServices = Maps.newHashMap();
Map<Long, WxMpService> id2MpServices = Maps.newHashMap();
Map<String, WxMpMessageRouter> mpMessageRouters = Maps.newHashMap();
// 处理 list
list.forEach(account -> {
// 构建 WxMpService 对象
WxMpService mpService = buildMpService(account);
mpServices.put(account.getAppId(), mpService);
appId2MpServices.put(account.getAppId(), mpService);
id2MpServices.put(account.getId(), mpService);
// 构建 WxMpMessageRouter 对象
WxMpMessageRouter mpMessageRouter = buildMpMessageRouter(mpService);
mpMessageRouters.put(account.getAppId(), mpMessageRouter);
});
// 设置到缓存
this.mpServices = mpServices;
this.appId2MpServices = appId2MpServices;
this.id2MpServices = id2MpServices;
this.mpMessageRouters = mpMessageRouters;
}
@Override
public WxMpService getMpService(Long id) {
return id2MpServices.get(id);
}
@Override
public WxMpService getMpService(String appId) {
return mpServices.get(appId);
return appId2MpServices.get(appId);
}
@Override

View File

@ -21,6 +21,20 @@ public interface MpServiceFactory {
*/
void init(List<MpAccountDO> list);
/**
* 获得 id 对应的 WxMpService 实例
*
* @param id 微信公众号的编号
* @return WxMpService 实例
*/
WxMpService getMpService(Long id);
default WxMpService getRequiredMpService(Long id) {
WxMpService wxMpService = getMpService(id);
Assert.notNull(wxMpService, "找到对应 id({}) 的 WxMpService请核实", id);
return wxMpService;
}
/**
* 获得 appId 对应的 WxMpService 实例
*

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.mp.service.statistics;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import java.time.LocalDateTime;
import java.util.List;
/**
* 公众号统计 Service 接口
*
* @author 芋道源码
*/
public interface MpStatisticsService {
/**
* 获取用户增减数据
*
* @param id 公众号账号编号
* @param date 时间区间
* @return 用户增减数据
*/
List<WxDataCubeUserSummary> getUserSummary(Long id, LocalDateTime[] date);
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.mp.service.statistics;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.STATISTICS_GET_USER_SUMMARY_FAIL;
/**
* 公众号统计 Service 实现类
*
* @author 芋道源码
*/
@Service
public class MpStatisticsServiceImpl implements MpStatisticsService {
@Resource
@Lazy // 延迟加载,解决循环依赖的问题
private MpServiceFactory mpServiceFactory;
@Override
public List<WxDataCubeUserSummary> getUserSummary(Long id, LocalDateTime[] date) {
WxMpService mpService = mpServiceFactory.getRequiredMpService(id);
try {
return mpService.getDataCubeService().getUserSummary(
DateUtil.date(date[0]), DateUtil.date(date[1]));
} catch (WxErrorException e) {
throw exception(STATISTICS_GET_USER_SUMMARY_FAIL, e.getError().getErrorMsg());
}
}
}