mirror of
https://gitee.com/yudaocode/yudao-boot-mini.git
synced 2025-12-26 07:06:22 +08:00
【同步】最新精简版本!(〃'▽'〃) v2025.11 发布:极大完善 vben5 的 antd、vben 版本的功能,优化整体稳定性
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled
yudao-ui-admin CI / build (14.x) (push) Has been cancelled
yudao-ui-admin CI / build (16.x) (push) Has been cancelled
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled
yudao-ui-admin CI / build (14.x) (push) Has been cancelled
yudao-ui-admin CI / build (16.x) (push) Has been cancelled
This commit is contained in:
parent
e2cbfed077
commit
0435a45df7
@ -1,86 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.encrypt;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
|
||||
import cn.hutool.crypto.asymmetric.KeyType;
|
||||
import cn.hutool.crypto.asymmetric.RSA;
|
||||
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 各种 API 加解密的测试类:不是单测,而是方便大家生成密钥、加密、解密等操作。
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@SuppressWarnings("ConstantValue")
|
||||
public class ApiEncryptTest {
|
||||
|
||||
@Test
|
||||
public void testGenerateAsymmetric() {
|
||||
String asymmetricAlgorithm = AsymmetricAlgorithm.RSA.getValue();
|
||||
// String asymmetricAlgorithm = "SM2";
|
||||
// String asymmetricAlgorithm = SM4.ALGORITHM_NAME;
|
||||
// String asymmetricAlgorithm = SymmetricAlgorithm.AES.getValue();
|
||||
String requestClientKey = null;
|
||||
String requestServerKey = null;
|
||||
String responseClientKey = null;
|
||||
String responseServerKey = null;
|
||||
if (Objects.equals(asymmetricAlgorithm, AsymmetricAlgorithm.RSA.getValue())) {
|
||||
// 请求的密钥
|
||||
RSA requestRsa = SecureUtil.rsa();
|
||||
requestClientKey = requestRsa.getPublicKeyBase64();
|
||||
requestServerKey = requestRsa.getPrivateKeyBase64();
|
||||
// 响应的密钥
|
||||
RSA responseRsa = new RSA();
|
||||
responseClientKey = responseRsa.getPrivateKeyBase64();
|
||||
responseServerKey = responseRsa.getPublicKeyBase64();
|
||||
} else if (Objects.equals(asymmetricAlgorithm, SymmetricAlgorithm.AES.getValue())) {
|
||||
// AES 密钥可选 32、24、16 位
|
||||
// 请求的密钥(前后端密钥一致)
|
||||
requestClientKey = RandomUtil.randomNumbers(32);
|
||||
requestServerKey = requestClientKey;
|
||||
// 响应的密钥(前后端密钥一致)
|
||||
responseClientKey = RandomUtil.randomNumbers(32);
|
||||
responseServerKey = responseClientKey;
|
||||
}
|
||||
|
||||
// 打印结果
|
||||
System.out.println("requestClientKey = " + requestClientKey);
|
||||
System.out.println("requestServerKey = " + requestServerKey);
|
||||
System.out.println("responseClientKey = " + responseClientKey);
|
||||
System.out.println("responseServerKey = " + responseServerKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncrypt_aes() {
|
||||
String key = "52549111389893486934626385991395";
|
||||
String body = "{\n" +
|
||||
" \"username\": \"admin\",\n" +
|
||||
" \"password\": \"admin123\",\n" +
|
||||
" \"uuid\": \"3acd87a09a4f48fb9118333780e94883\",\n" +
|
||||
" \"code\": \"1024\"\n" +
|
||||
"}";
|
||||
String encrypt = SecureUtil.aes(StrUtil.utf8Bytes(key))
|
||||
.encryptBase64(body);
|
||||
System.out.println("encrypt = " + encrypt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncrypt_rsa() {
|
||||
String key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCls2rIpnGdYnLFgz1XU13GbNQ5DloyPpvW00FPGjqn5Z6JpK+kDtVlnkhwR87iRrE5Vf2WNqRX6vzbLSgveIQY8e8oqGCb829myjf1MuI+ZzN4ghf/7tEYhZJGPI9AbfxFqBUzm+kR3/HByAI22GLT96WM26QiMK8n3tIP/yiLswIDAQAB";
|
||||
String body = "{\n" +
|
||||
" \"username\": \"admin\",\n" +
|
||||
" \"password\": \"admin123\",\n" +
|
||||
" \"uuid\": \"3acd87a09a4f48fb9118333780e94883\",\n" +
|
||||
" \"code\": \"1024\"\n" +
|
||||
"}";
|
||||
String encrypt = SecureUtil.rsa(null, key)
|
||||
.encryptBase64(body, KeyType.PublicKey);
|
||||
System.out.println("encrypt = " + encrypt);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
package cn.iocoder.yudao.module.ai.framework.ai.core.model.grok;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.model.ChatModel;
|
||||
import org.springframework.ai.chat.model.ChatResponse;
|
||||
import org.springframework.ai.chat.prompt.ChatOptions;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
* Grok {@link ChatModel} 实现类
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class GrokChatModel implements ChatModel {
|
||||
|
||||
public static final String BASE_URL = "https://api.x.ai";
|
||||
public static final String COMPLETE_PATH = "/v1/chat/completions";
|
||||
public static final String MODEL_DEFAULT = "grok-4-fast-reasoning";
|
||||
|
||||
/**
|
||||
* 兼容 OpenAI 接口,进行复用
|
||||
*/
|
||||
private final ChatModel openAiChatModel;
|
||||
|
||||
@Override
|
||||
public ChatResponse call(Prompt prompt) {
|
||||
return openAiChatModel.call(prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<ChatResponse> stream(Prompt prompt) {
|
||||
return openAiChatModel.stream(prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatOptions getDefaultOptions() {
|
||||
return openAiChatModel.getDefaultOptions();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,44 +0,0 @@
|
||||
### 请求 /mp/message-template/get 接口 => 成功
|
||||
GET {{baseUrl}}/mp/message-template/get?id=1
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
tenant-id: {{adminTenantId}}
|
||||
|
||||
### 请求 /mp/message-template/list 接口 => 成功
|
||||
GET {{baseUrl}}/mp/message-template/list?accountId=1
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
tenant-id: {{adminTenantId}}
|
||||
|
||||
### 请求 /mp/message-template/delete 接口 => 成功
|
||||
DELETE {{baseUrl}}/mp/message-template/delete?id=1
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
tenant-id: {{adminTenantId}}
|
||||
|
||||
### 请求 /mp/message-template/sync 接口 => 成功
|
||||
POST {{baseUrl}}/mp/message-template/sync?accountId=5
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
tenant-id: {{adminTenantId}}
|
||||
|
||||
### 请求 /mp/message-template/send 接口 => 成功
|
||||
POST {{baseUrl}}/mp/message-template/send
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer {{token}}
|
||||
tenant-id: {{adminTenantId}}
|
||||
|
||||
{
|
||||
"id": 66,
|
||||
"userId": 65,
|
||||
"url": "https://example.com",
|
||||
"data": {
|
||||
"result": "领奖成功",
|
||||
"withdrawMoney": "1000.00元",
|
||||
"withdrawTime": "2024-01-01 10:00:00",
|
||||
"cardInfo": "工商银行(尾号1234)",
|
||||
"arrivedTime": "2024-01-01 10:30:00"
|
||||
}
|
||||
}
|
||||
|
||||
// "miniprogram": "{\"appid\":\"wx1234567890\",\"pagepath\":\"pages/index/index\"}",
|
||||
@ -1,76 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.controller.admin.message;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateListReqVO;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateRespVO;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateSendReqVO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.message.MpMessageTemplateDO;
|
||||
import cn.iocoder.yudao.module.mp.service.message.MpMessageTemplateService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - 公众号模版消息")
|
||||
@RestController
|
||||
@RequestMapping("/mp/message-template")
|
||||
@Validated
|
||||
public class MpMessageTemplateController {
|
||||
|
||||
@Resource
|
||||
private MpMessageTemplateService messageTemplateService;
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除模版消息")
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
@PreAuthorize("@ss.hasPermission('mp:message-template:delete')")
|
||||
public CommonResult<Boolean> deleteMessageTemplate(@RequestParam("id") Long id) {
|
||||
messageTemplateService.deleteMessageTemplate(id);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得模版消息")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('mp:message-template:query')")
|
||||
public CommonResult<MpMessageTemplateRespVO> getMessageTemplate(@RequestParam("id") Long id) {
|
||||
MpMessageTemplateDO msgTemplate = messageTemplateService.getMessageTemplate(id);
|
||||
return success(BeanUtils.toBean(msgTemplate, MpMessageTemplateRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "获得模版消息列表")
|
||||
@Parameter(name = "accountId", description = "公众号账号的编号", required = true, example = "2048")
|
||||
@PreAuthorize("@ss.hasPermission('mp:message-template:query')")
|
||||
public CommonResult<List<MpMessageTemplateRespVO>> getMessageTemplateList(MpMessageTemplateListReqVO listReqVO) {
|
||||
List<MpMessageTemplateDO> list = messageTemplateService.getMessageTemplateList(listReqVO);
|
||||
return success(BeanUtils.toBean(list, MpMessageTemplateRespVO.class));
|
||||
}
|
||||
|
||||
@PostMapping("/sync")
|
||||
@Operation(summary = "同步公众号模板")
|
||||
@Parameter(name = "accountId", description = "公众号账号的编号", required = true, example = "2048")
|
||||
@PreAuthorize("@ss.hasPermission('mp:message-template:sync')")
|
||||
public CommonResult<Boolean> syncMessageTemplate(@RequestParam("accountId") Long accountId) {
|
||||
messageTemplateService.syncMessageTemplate(accountId);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/send")
|
||||
@Operation(summary = "给粉丝发送模版消息")
|
||||
@PreAuthorize("@ss.hasPermission('mp:message-template:send')")
|
||||
public CommonResult<Boolean> sendMessageTemplate(@Valid @RequestBody MpMessageTemplateSendReqVO sendReqVO) {
|
||||
messageTemplateService.sendMessageTempalte(sendReqVO);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.controller.admin.message.vo.template;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Schema(description = "管理后台 - 公众号模版消息列表 Request VO")
|
||||
@Data
|
||||
public class MpMessageTemplateListReqVO {
|
||||
|
||||
@Schema(description = "公众号账号的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
|
||||
@NotNull(message = "公众号账号的编号不能为空")
|
||||
private Long accountId;
|
||||
|
||||
}
|
||||
@ -1,42 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.controller.admin.message.vo.template;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Schema(description = "管理后台 - 公众号模版消息 Response VO")
|
||||
@Data
|
||||
public class MpMessageTemplateRespVO {
|
||||
|
||||
@Schema(description = "模版主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "7019")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "公众号账号的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long accountId;
|
||||
|
||||
@Schema(description = "appId", requiredMode = Schema.RequiredMode.REQUIRED, example = "wx1234567890abcdef")
|
||||
private String appId;
|
||||
|
||||
@Schema(description = "公众号模板ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "IjkGxO9M_mC9pE5Yl7QYJk1h0Dj2N4lC3oOp6rRsT8u")
|
||||
private String templateId;
|
||||
|
||||
@Schema(description = "标题", requiredMode = Schema.RequiredMode.REQUIRED, example = "订单状态提醒")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "模板内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String content;
|
||||
|
||||
@Schema(description = "模板示例")
|
||||
private String example;
|
||||
|
||||
@Schema(description = "模板所属行业的一级行业", example = "电商")
|
||||
private String primaryIndustry;
|
||||
|
||||
@Schema(description = "模板所属行业的二级行业", example = "商品售后")
|
||||
private String deputyIndustry;
|
||||
|
||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.controller.admin.message.vo.template;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - 公众号消息模版发送 Request VO") // 关联 https://developers.weixin.qq.com/doc/service/api/notify/template/api_sendtemplatemessage.html 文档
|
||||
@Data
|
||||
public class MpMessageTemplateSendReqVO {
|
||||
|
||||
@Schema(description = "模版主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "7019")
|
||||
@NotNull(message = "模版主键不能为空")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "公众号粉丝的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
@NotNull(message = "公众号粉丝的编号不能为空")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "模板跳转链接")
|
||||
private String url;
|
||||
|
||||
@Schema(description = "跳转小程序时填写")
|
||||
private String miniprogram;
|
||||
|
||||
@Schema(description = "模板内容")
|
||||
private Map<String, String> data;
|
||||
|
||||
}
|
||||
@ -1,69 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.dal.dataobject.message;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* 公众号模版消息 DO
|
||||
*
|
||||
* @author dengsl
|
||||
*/
|
||||
@TableName("mp_message_template")
|
||||
@KeySequence("mp_message_template_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MpMessageTemplateDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 公众号账号的编号
|
||||
*
|
||||
* 关联 {@link MpAccountDO#getId()}
|
||||
*/
|
||||
private Long accountId;
|
||||
/**
|
||||
* 公众号 appId
|
||||
*
|
||||
* 冗余 {@link MpAccountDO#getAppId()}
|
||||
*/
|
||||
private String appId;
|
||||
/**
|
||||
* 公众号模板 ID
|
||||
*/
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
private String title;
|
||||
/**
|
||||
* 模板内容
|
||||
*/
|
||||
private String content;
|
||||
/**
|
||||
* 模板示例
|
||||
*/
|
||||
private String example;
|
||||
|
||||
/**
|
||||
* 模板所属行业的一级行业
|
||||
*/
|
||||
private String primaryIndustry;
|
||||
/**
|
||||
* 模板所属行业的二级行业
|
||||
*/
|
||||
private String deputyIndustry;
|
||||
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.dal.mysql.message;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateListReqVO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.message.MpMessageTemplateDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface MpMessageTemplateMapper extends BaseMapperX<MpMessageTemplateDO> {
|
||||
|
||||
default List<MpMessageTemplateDO> selectList(MpMessageTemplateListReqVO listReqVO) {
|
||||
return selectList(MpMessageTemplateDO::getAccountId, listReqVO.getAccountId());
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.service.message;
|
||||
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateListReqVO;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateSendReqVO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.message.MpMessageTemplateDO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 公众号模版消息 Service 接口
|
||||
*
|
||||
* @author dengsl
|
||||
*/
|
||||
public interface MpMessageTemplateService {
|
||||
|
||||
/**
|
||||
* 删除模版消息
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteMessageTemplate(Long id);
|
||||
|
||||
/**
|
||||
* 获得模版消息
|
||||
*
|
||||
* @param id 编号
|
||||
* @return 模版消息
|
||||
*/
|
||||
MpMessageTemplateDO getMessageTemplate(Long id);
|
||||
|
||||
/**
|
||||
* 获得模版消息列表
|
||||
*
|
||||
* @param listReqVO 查询条件
|
||||
* @return 模版消息列表
|
||||
*/
|
||||
List<MpMessageTemplateDO> getMessageTemplateList(MpMessageTemplateListReqVO listReqVO);
|
||||
|
||||
/**
|
||||
* 同步公众号已添加的模版消息
|
||||
*
|
||||
* @param accountId 公众号账号的编号
|
||||
*/
|
||||
void syncMessageTemplate(Long accountId);
|
||||
|
||||
/**
|
||||
* 使用公众号,给粉丝发送【模版】消息
|
||||
*
|
||||
* @param sendReqVO 消息内容
|
||||
*/
|
||||
void sendMessageTempalte(MpMessageTemplateSendReqVO sendReqVO);
|
||||
|
||||
}
|
||||
@ -1,176 +0,0 @@
|
||||
package cn.iocoder.yudao.module.mp.service.message;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateListReqVO;
|
||||
import cn.iocoder.yudao.module.mp.controller.admin.message.vo.template.MpMessageTemplateSendReqVO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.account.MpAccountDO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.message.MpMessageTemplateDO;
|
||||
import cn.iocoder.yudao.module.mp.dal.dataobject.user.MpUserDO;
|
||||
import cn.iocoder.yudao.module.mp.dal.mysql.message.MpMessageTemplateMapper;
|
||||
import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory;
|
||||
import cn.iocoder.yudao.module.mp.service.account.MpAccountService;
|
||||
import cn.iocoder.yudao.module.mp.service.user.MpUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 公众号模版消息 Service 实现类
|
||||
*
|
||||
* @author dengsl
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class MpMessageTemplateServiceImpl implements MpMessageTemplateService {
|
||||
|
||||
@Resource
|
||||
@Lazy // 延迟加载,为了解决延迟加载
|
||||
private MpServiceFactory mpServiceFactory;
|
||||
|
||||
@Resource
|
||||
private MpMessageTemplateMapper messageTemplateMapper;
|
||||
|
||||
@Resource
|
||||
private MpAccountService mpAccountService;
|
||||
|
||||
@Resource
|
||||
private MpUserService mpUserService;
|
||||
|
||||
@Override
|
||||
public void deleteMessageTemplate(Long id) {
|
||||
// 校验存在
|
||||
MpMessageTemplateDO template = validateMsgTemplateExists(id);
|
||||
|
||||
// 第一步,删除模板到公众号平台
|
||||
try {
|
||||
mpServiceFactory.getRequiredMpService(template.getAppId())
|
||||
.getTemplateMsgService().delPrivateTemplate(template.getTemplateId());
|
||||
} catch (WxErrorException e) {
|
||||
throw exception(MESSAGE_TEMPLATE_DELETE_FAIL, e.getError().getErrorMsg());
|
||||
}
|
||||
|
||||
// 第二步,删除模板到数据库
|
||||
messageTemplateMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private MpMessageTemplateDO validateMsgTemplateExists(Long id) {
|
||||
MpMessageTemplateDO template = messageTemplateMapper.selectById(id);
|
||||
if (template == null) {
|
||||
throw exception(MESSAGE_TEMPLATE_NOT_EXISTS);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MpMessageTemplateDO getMessageTemplate(Long id) {
|
||||
return messageTemplateMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MpMessageTemplateDO> getMessageTemplateList(MpMessageTemplateListReqVO listReqVO) {
|
||||
return messageTemplateMapper.selectList(listReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncMessageTemplate(Long accountId) {
|
||||
MpAccountDO account = mpAccountService.getRequiredAccount(accountId);
|
||||
|
||||
// 第一步,从公众号平台获取最新的模板列表
|
||||
List<WxMpTemplate> wxTemplates;
|
||||
try {
|
||||
wxTemplates = mpServiceFactory.getRequiredMpService(accountId)
|
||||
.getTemplateMsgService().getAllPrivateTemplate();
|
||||
} catch (WxErrorException e) {
|
||||
throw exception(MESSAGE_TEMPLATE_SYNC_FAIL, e.getError().getErrorMsg());
|
||||
}
|
||||
|
||||
// 第二步,合并更新回自己的数据库
|
||||
Map<String, MpMessageTemplateDO> templateMap = convertMap(
|
||||
messageTemplateMapper.selectList(new LambdaQueryWrapperX<MpMessageTemplateDO>()
|
||||
.eq(MpMessageTemplateDO::getAppId, account.getAppId())),
|
||||
MpMessageTemplateDO::getTemplateId);
|
||||
wxTemplates.forEach(wxTemplate -> {
|
||||
MpMessageTemplateDO template = templateMap.remove(wxTemplate.getTemplateId());
|
||||
// 情况一,不存在,新增
|
||||
if (template == null) {
|
||||
template = new MpMessageTemplateDO().setAccountId(account.getId()).setAppId(account.getAppId())
|
||||
.setTemplateId(wxTemplate.getTemplateId()).setTitle(wxTemplate.getTitle())
|
||||
.setContent(wxTemplate.getContent()).setExample(wxTemplate.getExample())
|
||||
.setPrimaryIndustry(wxTemplate.getPrimaryIndustry()).setDeputyIndustry(wxTemplate.getDeputyIndustry());
|
||||
messageTemplateMapper.insert(template);
|
||||
return;
|
||||
}
|
||||
// 情况二,存在,则更新
|
||||
messageTemplateMapper.updateById(new MpMessageTemplateDO().setId(template.getId())
|
||||
.setTitle(wxTemplate.getTitle()).setContent(wxTemplate.getContent()).setExample(wxTemplate.getExample())
|
||||
.setPrimaryIndustry(wxTemplate.getPrimaryIndustry()).setDeputyIndustry(wxTemplate.getDeputyIndustry()));
|
||||
});
|
||||
// 情况三,部分模板已经不存在了,删除
|
||||
if (CollUtil.isNotEmpty(templateMap)) {
|
||||
messageTemplateMapper.deleteByIds(convertList(templateMap.values(), MpMessageTemplateDO::getId));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessageTempalte(MpMessageTemplateSendReqVO sendReqVO) {
|
||||
// 获得关联信息
|
||||
MpUserDO user = mpUserService.getRequiredUser(sendReqVO.getUserId());
|
||||
MpMessageTemplateDO template = validateMsgTemplateExists(sendReqVO.getId());
|
||||
|
||||
// 发送模版消息
|
||||
WxMpTemplateMessage templateMessage = buildTemplateMessage(template, user, sendReqVO);
|
||||
try {
|
||||
mpServiceFactory.getRequiredMpService(template.getAppId())
|
||||
.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
||||
} catch (WxErrorException e) {
|
||||
throw exception(MESSAGE_TEMPLATE_SEND_FAIL, e.getError().getErrorMsg());
|
||||
}
|
||||
|
||||
// 不用记录 MpMessageDO 记录,因为,微信会主动推送,可见文档 https://developers.weixin.qq.com/doc/service/guide/product/template_message/Template_Message_Interface.html
|
||||
}
|
||||
|
||||
private WxMpTemplateMessage buildTemplateMessage(MpMessageTemplateDO msgTemplateDO, MpUserDO user,
|
||||
MpMessageTemplateSendReqVO sendReqVO) {
|
||||
List<WxMpTemplateData> data = new ArrayList<>();
|
||||
WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder()
|
||||
.templateId(msgTemplateDO.getTemplateId())
|
||||
.data(data)
|
||||
.toUser(user.getOpenid());
|
||||
// 设置跳转链接
|
||||
if (StrUtil.isNotBlank(sendReqVO.getUrl())) {
|
||||
builder.url(sendReqVO.getUrl());
|
||||
}
|
||||
// 设置小程序跳转
|
||||
if (StrUtil.isNotBlank(sendReqVO.getMiniprogram())) {
|
||||
// https://developers.weixin.qq.com/doc/service/api/notify/template/api_sendtemplatemessage.html#Body__miniprogram
|
||||
builder.miniProgram(JsonUtils.parseObject(sendReqVO.getMiniprogram(), WxMpTemplateMessage.MiniProgram.class));
|
||||
}
|
||||
// 设置模板数据
|
||||
if (sendReqVO.getData() != null) {
|
||||
sendReqVO.getData().forEach((key, value) -> data.add(new WxMpTemplateData(key, value)));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user