【同步】Boot 和 Cloud 的功能同步

pull/126/head
YunaiV 2024-07-13 12:56:09 +08:00
parent 3b8675dc6a
commit b8f1d01733
53 changed files with 1766 additions and 161 deletions

View File

@ -33,7 +33,6 @@ public class CrmStatisticsPerformanceReqVO {
@Schema(description = "负责人用户 id 集合", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "2")
private List<Long> userIds;
// TODO @scholar应该传递的是 int year年份
@Schema(description = "时间范围", requiredMode = Schema.RequiredMode.REQUIRED)
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@NotEmpty(message = "时间范围不能为空")

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.crm.service.statistics;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO;
@ -19,9 +17,11 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
/**
* CRM Service
@ -42,10 +42,6 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
@Override
public List<CrmStatisticsPerformanceRespVO> getContractCountPerformance(CrmStatisticsPerformanceReqVO performanceReqVO) {
// TODO @scholar可以把下面这个注释你理解后重新整理下写到 getPerformance 里;
// 比如说2024 年的合同数据,是不是 2022-12 到 2024-12-31每个月的统计呢
// 理解之后,我们可以数据 group by 年-月20222-12 到 2024-12-31 的,然后内存在聚合出 CrmStatisticsPerformanceRespVO 这样
// 这样,我们就可以减少数据库的计算量,提升性能;同时 SQL 也会很简单,开发者理解起来也简单哈;
return getPerformance(performanceReqVO, performanceMapper::selectContractCountPerformance);
}
@ -59,99 +55,45 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
return getPerformance(performanceReqVO, performanceMapper::selectReceivablePricePerformance);
}
// TODO @scholar代码注释应该有 3 个变量哈;
/**
*
*
* 1. +
* 2.
*
* @param performanceReqVO
* @param performanceFunction
* @return
*/
// TODO @scholar下面一行的变量超过一行了阅读不美观可以考虑每一行一个变量
private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO, Function<CrmStatisticsPerformanceReqVO,
List<CrmStatisticsPerformanceRespVO>> performanceFunction) {
// TODO @scholar没使用到的变量建议删除
List<CrmStatisticsPerformanceRespVO> performanceRespVOList;
private List<CrmStatisticsPerformanceRespVO> getPerformance(CrmStatisticsPerformanceReqVO performanceReqVO,
Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction) {
// 1. 获得用户编号数组
final List<Long> userIds = getUserIds(performanceReqVO);
List<Long> userIds = getUserIds(performanceReqVO);
if (CollUtil.isEmpty(userIds)) {
return Collections.emptyList();
}
performanceReqVO.setUserIds(userIds);
// TODO @scholar1. 和 2. 之间,可以考虑换一行;保证每一块逻辑的间隔;
// 2. 获得业绩数据
// TODO @scholar复数变量建议使用 s 或者 list 结果;这里用 performanceList 好列;
List<CrmStatisticsPerformanceRespVO> performance = performanceFunction.apply(performanceReqVO);
int year = performanceReqVO.getTimes()[0].getYear(); // 获取查询的年份
performanceReqVO.getTimes()[0] = performanceReqVO.getTimes()[0].minusYears(1);
List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO);
Map<String, BigDecimal> performanceMap = convertMap(performanceList, CrmStatisticsPerformanceRespVO::getTime,
CrmStatisticsPerformanceRespVO::getCurrentMonthCount);
// 获取查询的年份
// TODO @scholar逻辑可以简化一下
// TODO 1把 performance 转换成 mapkey 是 timevalue 是 count
// TODO 2当前年遍历 1-12 月份,去 map 拿到 count接着月份 -1去 map 拿 count再年份 -1拿 count
String currentYear = LocalDateTimeUtil.format(performanceReqVO.getTimes()[0],"yyyy");
// 构造查询当年和前一年每年12个月的年月组合
List<String> allMonths = new ArrayList<>();
for (int year = Integer.parseInt(currentYear)-1; year <= Integer.parseInt(currentYear); year++) {
for (int month = 1; month <= 12; month++) {
allMonths.add(String.format("%d%02d", year, month));
}
// 3. 组装数据返回
List<CrmStatisticsPerformanceRespVO> result = new ArrayList<>();
for (int month = 1; month <= 12; month++) {
String currentMonth = String.format("%d%02d", year, month);
String lastMonth = month == 1 ? String.format("%d%02d", year - 1, 12) : String.format("%d%02d", year, month - 1);
String lastYear = String.format("%d%02d", year - 1, month);
result.add(new CrmStatisticsPerformanceRespVO().setTime(currentMonth)
.setCurrentMonthCount(performanceMap.getOrDefault(currentMonth, BigDecimal.ZERO))
.setLastMonthCount(performanceMap.getOrDefault(lastMonth, BigDecimal.ZERO))
.setLastYearCount(performanceMap.getOrDefault(lastYear, BigDecimal.ZERO)));
}
List<CrmStatisticsPerformanceRespVO> computedList = new ArrayList<>();
List<CrmStatisticsPerformanceRespVO> respVOList = new ArrayList<>();
// 生成computedList基础数据
// 构造完整的2*12个月的数据如果某月数据缺失需要补上0一年12个月不能有缺失
for (String month : allMonths) {
CrmStatisticsPerformanceRespVO foundData = performance.stream()
.filter(data -> data.getTime().equals(month))
.findFirst()
.orElse(null);
if (foundData != null) {
computedList.add(foundData);
} else {
CrmStatisticsPerformanceRespVO missingData = new CrmStatisticsPerformanceRespVO();
missingData.setTime(month);
missingData.setCurrentMonthCount(BigDecimal.ZERO);
missingData.setLastMonthCount(BigDecimal.ZERO);
missingData.setLastYearCount(BigDecimal.ZERO);
computedList.add(missingData);
}
}
//根据查询年份和前一年的数据,计算查询年份的同比环比数据
for (CrmStatisticsPerformanceRespVO currentData : computedList) {
String currentMonth = currentData.getTime();
// 根据当年和前一年的月销售数据计算currentYear的完整数据
if (currentMonth.startsWith(currentYear)) {
// 计算 LastMonthCount
int currentIndex = computedList.indexOf(currentData);
if (currentIndex > 0) {
CrmStatisticsPerformanceRespVO lastMonthData = computedList.get(currentIndex - 1);
currentData.setLastMonthCount(lastMonthData.getCurrentMonthCount());
} else {
currentData.setLastMonthCount(BigDecimal.ZERO); // 第一个月的 LastMonthCount 设为0
}
// 计算 LastYearCount
String lastYearMonth = String.valueOf(Integer.parseInt(currentMonth) - 100);
CrmStatisticsPerformanceRespVO lastYearData = computedList.stream()
.filter(data -> data.getTime().equals(lastYearMonth))
.findFirst()
.orElse(null);
if (lastYearData != null) {
currentData.setLastYearCount(lastYearData.getCurrentMonthCount());
} else {
currentData.setLastYearCount(BigDecimal.ZERO); // 如果去年同月数据不存在设为0
}
respVOList.add(currentData);//给前端只需要返回查询当年的数据,不需要前一年数据
}
}
return respVOList;
return result;
}
/**
@ -163,7 +105,7 @@ public class CrmStatisticsPerformanceServiceImpl implements CrmStatisticsPerform
private List<Long> getUserIds(CrmStatisticsPerformanceReqVO reqVO) {
// 情况一:选中某个用户
if (ObjUtil.isNotNull(reqVO.getUserId())) {
return ListUtil.of(reqVO.getUserId());
return List.of(reqVO.getUserId());
}
// 情况二:选中某个部门
// 2.1 获得部门列表

View File

@ -9,51 +9,47 @@
COUNT(1) AS currentMonthCount
FROM crm_contract
WHERE deleted = 0
<!-- TODO @scholar20 改成静态类引入 -->
AND audit_status = 20
AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status}
AND owner_user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
<!-- TODO @scholarCrmStatisticsPerformanceReqVO 传递 year然后 java 代码里,转换出 times这样order_time 使用范围查询,避免使用函数 -->
AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime}, '%Y')
or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime}, '%Y') - 1)
AND order_date between #{times[0],javaType=java.time.LocalDateTime} and
#{times[1],javaType=java.time.LocalDateTime}
GROUP BY time
</select>
<!-- TODO @scholar参考上面调整下这个 SQL 的排版、和代码建议哈 -->
<select id="selectContractPricePerformance"
resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO">
SELECT
DATE_FORMAT(order_date, '%Y%m') AS time,
IFNULL(SUM(total_price), 0) AS currentMonthCount
FROM crm_contract
DATE_FORMAT(order_date, '%Y%m') AS time,
IFNULL(SUM(total_price), 0) AS currentMonthCount
FROM crm_contract
WHERE deleted = 0
AND audit_status = 20
AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status}
AND owner_user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
AND (DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
or DATE_FORMAT(order_date, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1)
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
AND order_date between #{times[0],javaType=java.time.LocalDateTime} and
#{times[1],javaType=java.time.LocalDateTime}
GROUP BY time
</select>
<!-- TODO @scholar参考上面调整下这个 SQL 的排版、和代码建议哈 -->
<select id="selectReceivablePricePerformance"
resultType="cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO">
SELECT
DATE_FORMAT(return_time, '%Y%m') AS time,
IFNULL(SUM(price), 0) AS currentMonthCount
FROM crm_receivable
DATE_FORMAT(return_time, '%Y%m') AS time,
IFNULL(SUM(price), 0) AS currentMonthCount
FROM crm_receivable
WHERE deleted = 0
AND audit_status = 20
AND audit_status = ${@cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum@APPROVE.status}
AND owner_user_id in
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
AND (DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')
or DATE_FORMAT(return_time, '%Y') = DATE_FORMAT(#{times[0],javaType=java.time.LocalDateTime},'%Y')-1)
<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
AND return_time between #{times[0],javaType=java.time.LocalDateTime} and
#{times[1],javaType=java.time.LocalDateTime}
GROUP BY time
</select>

View File

@ -125,4 +125,10 @@ public interface ErrorCodeConstants {
ErrorCode DIY_PAGE_NOT_EXISTS = new ErrorCode(1_013_018_000, "装修页面不存在");
ErrorCode DIY_PAGE_NAME_USED = new ErrorCode(1_013_018_001, "装修页面名称({})已经被使用");
// ========== 客服会话 1-013-019-000 ==========
ErrorCode KEFU_CONVERSATION_NOT_EXISTS = new ErrorCode(1_013_019_000, "客服会话不存在");
// ========== 客服消息 1-013-020-000 ==========
ErrorCode KEFU_MESSAGE_NOT_EXISTS = new ErrorCode(1_013_020_000, "客服消息不存在");
}

View File

@ -0,0 +1,15 @@
package cn.iocoder.yudao.module.promotion.enums;
/**
* Promotion WebSocket
*
* @author HUIHUI
*/
public interface WebSocketMessageTypeConstants {
// ======================= mall 客服 =======================
String KEFU_MESSAGE_TYPE = "kefu_message_type"; // 客服消息类型
String KEFU_MESSAGE_ADMIN_READ = "kefu_message_read_status_change"; // 客服消息管理员已读
}

View File

@ -0,0 +1,45 @@
package cn.iocoder.yudao.module.promotion.enums.kefu;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
*
*
* @author HUIHUI
*/
@AllArgsConstructor
@Getter
public enum KeFuMessageContentTypeEnum implements IntArrayValuable {
TEXT(1, "文本消息"),
IMAGE(2, "图片消息"),
VOICE(3, "语音消息"),
VIDEO(4, "视频消息"),
SYSTEM(5, "系统消息"),
// ========== 商城特殊消息 ==========
PRODUCT(10, "商品消息"),
ORDER(11, "订单消息");
private static final int[] ARRAYS = Arrays.stream(values()).mapToInt(KeFuMessageContentTypeEnum::getType).toArray();
/**
*
*/
private final Integer type;
/**
*
*/
private final String name;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -0,0 +1,69 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
@Tag(name = "管理后台 - 客服会话")
@RestController
@RequestMapping("/promotion/kefu-conversation")
@Validated
public class KeFuConversationController {
@Resource
private KeFuConversationService conversationService;
@Resource
private MemberUserApi memberUserApi;
@PutMapping("/update-conversation-pinned")
@Operation(summary = "置顶/取消置顶客服会话")
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:update')")
public CommonResult<Boolean> updateConversationPinned(@Valid @RequestBody KeFuConversationUpdatePinnedReqVO updateReqVO) {
conversationService.updateConversationPinnedByAdmin(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除客服会话")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:delete')")
public CommonResult<Boolean> deleteConversation(@RequestParam("id") Long id) {
conversationService.deleteKefuConversation(id);
return success(true);
}
@GetMapping("/list")
@Operation(summary = "获得客服会话列表")
@PreAuthorize("@ss.hasPermission('promotion:kefu-conversation:query')")
public CommonResult<List<KeFuConversationRespVO>> getConversationList() {
// 查询会话列表
List<KeFuConversationRespVO> respList = BeanUtils.toBean(conversationService.getKefuConversationList(),
KeFuConversationRespVO.class);
// 拼接数据
Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(convertSet(respList, KeFuConversationRespVO::getUserId));
respList.forEach(item-> findAndThen(userMap, item.getUserId(),
memberUser-> item.setUserAvatar(memberUser.getAvatar()).setUserNickname(memberUser.getNickname())));
return success(respList);
}
}

View File

@ -0,0 +1,75 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - 客服消息")
@RestController
@RequestMapping("/promotion/kefu-message")
@Validated
public class KeFuMessageController {
@Resource
private KeFuMessageService messageService;
@Resource
private AdminUserApi adminUserApi;
@PostMapping("/send")
@Operation(summary = "发送客服消息")
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:send')")
public CommonResult<Long> sendKeFuMessage(@Valid @RequestBody KeFuMessageSendReqVO sendReqVO) {
sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.ADMIN.getValue()); // 设置用户编号和类型
return success(messageService.sendKefuMessage(sendReqVO));
}
@PutMapping("/update-read-status")
@Operation(summary = "更新客服消息已读状态")
@Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:update')")
public CommonResult<Boolean> updateKeFuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
messageService.updateKeFuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.ADMIN.getValue());
return success(true);
}
@GetMapping("/page")
@Operation(summary = "获得客服消息分页")
@PreAuthorize("@ss.hasPermission('promotion:kefu-message:query')")
public CommonResult<PageResult<KeFuMessageRespVO>> getKeFuMessagePage(@Valid KeFuMessagePageReqVO pageReqVO) {
// 获得数据
PageResult<KeFuMessageDO> pageResult = messageService.getKeFuMessagePage(pageReqVO);
// 拼接数据
PageResult<KeFuMessageRespVO> result = BeanUtils.toBean(pageResult, KeFuMessageRespVO.class);
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSet(filterList(result.getList(),
item -> UserTypeEnum.ADMIN.getValue().equals(item.getSenderType())), KeFuMessageRespVO::getSenderId));
result.getList().forEach(item-> findAndThen(userMap, item.getSenderId(),
user -> item.setSenderAvatar(user.getAvatar())));
return success(result);
}
}

View File

@ -0,0 +1,46 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 客服会话 Response VO")
@Data
public class KeFuConversationRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24988")
private Long id;
@Schema(description = "会话所属用户", requiredMode = Schema.RequiredMode.REQUIRED, example = "8300")
private Long userId;
@Schema(description = "会话所属用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://yudao.com/images/avatar.jpg")
private String userAvatar;
@Schema(description = "会话所属用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
private String userNickname;
@Schema(description = "最后聊天时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime lastMessageTime;
@Schema(description = "最后聊天内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "嗨,您好啊")
private String lastMessageContent;
@Schema(description = "最后发送的消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer lastMessageContentType;
@Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
private Boolean adminPinned;
@Schema(description = "用户是否可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean userDeleted;
@Schema(description = "管理员是否可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean adminDeleted;
@Schema(description = "管理员未读消息数", requiredMode = Schema.RequiredMode.REQUIRED, example = "6")
private Integer adminUnreadMessageCount;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Schema(description = "管理后台 - 客服会话置顶 Request VO")
@Data
public class KeFuConversationUpdatePinnedReqVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
@NotNull(message = "会话编号不能为空")
private Long id;
@Schema(description = "管理端置顶", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
@NotNull(message = "管理端置顶不能为空")
private Boolean adminPinned;
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;
@Schema(description = "管理后台 - 客服消息分页 Request VO")
@Data
@ToString(callSuper = true)
public class KeFuMessagePageReqVO extends PageParam {
@Schema(description = "会话编号", example = "12580")
private Long conversationId;
}

View File

@ -0,0 +1,44 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 客服消息 Response VO")
@Data
public class KeFuMessageRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
private Long id;
@Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580")
private Long conversationId;
@Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571")
private Long senderId;
@Schema(description = "发送人头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://yudao.com/images/avatar.jpg")
private String senderAvatar;
@Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer senderType;
@Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124")
private Long receiverId;
@Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
private Integer receiverType;
@Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer contentType;
@Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
private String content;
@Schema(description = "是否已读", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Boolean readStatus;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Schema(description = "管理后台 - 发送客服消息 Request VO")
@Data
public class KeFuMessageSendReqVO {
@Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580")
@NotNull(message = "会话编号不能为空")
private Long conversationId;
@Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "消息类型不能为空")
private Integer contentType;
@Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "消息不能为空")
private String content;
// ========== 后端设置的参数,前端无需传递 ==========
@Schema(description = "发送人编号", example = "24571", hidden = true)
private Long senderId;
@Schema(description = "发送人类型", example = "1", hidden = true)
private Integer senderType;
}

View File

@ -78,9 +78,9 @@ public class SeckillConfigController {
return success(SeckillConfigConvert.INSTANCE.convertList(list));
}
@GetMapping("/list-all-simple")
@GetMapping("/simple-list")
@Operation(summary = "获得所有开启状态的秒杀时段精简列表", description = "主要用于前端的下拉选项")
public CommonResult<List<SeckillConfigSimpleRespVO>> getListAllSimple() {
public CommonResult<List<SeckillConfigSimpleRespVO>> getSeckillConfigSimpleList() {
List<SeckillConfigDO> list = seckillConfigService.getSeckillConfigListByStatus(
CommonStatusEnum.ENABLE.getStatus());
return success(SeckillConfigConvert.INSTANCE.convertList1(list));

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.service.kefu.KeFuMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "用户 APP - 客服消息")
@RestController
@RequestMapping("/promotion/kefu-message")
@Validated
public class AppKeFuMessageController {
@Resource
private KeFuMessageService kefuMessageService;
@PostMapping("/send")
@Operation(summary = "发送客服消息")
@PreAuthenticated
public CommonResult<Long> sendKefuMessage(@Valid @RequestBody AppKeFuMessageSendReqVO sendReqVO) {
sendReqVO.setSenderId(getLoginUserId()).setSenderType(UserTypeEnum.MEMBER.getValue()); // 设置用户编号和类型
return success(kefuMessageService.sendKefuMessage(sendReqVO));
}
@PutMapping("/update-read-status")
@Operation(summary = "更新客服消息已读状态")
@Parameter(name = "conversationId", description = "会话编号", required = true)
@PreAuthenticated
public CommonResult<Boolean> updateKefuMessageReadStatus(@RequestParam("conversationId") Long conversationId) {
kefuMessageService.updateKeFuMessageReadStatus(conversationId, getLoginUserId(), UserTypeEnum.MEMBER.getValue());
return success(true);
}
@GetMapping("/page")
@Operation(summary = "获得客服消息分页")
@PreAuthenticated
public CommonResult<PageResult<KeFuMessageRespVO>> getKefuMessagePage(@Valid AppKeFuMessagePageReqVO pageReqVO) {
PageResult<KeFuMessageDO> pageResult = kefuMessageService.getKeFuMessagePage(pageReqVO, getLoginUserId());
return success(BeanUtils.toBean(pageResult, KeFuMessageRespVO.class));
}
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;
@Schema(description = "用户 App - 客服消息分页 Request VO")
@Data
@ToString(callSuper = true)
public class AppKeFuMessagePageReqVO extends PageParam {
@Schema(description = "会话编号", example = "12580")
private Long conversationId;
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "用户 App - 客服消息 Response VO")
@Data
public class AppKeFuMessageRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "23202")
private Long id;
@Schema(description = "会话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "12580")
private Long conversationId;
@Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571")
private Long senderId;
@Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer senderType;
@Schema(description = "接收人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "29124")
private Long receiverId;
@Schema(description = "接收人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
private Integer receiverType;
@Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer contentType;
@Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
private String content;
@Schema(description = "是否已读", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Boolean readStatus;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Schema(description = "用户 App - 发送客服消息 Request VO")
@Data
public class AppKeFuMessageSendReqVO {
@Schema(description = "消息类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "消息类型不能为空")
private Integer contentType;
@Schema(description = "消息", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "消息不能为空")
private String content;
// ========== 后端设置的参数,前端无需传递 ==========
@Schema(description = "发送人编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24571", hidden = true)
private Long senderId;
@Schema(description = "发送人类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1", hidden = true)
private Integer senderType;
}

View File

@ -0,0 +1 @@
package cn.iocoder.yudao.module.promotion.controller.app.kefu.vo;

View File

@ -0,0 +1,83 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.kefu;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.promotion.enums.kefu.KeFuMessageContentTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.time.LocalDateTime;
/**
* DO
*
* @author HUIHUI
*/
@TableName("promotion_kefu_conversation")
@KeySequence("promotion_kefu_conversation_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KeFuConversationDO extends BaseDO {
/**
*
*/
@TableId
private Long id;
/**
*
*
* {@link MemberUserRespDTO#getId()}
*/
private Long userId;
/**
*
*/
private LocalDateTime lastMessageTime;
/**
*
*/
private String lastMessageContent;
/**
*
*
* {@link KeFuMessageContentTypeEnum}
*/
private Integer lastMessageContentType;
//======================= 会话操作相关 =======================
/**
*
*/
private Boolean adminPinned;
/**
*
*
* false -
* true - true
*/
private Boolean userDeleted;
/**
*
*
* false -
* true - true
*/
private Boolean adminDeleted;
/**
*
*
*
*/
private Integer adminUnreadMessageCount;
}

View File

@ -0,0 +1,81 @@
package cn.iocoder.yudao.module.promotion.dal.dataobject.kefu;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.promotion.enums.kefu.KeFuMessageContentTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
/**
* DO
*
* @author HUIHUI
*/
@TableName("promotion_kefu_message")
@KeySequence("promotion_kefu_message_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class KeFuMessageDO extends BaseDO {
/**
*
*/
@TableId
private Long id;
/**
*
*
* {@link KeFuConversationDO#getId()}
*/
private Long conversationId;
/**
*
*
*
*/
private Long senderId;
/**
*
*
* {@link UserTypeEnum}
*/
private Integer senderType;
/**
*
*
*
*/
private Long receiverId;
/**
*
*
* {@link UserTypeEnum}
*/
private Integer receiverType;
/**
*
*
* {@link KeFuMessageContentTypeEnum}
*/
private Integer contentType;
/**
*
*/
private String content;
//======================= 消息相关状态 =======================
/**
* /
*/
private Boolean readStatus;
}

View File

@ -0,0 +1,35 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.kefu;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* Mapper
*
* @author HUIHUI
*/
@Mapper
public interface KeFuConversationMapper extends BaseMapperX<KeFuConversationDO> {
default List<KeFuConversationDO> selectConversationList() {
return selectList(new LambdaQueryWrapperX<KeFuConversationDO>()
.eq(KeFuConversationDO::getAdminDeleted, Boolean.FALSE)
.orderByDesc(KeFuConversationDO::getCreateTime));
}
default void updateAdminUnreadMessageCountIncrement(Long id) {
update(new LambdaUpdateWrapper<KeFuConversationDO>()
.eq(KeFuConversationDO::getId, id)
.setSql("admin_unread_message_count = admin_unread_message_count + 1"));
}
default KeFuConversationDO selectByUserId(Long userId) {
return selectOne(KeFuConversationDO::getUserId, userId);
}
}

View File

@ -0,0 +1,49 @@
package cn.iocoder.yudao.module.promotion.dal.mysql.kefu;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.List;
/**
* Mapper
*
* @author HUIHUI
*/
@Mapper
public interface KeFuMessageMapper extends BaseMapperX<KeFuMessageDO> {
default PageResult<KeFuMessageDO> selectPage(KeFuMessagePageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<KeFuMessageDO>()
.eqIfPresent(KeFuMessageDO::getConversationId, reqVO.getConversationId())
.orderByDesc(KeFuMessageDO::getCreateTime));
}
default List<KeFuMessageDO> selectListByConversationIdAndUserTypeAndReadStatus(Long conversationId, Integer userType,
Boolean readStatus) {
return selectList(new LambdaQueryWrapper<KeFuMessageDO>()
.eq(KeFuMessageDO::getConversationId, conversationId)
.ne(KeFuMessageDO::getSenderType, userType) // 管理员:查询出未读的会员消息,会员:查询出未读的客服消息
.eq(KeFuMessageDO::getReadStatus, readStatus));
}
default void updateReadStatusBatchByIds(Collection<Long> ids, KeFuMessageDO keFuMessageDO) {
update(keFuMessageDO, new LambdaUpdateWrapper<KeFuMessageDO>()
.in(KeFuMessageDO::getId, ids));
}
default PageResult<KeFuMessageDO> selectPage(AppKeFuMessagePageReqVO pageReqVO) {
return selectPage(pageReqVO, new LambdaQueryWrapperX<KeFuMessageDO>()
.eqIfPresent(KeFuMessageDO::getConversationId, pageReqVO.getConversationId())
.orderByDesc(KeFuMessageDO::getCreateTime));
}
}

View File

@ -1,15 +1,18 @@
package cn.iocoder.yudao.module.promotion.framework.rpc.config;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.product.api.category.ProductCategoryApi;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.trade.api.order.TradeOrderApi;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {ProductSkuApi.class, ProductSpuApi.class, ProductCategoryApi.class,
MemberUserApi.class, TradeOrderApi.class})
MemberUserApi.class, TradeOrderApi.class, AdminUserApi.class,
WebSocketSenderApi.class})
public class RpcConfiguration {
}

View File

@ -0,0 +1,85 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import java.util.List;
/**
* Service
*
* @author HUIHUI
*/
public interface KeFuConversationService {
/**
*
*
* @param id
*/
void deleteKefuConversation(Long id);
/**
*
*
* @param updateReqVO
*/
void updateConversationPinnedByAdmin(KeFuConversationUpdatePinnedReqVO updateReqVO);
/**
*
*
* @param kefuMessage
*/
void updateConversationLastMessage(KeFuMessageDO kefuMessage);
/**
*
*
* @param id
*/
void updateAdminUnreadMessageCountToZero(Long id);
/**
*
*
* @param id
* @param adminDeleted
*/
void updateConversationAdminDeleted(Long id, Boolean adminDeleted);
/**
*
*
* @return
*/
List<KeFuConversationDO> getKefuConversationList();
/**
*
*
*
*
* @param userId
* @return
*/
KeFuConversationDO getOrCreateConversation(Long userId);
/**
*
*
* @param id
* @return
*/
KeFuConversationDO validateKefuConversationExists(Long id);
/**
*
*
* @param userId
* @return
*/
KeFuConversationDO getConversationByUserId(Long userId);
}

View File

@ -0,0 +1,118 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.conversation.KeFuConversationUpdatePinnedReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.kefu.KeFuConversationMapper;
import cn.iocoder.yudao.module.promotion.enums.kefu.KeFuMessageContentTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
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.promotion.enums.ErrorCodeConstants.KEFU_CONVERSATION_NOT_EXISTS;
/**
* Service
*
* @author HUIHUI
*/
@Service
@Validated
public class KeFuConversationServiceImpl implements KeFuConversationService {
@Resource
private KeFuConversationMapper conversationMapper;
@Override
public void deleteKefuConversation(Long id) {
// 校验存在
validateKefuConversationExists(id);
// 只有管理员端可以删除会话,也不真的删,只是管理员端看不到啦
conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminDeleted(Boolean.TRUE));
}
@Override
public void updateConversationPinnedByAdmin(KeFuConversationUpdatePinnedReqVO updateReqVO) {
// 校验存在
validateKefuConversationExists(updateReqVO.getId());
// 更新管理员会话置顶状态
conversationMapper.updateById(new KeFuConversationDO().setId(updateReqVO.getId()).setAdminPinned(updateReqVO.getAdminPinned()));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateConversationLastMessage(KeFuMessageDO kefuMessage) {
// 1.1 校验会话是否存在
KeFuConversationDO conversation = validateKefuConversationExists(kefuMessage.getConversationId());
// 1.2 更新会话消息冗余
conversationMapper.updateById(new KeFuConversationDO().setId(kefuMessage.getConversationId())
.setLastMessageTime(kefuMessage.getCreateTime()).setLastMessageContent(kefuMessage.getContent())
.setLastMessageContentType(kefuMessage.getContentType()));
// 2.1 更新管理员未读消息数
if (UserTypeEnum.MEMBER.getValue().equals(kefuMessage.getSenderType())) {
conversationMapper.updateAdminUnreadMessageCountIncrement(kefuMessage.getConversationId());
}
// 2.2 会员用户发送消息时,如果管理员删除过会话则进行恢复
if (Boolean.TRUE.equals(conversation.getAdminDeleted())) {
updateConversationAdminDeleted(kefuMessage.getConversationId(), Boolean.FALSE);
}
}
@Override
public void updateAdminUnreadMessageCountToZero(Long id) {
// 校验存在
validateKefuConversationExists(id);
// 管理员未读消息数归零
conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminUnreadMessageCount(0));
}
@Override
public void updateConversationAdminDeleted(Long id, Boolean adminDeleted) {
conversationMapper.updateById(new KeFuConversationDO().setId(id).setAdminDeleted(adminDeleted));
}
@Override
public List<KeFuConversationDO> getKefuConversationList() {
return conversationMapper.selectConversationList();
}
@Override
public KeFuConversationDO getOrCreateConversation(Long userId) {
KeFuConversationDO conversation = conversationMapper.selectOne(KeFuConversationDO::getUserId, userId);
// 没有历史会话,则初始化一个新会话
if (conversation == null) {
conversation = new KeFuConversationDO().setUserId(userId).setLastMessageTime(LocalDateTime.now())
.setLastMessageContent(StrUtil.EMPTY).setLastMessageContentType(KeFuMessageContentTypeEnum.TEXT.getType())
.setAdminPinned(Boolean.FALSE).setUserDeleted(Boolean.FALSE).setAdminDeleted(Boolean.FALSE)
.setAdminUnreadMessageCount(0);
conversationMapper.insert(conversation);
}
return conversation;
}
@Override
public KeFuConversationDO validateKefuConversationExists(Long id) {
KeFuConversationDO conversation = conversationMapper.selectById(id);
if (conversation == null) {
throw exception(KEFU_CONVERSATION_NOT_EXISTS);
}
return conversation;
}
@Override
public KeFuConversationDO getConversationByUserId(Long userId) {
return conversationMapper.selectByUserId(userId);
}
}

View File

@ -0,0 +1,60 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import jakarta.validation.Valid;
/**
* Service
*
* @author HUIHUI
*/
public interface KeFuMessageService {
/**
*
*
* @param sendReqVO
* @return
*/
Long sendKefuMessage(@Valid KeFuMessageSendReqVO sendReqVO);
/**
*
*
* @param sendReqVO
* @return
*/
Long sendKefuMessage(AppKeFuMessageSendReqVO sendReqVO);
/**
*
*
* @param conversationId
* @param userId
* @param userType
*/
void updateKeFuMessageReadStatus(Long conversationId, Long userId, Integer userType);
/**
*
*
* @param pageReqVO
* @return
*/
PageResult<KeFuMessageDO> getKeFuMessagePage(KeFuMessagePageReqVO pageReqVO);
/**
*
*
* @param pageReqVO
* @param userId
* @return
*/
PageResult<KeFuMessageDO> getKeFuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId);
}

View File

@ -0,0 +1,161 @@
package cn.iocoder.yudao.module.promotion.service.kefu;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.kefu.vo.message.KeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessagePageReqVO;
import cn.iocoder.yudao.module.promotion.controller.app.kefu.vo.message.AppKeFuMessageSendReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuConversationDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.kefu.KeFuMessageDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.kefu.KeFuMessageMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.KEFU_CONVERSATION_NOT_EXISTS;
import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ;
import static cn.iocoder.yudao.module.promotion.enums.WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE;
/**
* Service
*
* @author HUIHUI
*/
@Service
@Validated
public class KeFuMessageServiceImpl implements KeFuMessageService {
@Resource
private KeFuMessageMapper keFuMessageMapper;
@Resource
private KeFuConversationService conversationService;
@Resource
private AdminUserApi adminUserApi;
@Resource
private MemberUserApi memberUserApi;
@Resource
private WebSocketSenderApi webSocketSenderApi;
@Override
@Transactional(rollbackFor = Exception.class)
public Long sendKefuMessage(KeFuMessageSendReqVO sendReqVO) {
// 1.1 校验会话是否存在
KeFuConversationDO conversation = conversationService.validateKefuConversationExists(sendReqVO.getConversationId());
// 1.2 校验接收人是否存在
validateReceiverExist(conversation.getUserId(), UserTypeEnum.MEMBER.getValue());
// 2.1 保存消息
KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
kefuMessage.setReceiverId(conversation.getUserId()).setReceiverType(UserTypeEnum.MEMBER.getValue()); // 设置接收人
keFuMessageMapper.insert(kefuMessage);
// 2.2 更新会话消息冗余
conversationService.updateConversationLastMessage(kefuMessage);
// 3.1 发送消息给会员
getSelf().sendAsyncMessageToMember(conversation.getUserId(), KEFU_MESSAGE_TYPE, kefuMessage);
// 3.2 通知所有管理员更新对话
getSelf().sendAsyncMessageToAdmin(KEFU_MESSAGE_TYPE, kefuMessage);
return kefuMessage.getId();
}
@Override
public Long sendKefuMessage(AppKeFuMessageSendReqVO sendReqVO) {
// 1.1 设置会话编号
KeFuMessageDO kefuMessage = BeanUtils.toBean(sendReqVO, KeFuMessageDO.class);
KeFuConversationDO conversation = conversationService.getOrCreateConversation(sendReqVO.getSenderId());
kefuMessage.setConversationId(conversation.getId());
// 1.2 保存消息
keFuMessageMapper.insert(kefuMessage);
// 2. 更新会话消息冗余
conversationService.updateConversationLastMessage(kefuMessage);
// 3. 通知所有管理员更新对话
getSelf().sendAsyncMessageToAdmin(KEFU_MESSAGE_TYPE, kefuMessage);
return kefuMessage.getId();
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateKeFuMessageReadStatus(Long conversationId, Long userId, Integer userType) {
// 1.1 校验会话是否存在
KeFuConversationDO conversation = conversationService.validateKefuConversationExists(conversationId);
// 1.2 如果是会员端处理已读,需要传递 userId万一用户模拟一个 conversationId
if (UserTypeEnum.MEMBER.getValue().equals(userType) && ObjUtil.notEqual(conversation.getUserId(), userId)) {
throw exception(KEFU_CONVERSATION_NOT_EXISTS);
}
// 1.3 查询会话所有的未读消息 (tips: 多个客服,一个人点了,就都点了)
List<KeFuMessageDO> messageList = keFuMessageMapper.selectListByConversationIdAndUserTypeAndReadStatus(conversationId, userType, Boolean.FALSE);
if (CollUtil.isEmpty(messageList)) {
return;
}
// 2.1 情况二:更新未读消息状态为已读
keFuMessageMapper.updateReadStatusBatchByIds(convertSet(messageList, KeFuMessageDO::getId),
new KeFuMessageDO().setReadStatus(Boolean.TRUE));
// 2.2 将管理员未读消息计数更新为零
conversationService.updateAdminUnreadMessageCountToZero(conversationId);
// 2.3 发送消息通知会员,管理员已读 -> 会员更新发送的消息状态
KeFuMessageDO keFuMessage = getFirst(filterList(messageList, message -> UserTypeEnum.MEMBER.getValue().equals(message.getSenderType())));
assert keFuMessage != null; // 断言避免警告
getSelf().sendAsyncMessageToMember(keFuMessage.getSenderId(), KEFU_MESSAGE_ADMIN_READ, StrUtil.EMPTY);
// 2.4 通知所有管理员消息已读
getSelf().sendAsyncMessageToAdmin(KEFU_MESSAGE_ADMIN_READ, StrUtil.EMPTY);
}
private void validateReceiverExist(Long receiverId, Integer receiverType) {
if (UserTypeEnum.ADMIN.getValue().equals(receiverType)) {
adminUserApi.validateUser(receiverId);
}
if (UserTypeEnum.MEMBER.getValue().equals(receiverType)) {
memberUserApi.validateUser(receiverId);
}
}
@Async
public void sendAsyncMessageToMember(Long userId, String messageType, Object content) {
webSocketSenderApi.sendObject(UserTypeEnum.MEMBER.getValue(), userId, messageType, content);
}
@Async
public void sendAsyncMessageToAdmin(String messageType, Object content) {
webSocketSenderApi.sendObject(UserTypeEnum.ADMIN.getValue(), messageType, content);
}
@Override
public PageResult<KeFuMessageDO> getKeFuMessagePage(KeFuMessagePageReqVO pageReqVO) {
return keFuMessageMapper.selectPage(pageReqVO);
}
@Override
public PageResult<KeFuMessageDO> getKeFuMessagePage(AppKeFuMessagePageReqVO pageReqVO, Long userId) {
// 1. 获得客服会话
KeFuConversationDO conversation = conversationService.getConversationByUserId(userId);
if (conversation == null) {
return PageResult.empty();
}
// 2. 设置会话编号
pageReqVO.setConversationId(conversation.getId());
return keFuMessageMapper.selectPage(pageReqVO);
}
private KeFuMessageServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
}

View File

@ -20,13 +20,13 @@ import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageWithdrawSumma
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Map;
@ -50,7 +50,6 @@ public class AppBrokerageUserController {
private BrokerageRecordService brokerageRecordService;
@Resource
private BrokerageWithdrawService brokerageWithdrawService;
@Resource
private MemberUserApi memberUserApi;
@ -58,7 +57,7 @@ public class AppBrokerageUserController {
@Operation(summary = "获得个人分销信息")
@PreAuthenticated
public CommonResult<AppBrokerageUserRespVO> getBrokerageUser() {
Optional<BrokerageUserDO> user = Optional.ofNullable(brokerageUserService.getBrokerageUser(getLoginUserId()));
Optional<BrokerageUserDO> user = Optional.ofNullable(brokerageUserService.getOrCreateBrokerageUser(getLoginUserId()));
// 返回数据
AppBrokerageUserRespVO respVO = new AppBrokerageUserRespVO()
.setBrokerageEnabled(user.map(BrokerageUserDO::getBrokerageEnabled).orElse(false))
@ -79,21 +78,22 @@ public class AppBrokerageUserController {
@PreAuthenticated
public CommonResult<AppBrokerageUserMySummaryRespVO> getBrokerageUserSummary() {
// 查询当前登录用户信息
BrokerageUserDO brokerageUser = brokerageUserService.getBrokerageUser(getLoginUserId());
Long userId = getLoginUserId();
BrokerageUserDO brokerageUser = brokerageUserService.getBrokerageUser(userId);
// 统计用户昨日的佣金
LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
LocalDateTime beginTime = LocalDateTimeUtil.beginOfDay(yesterday);
LocalDateTime endTime = LocalDateTimeUtil.endOfDay(yesterday);
Integer yesterdayPrice = brokerageRecordService.getSummaryPriceByUserId(brokerageUser.getId(),
Integer yesterdayPrice = brokerageRecordService.getSummaryPriceByUserId(userId,
BrokerageRecordBizTypeEnum.ORDER, BrokerageRecordStatusEnum.SETTLEMENT, beginTime, endTime);
// 统计用户提现的佣金
Integer withdrawPrice = brokerageWithdrawService.getWithdrawSummaryListByUserId(Collections.singleton(brokerageUser.getId()),
Integer withdrawPrice = brokerageWithdrawService.getWithdrawSummaryListByUserId(Collections.singleton(userId),
BrokerageWithdrawStatusEnum.AUDIT_SUCCESS).stream()
.findFirst().map(BrokerageWithdrawSummaryRespBO::getPrice).orElse(0);
// 统计分销用户数量(一级)
Long firstBrokerageUserCount = brokerageUserService.getBrokerageUserCountByBindUserId(brokerageUser.getId(), 1);
Long firstBrokerageUserCount = brokerageUserService.getBrokerageUserCountByBindUserId(userId, 1);
// 统计分销用户数量(二级)
Long secondBrokerageUserCount = brokerageUserService.getBrokerageUserCountByBindUserId(brokerageUser.getId(), 2);
Long secondBrokerageUserCount = brokerageUserService.getBrokerageUserCountByBindUserId(userId, 2);
// 拼接返回
return success(BrokerageUserConvert.INSTANCE.convert(yesterdayPrice, withdrawPrice, firstBrokerageUserCount, secondBrokerageUserCount, brokerageUser));

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -8,7 +7,7 @@ import jakarta.validation.constraints.NotNull;
@Schema(description = "应用 App - 绑定推广员 Request VO")
@Data
public class AppBrokerageUserBindReqVO extends PageParam {
public class AppBrokerageUserBindReqVO {
@Schema(description = "推广员编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
@NotNull(message = "推广员编号不能为空")

View File

@ -7,8 +7,8 @@ import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokera
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankByUserCountRespVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO;
import jakarta.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
@ -67,6 +67,14 @@ public interface BrokerageUserService {
*/
BrokerageUserDO getBindBrokerageUser(Long id);
/**
*
*
* @param id
* @return
*/
BrokerageUserDO getOrCreateBrokerageUser(Long id);
/**
*
*
@ -104,8 +112,8 @@ public interface BrokerageUserService {
/**
* 广
*
* @param userId
* @param bindUserId 广
* @param userId
* @param bindUserId 广
* @return
*/
boolean bindBrokerageUser(@NotNull Long userId, @NotNull Long bindUserId);
@ -134,4 +142,5 @@ public interface BrokerageUserService {
* @return
*/
PageResult<AppBrokerageUserChildSummaryRespVO> getBrokerageUserChildSummaryPage(AppBrokerageUserChildSummaryPageReqVO pageReqVO, Long userId);
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
@ -25,10 +26,10 @@ import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum;
import cn.iocoder.yudao.module.trade.service.config.TradeConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
@ -127,6 +128,19 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
.orElse(null);
}
@Override
public BrokerageUserDO getOrCreateBrokerageUser(Long id) {
BrokerageUserDO brokerageUser = brokerageUserMapper.selectById(id);
// 特殊:人人分销的情况下,如果分销人为空则创建分销人
if (brokerageUser == null && ObjUtil.equal(BrokerageEnabledConditionEnum.ALL.getCondition(),
tradeConfigService.getTradeConfig().getBrokerageEnabledCondition())) {
brokerageUser = new BrokerageUserDO().setId(id).setBrokerageEnabled(true).setBrokeragePrice(0)
.setBrokerageTime(LocalDateTime.now()).setFrozenPrice(0);
brokerageUserMapper.insert(brokerageUser);
}
return brokerageUser;
}
@Override
public boolean updateUserPrice(Long id, Integer price) {
if (price > 0) {
@ -184,7 +198,6 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
if (BrokerageEnabledConditionEnum.ALL.getCondition().equals(enabledCondition)) { // 人人分销:用户默认就有分销资格
brokerageUser.setBrokerageEnabled(true).setBrokerageTime(LocalDateTime.now());
}
brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now());
brokerageUserMapper.insert(fillBindUserData(bindUserId, brokerageUser));
} else {
brokerageUserMapper.updateById(fillBindUserData(bindUserId, new BrokerageUserDO().setId(userId)));
@ -294,18 +307,23 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
private void validateCanBindUser(BrokerageUserDO user, Long bindUserId) {
// 校验要绑定的用户有无推广资格
BrokerageUserDO bindUser = brokerageUserMapper.selectById(bindUserId);
// 1.1 校验推广人是否存在
MemberUserRespDTO bindUserInfo = memberUserApi.getUser(bindUserId).getCheckedData();
if (bindUserInfo == null) {
throw exception(BROKERAGE_USER_NOT_EXISTS);
}
// 1.2 校验要绑定的用户有无推广资格
BrokerageUserDO bindUser = getOrCreateBrokerageUser(bindUserId);
if (bindUser == null || BooleanUtil.isFalse(bindUser.getBrokerageEnabled())) {
throw exception(BROKERAGE_BIND_USER_NOT_ENABLED);
}
// 校验绑定自己
// 2. 校验绑定自己
if (Objects.equals(user.getId(), bindUserId)) {
throw exception(BROKERAGE_BIND_SELF);
}
// 下级不能绑定自己的上级
// 3. 下级不能绑定自己的上级
for (int i = 0; i <= Short.MAX_VALUE; i++) {
if (Objects.equals(bindUser.getBindUserId(), user.getId())) {
throw exception(BROKERAGE_BIND_LOOP);

View File

@ -53,4 +53,9 @@ public interface MemberUserApi {
@Parameter(name = "mobile", description = "基于手机号,精准匹配用户", required = true, example = "1560")
CommonResult<MemberUserRespDTO> getUserByMobile(@RequestParam("mobile") String mobile);
@GetMapping(PREFIX + "/valid")
@Operation(summary = "校验用户是否存在")
@Parameter(name = "id", description = "用户编号", required = true, example = "1")
CommonResult<Boolean> validateUser(@RequestParam("id") Long id);
}

View File

@ -12,7 +12,9 @@ import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;
/**
* API
@ -47,4 +49,13 @@ public class MemberUserApiImpl implements MemberUserApi {
return success(MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile)));
}
@Override
public CommonResult<Boolean> validateUser(Long id) {
MemberUserDO user = userService.getUser(id);
if (user == null) {
throw exception(USER_MOBILE_NOT_EXISTS);
}
return success(true);
}
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.member.controller.app.social;
import cn.hutool.core.codec.Base64;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -7,18 +8,20 @@ import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserBindReqVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserRespVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialUserUnbindReqVO;
import cn.iocoder.yudao.module.member.controller.app.social.vo.AppSocialWxQrcodeReqVO;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserUnbindReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -31,6 +34,8 @@ public class AppSocialUserController {
@Resource
private SocialUserApi socialUserApi;
@Resource
private SocialClientApi socialClientApi;
@PostMapping("/bind")
@Operation(summary = "社交绑定,使用 code 授权码")
@ -47,7 +52,7 @@ public class AppSocialUserController {
public CommonResult<Boolean> socialUnbind(@RequestBody AppSocialUserUnbindReqVO reqVO) {
SocialUserUnbindReqDTO reqDTO = new SocialUserUnbindReqDTO(getLoginUserId(), UserTypeEnum.MEMBER.getValue(),
reqVO.getType(), reqVO.getOpenid());
socialUserApi.unbindSocialUser(reqDTO).getCheckedData();
socialUserApi.unbindSocialUser(reqDTO);
return success(true);
}
@ -60,4 +65,11 @@ public class AppSocialUserController {
return success(BeanUtils.toBean(socialUser, AppSocialUserRespVO.class));
}
@PostMapping("/wxa-qrcode")
@Operation(summary = "获得微信小程序码(base64 image)")
public CommonResult<String> getWxaQrcode(@RequestBody @Valid AppSocialWxQrcodeReqVO reqVO) {
byte[] wxQrcode = socialClientApi.getWxaQrcode(BeanUtils.toBean(reqVO, SocialWxQrcodeReqDTO.class)).getCheckedData();
return success(Base64.encode(wxQrcode));
}
}

View File

@ -0,0 +1,38 @@
package cn.iocoder.yudao.module.member.controller.app.social.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
@Schema(description = "用户 APP - 获得获取小程序码 Request VO")
@Data
public class AppSocialWxQrcodeReqVO {
/**
* scene
*/
@Schema(description = "场景值", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001")
private String scene;
/**
* page pages/index/index /scene
* scancode_time
*/
@Schema(description = "页面路径", requiredMode = Schema.RequiredMode.REQUIRED, example = "pages/goods/index")
@NotEmpty(message = "页面路径不能为空")
private String path;
@Schema(description = "二维码宽度", requiredMode = Schema.RequiredMode.REQUIRED, example = "430")
private Integer width;
@Schema(description = "是/否自动配置线条颜色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean autoColor;
@Schema(description = "是/否检查 page 是否存在", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean checkPath;
@Schema(description = "是/否需要透明底色", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
private Boolean hyaline;
}

View File

@ -11,6 +11,9 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
public class AppMemberUserInfoRespVO {
@Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long id;
@Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
private String nickname;

View File

@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.member.dal.dataobject.tag.MemberTagDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@ -27,8 +28,12 @@ public interface MemberUserConvert {
AppMemberUserInfoRespVO convert(MemberUserDO bean);
@Mapping(source = "level", target = "level")
@Mapping(source = "bean.experience", target = "experience")
@Mappings({
@Mapping(source = "level", target = "level"),
@Mapping(source = "bean.id", target = "id"),
@Mapping(source = "bean.experience", target = "experience")
})
AppMemberUserInfoRespVO convert(MemberUserDO bean, MemberLevelDO level);
MemberUserRespDTO convert2(MemberUserDO bean);

View File

@ -3,11 +3,13 @@ package cn.iocoder.yudao.module.system.api.social;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import cn.iocoder.yudao.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -47,4 +49,8 @@ public interface SocialClientApi {
CommonResult<SocialWxPhoneNumberInfoRespDTO> getWxMaPhoneNumberInfo(@RequestParam("userType") Integer userType,
@RequestParam("phoneCode") String phoneCode);
@GetMapping(PREFIX + "/get-wxa-qrcode")
@Operation(summary = "获得小程序二维码")
CommonResult<byte[]> getWxaQrcode(@Valid SocialWxQrcodeReqDTO reqVO);
}

View File

@ -0,0 +1,57 @@
package cn.iocoder.yudao.module.system.api.social.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
/**
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html"></a>
*/
@Schema(description = "RPC 服务 - 获得获取小程序码 Request DTO")
@Data
public class SocialWxQrcodeReqDTO {
/**
* scene
*/
public static final String SCENE = "";
/**
*
*/
public static final Integer WIDTH = 430;
/**
* 线
*/
public static final Boolean AUTO_COLOR = true;
/**
* page
*/
public static final Boolean CHECK_PATH = true;
/**
*
*
* hyaline true
*/
public static final Boolean HYALINE = true;
@Schema(description = "场景", requiredMode = Schema.RequiredMode.REQUIRED, example = "1001")
@NotEmpty(message = "场景不能为空")
private String scene;
@Schema(description = "页面路径", requiredMode = Schema.RequiredMode.REQUIRED, example = "pages/goods/index")
@NotEmpty(message = "页面路径不能为空")
private String path;
@Schema(description = "二维码宽度", example = "430")
private Integer width;
@Schema(description = "是否需要透明底色", example = "true")
private Boolean autoColor;
@Schema(description = "是否检查 page 是否存在", example = "true")
private Boolean checkPath;
@Schema(description = "是否需要透明底色", example = "true")
private Boolean hyaline;
}

View File

@ -28,4 +28,7 @@ public class AdminUserRespDTO implements VO {
@Schema(description = "手机号码", requiredMode = Schema.RequiredMode.REQUIRED, example = "15601691300")
private String mobile;
@Schema(description = "用户头像", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://www.iocoder.cn/1.png")
private String avatar;
}

View File

@ -120,8 +120,10 @@ public interface ErrorCodeConstants {
ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1_002_018_001, "社交授权失败,找不到对应的用户");
ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_002_018_200, "获得手机号失败");
ErrorCode SOCIAL_CLIENT_NOT_EXISTS = new ErrorCode(1_002_018_201, "社交客户端不存在");
ErrorCode SOCIAL_CLIENT_UNIQUE = new ErrorCode(1_002_018_202, "社交客户端已存在配置");
ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_QRCODE_ERROR = new ErrorCode(1_002_018_201, "获得小程序码失败");
ErrorCode SOCIAL_CLIENT_NOT_EXISTS = new ErrorCode(1_002_018_202, "社交客户端不存在");
ErrorCode SOCIAL_CLIENT_UNIQUE = new ErrorCode(1_002_018_203, "社交客户端已存在配置");
// ========== OAuth2 客户端 1-002-020-000 =========
ErrorCode OAUTH2_CLIENT_NOT_EXISTS = new ErrorCode(1_002_020_000, "OAuth2 客户端不存在");

View File

@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import cn.iocoder.yudao.module.system.service.social.SocialClientService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import org.springframework.validation.annotation.Validated;
@ -43,4 +44,9 @@ public class SocialClientApiImpl implements SocialClientApi {
return success(BeanUtils.toBean(info, SocialWxPhoneNumberInfoRespDTO.class));
}
@Override
public CommonResult<byte[]> getWxaQrcode(SocialWxQrcodeReqDTO reqVO) {
return success(socialClientService.getWxaQrcode(reqVO));
}
}

View File

@ -42,4 +42,14 @@ public class SmsCallbackController {
return success(true);
}
@PostMapping("/huawei")
@PermitAll
@Operation(summary = "华为云短信的回调", description = "参见 https://support.huaweicloud.com/api-msgsms/sms_05_0003.html 文档")
public CommonResult<Boolean> receiveHuaweiSmsStatus(HttpServletRequest request) throws Throwable {
String text = ServletUtils.getBody(request);
smsSendService.receiveSmsStatus(SmsChannelEnum.HUAWEI.getCode(), text);
return success(true);
}
}

View File

@ -0,0 +1,221 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONArray;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT;
/**
*
*
* @author scholar
* @since 2024/6/02 11:55
*/
@Slf4j
public class HuaweiSmsClient extends AbstractSmsClient {
/**
* code
*/
public static final String API_CODE_SUCCESS = "OK";
public HuaweiSmsClient(SmsChannelProperties properties) {
super(properties);
Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
}
@Override
protected void doInit() {
}
@Override
public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId,
List<KeyValue<String, Object>> templateParams) throws Throwable {
// TODO @scholarhttps://smsapi.cn-north-4.myhuaweicloud.com:443 是不是枚举成静态变量
String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1"; //APP接入地址+接口访问URI
// 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构
// 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。
// TODO @scholar暂时只考虑中国大陆所以不需要 sender 哈
String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号
String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID
// 选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
String statusCallBack = properties.getCallbackUrl();
// TODO @scholar1是不是用 LocalDateTimeUtil.format();这样 3 行变成一行
// TODO @scholarsingerDate 叫 sdkDate 会更合适哈这样理解起来简单。另外singer 应该是 signed 么?
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String singerDate = sdf.format(new Date());
// TODO @scholar整个处理加密的过程是不是应该抽成一个 private 方法哈。这样整个调用的主干更清晰。
// ************* 步骤 1拼接规范请求串 *************
String httpRequestMethod = "POST";
String canonicalUri = "/sms/batchSendSms/v1/";
String canonicalQueryString = ""; // 查询参数为空
String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n"
+ "host:smsapi.cn-north-4.myhuaweicloud.com:443\n"
+ "x-sdk-date:" + singerDate + "\n";
// TODO @scholar静态枚举了
String signedHeaders = "content-type;host;x-sdk-date";
// TODO @scholar下面的注释可以考虑去掉
/*
* ,使 String templateParas = "";
* :"您的验证码是${NUM_6}",templateParas"[\"111111\"]"
* :"您有${NUM_2}件快递请到${TXT_20}领取",templateParas"[\"3\",\"人民公园正门\"]"
*/
// TODO @scholarCollectionUtils.convertList 可以把 4 行变成 1 行。
// TODO @scholartemplateParams 拼写错误哈
List<String> templateParas = new ArrayList<>();
for (KeyValue<String, Object> kv : templateParams) {
templateParas.add(String.valueOf(kv.getValue()));
}
// 请求Body,不携带签名名称时,signature请填null
String body = buildRequestBody(sender, mobile, templateId, templateParas, statusCallBack, null);
// TODO @scholarAssert 断言,抛出异常
if (null == body || body.isEmpty()) {
return null;
}
String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body));
String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n"
+ canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody;
// ************* 步骤 2拼接待签名字符串 *************
// TODO @scholarsha256Hex 是不是更简洁哈
String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest));
String stringToSign = "SDK-HMAC-SHA256" + "\n" + singerDate + "\n" + hashedCanonicalRequest;
// ************* 步骤 3计算签名 *************
String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign);
// ************* 步骤 4拼接 Authorization *************
String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", "
+ "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature;
// ************* 步骤 5构造HttpRequest 并执行request请求获得response *************
// TODO @scholar考虑了下还是换 hutool 的 httpUtils。因为未来 httpclient 我们可能会移除掉
HttpUriRequest postMethod = RequestBuilder.post()
.setUri(url)
.setEntity(new StringEntity(body, StandardCharsets.UTF_8))
.setHeader("Content-Type","application/x-www-form-urlencoded")
.setHeader("X-Sdk-Date", singerDate)
.setHeader("Authorization", authorization)
.build();
// TODO @scholar这种不太适合一直 new 的哈
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(postMethod);
// TODO @scholar失败的情况下的处理
// TODO @scholarsetSerialNo(Integer.toString(response.getStatusLine().getStatusCode())) 这部分,空一行。一行代码太多了,阅读性不太好哈
return new SmsSendRespDTO().setSuccess(Objects.equals(response.getStatusLine().getReasonPhrase(), API_CODE_SUCCESS)).setSerialNo(Integer.toString(response.getStatusLine().getStatusCode()))
.setApiRequestId(null).setApiCode(null).setApiMsg(null);
}
static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas,
String statusCallBack, @SuppressWarnings("SameParameterValue") String signature) {
// TODO @scholar参数不满足是不是抛出异常更好哈通过 hutool 的 Assert 去断言
if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
|| templateId.isEmpty()) {
System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
return null;
}
StringBuilder body = new StringBuilder();
appendToBody(body, "from=", sender);
appendToBody(body, "&to=", receiver);
appendToBody(body, "&templateId=", templateId);
// TODO @scholarnew JSONArray(templateParas).toString(),是不是 JsonUtils.toString 呀?
appendToBody(body, "&templateParas=", new JSONArray(templateParas).toString());
appendToBody(body, "&statusCallback=", statusCallBack);
appendToBody(body, "&signature=", signature);
return body.toString();
}
private static void appendToBody(StringBuilder body, String key, String val) {
// TODO @scholarStrUtils.isNotEmpty(val),是不是更简洁哈
if (null != val && !val.isEmpty()) {
body.append(key).append(URLEncoder.encode(val, StandardCharsets.UTF_8));
}
}
@Override
public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) {
List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(Objects.equals(status.getStatus(),"DELIVRD"))
.setErrorCode(status.getStatus()).setErrorMsg(status.getStatus())
.setMobile(status.getPhoneNumber()).setReceiveTime(status.getUpdateTime())
.setSerialNo(status.getSmsMsgId()));
}
@Override
public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable {
// 华为短信模板查询和发送短信,是不同的两套 key 和 secret与阿里、腾讯的区别较大这里模板查询校验暂不实现
// 对应文档 https://support.huaweicloud.com/api-msgsms/sms_05_0040.html
return new SmsTemplateRespDTO().setId(apiTemplateId).setContent(null)
.setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason(null);
}
/**
*
*
* <a href="https://support.huaweicloud.com/api-msgsms/sms_05_0003.html"></a>
*
* @author scholar
*/
@Data
public static class SmsReceiveStatus {
/**
* extend
*/
@JsonProperty("to")
private String phoneNumber;
/**
*
*/
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private LocalDateTime updateTime;
/**
*
*/
private String status;
/**
*
*/
private String smsMsgId;
}
}

View File

@ -78,6 +78,7 @@ public class SmsClientFactoryImpl implements SmsClientFactory {
case ALIYUN: return new AliyunSmsClient(properties);
case DEBUG_DING_TALK: return new DebugDingTalkSmsClient(properties);
case TENCENT: return new TencentSmsClient(properties);
case HUAWEI: return new HuaweiSmsClient(properties);
}
// 创建失败,错误日志 + 抛出异常
log.error("[createSmsClient][配置({}) 找不到合适的客户端实现]", properties);

View File

@ -17,7 +17,7 @@ public enum SmsChannelEnum {
DEBUG_DING_TALK("DEBUG_DING_TALK", "调试(钉钉)"),
ALIYUN("ALIYUN", "阿里云"),
TENCENT("TENCENT", "腾讯云"),
// HUA_WEI("HUA_WEI", "华为云"),
HUAWEI("HUAWEI", "华为云"),
;
/**

View File

@ -2,14 +2,14 @@ package cn.iocoder.yudao.module.system.service.social;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import com.xingyuv.jushauth.model.AuthUser;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import jakarta.validation.Valid;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
/**
* Service
@ -21,8 +21,8 @@ public interface SocialClientService {
/**
* URL
*
* @param socialType {@link SocialTypeEnum}
* @param userType
* @param socialType {@link SocialTypeEnum}
* @param userType
* @param redirectUri URL
* @return URL
*/
@ -32,9 +32,9 @@ public interface SocialClientService {
*
*
* @param socialType
* @param userType
* @param code
* @param state state
* @param userType
* @param code
* @param state state
* @return
*/
AuthUser getAuthUser(Integer socialType, Integer userType, String code, String state);
@ -45,7 +45,7 @@ public interface SocialClientService {
* JS SDK
*
* @param userType
* @param url 访 URL
* @param url 访 URL
* @return
*/
WxJsapiSignature createWxMpJsapiSignature(Integer userType, String url);
@ -55,12 +55,20 @@ public interface SocialClientService {
/**
*
*
* @param userType
* @param userType
* @param phoneCode
* @return
*/
WxMaPhoneNumberInfo getWxMaPhoneNumberInfo(Integer userType, String phoneCode);
/**
*
*
* @param reqVO
* @return
*/
byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO);
// =================== 客户端管理 ===================
/**

View File

@ -9,10 +9,12 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.cache.CacheUtils;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
@ -30,6 +32,7 @@ import com.xingyuv.jushauth.model.AuthUser;
import com.xingyuv.jushauth.request.AuthRequest;
import com.xingyuv.jushauth.utils.AuthStateUtils;
import com.xingyuv.justauth.AuthRequestFactory;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
@ -38,17 +41,17 @@ import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.time.Duration;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SOCIAL_CLIENT_WEIXIN_MINI_APP_QRCODE_ERROR;
/**
* Service
@ -59,6 +62,12 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
@Slf4j
public class SocialClientServiceImpl implements SocialClientService {
/**
*
*/
@Value("${yudao.wxa-code.env-version}")
public String envVersion;
@Resource
private AuthRequestFactory authRequestFactory;
@ -76,7 +85,7 @@ public class SocialClientServiceImpl implements SocialClientService {
*
* WxMpService WxMpService
*/
private final LoadingCache<String, WxMpService> wxMpServiceCache = buildAsyncReloadingCache(
private final LoadingCache<String, WxMpService> wxMpServiceCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofSeconds(10L),
new CacheLoader<String, WxMpService>() {
@ -97,7 +106,7 @@ public class SocialClientServiceImpl implements SocialClientService {
*
* {@link #wxMpServiceCache}
*/
private final LoadingCache<String, WxMaService> wxMaServiceCache = buildAsyncReloadingCache(
private final LoadingCache<String, WxMaService> wxMaServiceCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofSeconds(10L),
new CacheLoader<String, WxMaService>() {
@ -140,7 +149,7 @@ public class SocialClientServiceImpl implements SocialClientService {
* AuthRequest
*
* @param socialType
* @param userType
* @param userType
* @return AuthRequest
*/
@VisibleForTesting
@ -197,7 +206,7 @@ public class SocialClientServiceImpl implements SocialClientService {
/**
* clientId + clientSecret WxMpService
*
* @param clientId appId
* @param clientId appId
* @param clientSecret secret
* @return WxMpService
*/
@ -228,6 +237,25 @@ public class SocialClientServiceImpl implements SocialClientService {
}
}
@Override
public byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO) {
WxMaService service = getWxMaService(UserTypeEnum.MEMBER.getValue());
try {
return service.getQrcodeService().createWxaCodeUnlimitBytes(
ObjUtil.defaultIfEmpty(reqVO.getScene(), SocialWxQrcodeReqDTO.SCENE),
reqVO.getPath(),
ObjUtil.defaultIfNull(reqVO.getCheckPath(), SocialWxQrcodeReqDTO.CHECK_PATH),
envVersion,
ObjUtil.defaultIfNull(reqVO.getWidth(), SocialWxQrcodeReqDTO.WIDTH),
ObjUtil.defaultIfNull(reqVO.getAutoColor(), SocialWxQrcodeReqDTO.AUTO_COLOR),
null,
ObjUtil.defaultIfNull(reqVO.getHyaline(), SocialWxQrcodeReqDTO.HYALINE));
} catch (WxErrorException e) {
log.error("[getWxQrcode][reqVO({})) 获得小程序码失败]", reqVO, e);
throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_QRCODE_ERROR);
}
}
/**
* clientId + clientSecret WxMpService
*
@ -249,7 +277,7 @@ public class SocialClientServiceImpl implements SocialClientService {
/**
* clientId + clientSecret WxMaService
*
* @param clientId appId
* @param clientId appId
* @param clientSecret secret
* @return WxMaService
*/
@ -311,8 +339,8 @@ public class SocialClientServiceImpl implements SocialClientService {
*
* userTypesocialType {@link #buildAuthRequest(Integer, Integer)}
*
* @param id
* @param userType
* @param id
* @param userType
* @param socialType
*/
private void validateSocialClientUnique(Long id, Integer userType, Integer socialType) {

View File

@ -142,6 +142,8 @@ yudao:
pay-return-url: http://niubi.natapp1.cc/api/pay/order/return
refund-notify-url: http://niubi.natapp1.cc/api/pay/refund/notify
demo: true # 开启演示模式
wxa-code:
env-version: release # 小程序版本: 正式版为 "release";体验版为 "trial";开发版为 "develop"
justauth:
enabled: true

View File

@ -172,6 +172,8 @@ yudao:
access-log: # 访问日志的配置项
enable: false
demo: false # 关闭演示模式
wxa-code:
env-version: develop # 小程序版本: 正式版为 "release";体验版为 "trial";开发版为 "develop"
justauth:
enabled: true

View File

@ -147,6 +147,8 @@ yudao:
base-package: ${yudao.info.base-package}
captcha:
enable: true # 验证码的开关,默认为 true
wxa-code:
env-version: release # 小程序版本: 正式版为 "release";体验版为 "trial";开发版为 "develop"。默认为 release
tenant: # 多租户相关配置项
enable: true
ignore-urls:

View File

@ -0,0 +1,36 @@
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.util.List;
/**
* {@link SmsClientTests
*
* @author
*/
public class SmsClientTests {
@Test
@Disabled
public void testHuaweiSmsClient() throws Throwable {
SmsChannelProperties properties = new SmsChannelProperties()
.setApiKey("123")
.setApiSecret("456");
HuaweiSmsClient client = new HuaweiSmsClient(properties);
// 准备参数
Long sendLogId = System.currentTimeMillis();
String mobile = "15601691323";
String apiTemplateId = "xx test01";
List<KeyValue<String, Object>> templateParams = List.of(new KeyValue<>("code", "1024"));
// 调用
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
// 打印结果
System.out.println(smsSendRespDTO);
}
}