|
@@ -1,32 +1,24 @@
|
|
|
package com.zanxiang.game.module.manage.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.zanxiang.game.module.base.ServerInfo;
|
|
|
-import com.zanxiang.game.module.base.pojo.enums.HttpStatusEnum;
|
|
|
-import com.zanxiang.game.module.base.rpc.IWxApiServiceRpc;
|
|
|
-import com.zanxiang.game.module.manage.pojo.dto.GameAppletDTO;
|
|
|
-import com.zanxiang.game.module.manage.pojo.dto.KfUploadTempMediaDTO;
|
|
|
+import com.zanxiang.game.module.manage.enums.KfRoomMsgOwnerEnum;
|
|
|
+import com.zanxiang.game.module.manage.enums.KfRoomMsgTypeEnum;
|
|
|
+import com.zanxiang.game.module.manage.pojo.dto.KfAppletMsgDTO;
|
|
|
+import com.zanxiang.game.module.manage.pojo.dto.KfMsgDTO;
|
|
|
import com.zanxiang.game.module.manage.service.IKfRoomMsgService;
|
|
|
+import com.zanxiang.game.module.manage.websocket.KfMsgWebsocketHandler;
|
|
|
import com.zanxiang.game.module.mybatis.entity.KfRoomMsg;
|
|
|
import com.zanxiang.game.module.mybatis.mapper.KfRoomMsgMapper;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
-import com.zanxiang.module.util.exception.BaseException;
|
|
|
+import com.zanxiang.module.util.bean.BeanUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.core.io.Resource;
|
|
|
-import org.springframework.http.HttpEntity;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.LinkedMultiValueMap;
|
|
|
-import org.springframework.web.client.RestTemplate;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
-import java.net.URI;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @author : lingfeng
|
|
@@ -37,92 +29,49 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class KfRoomMsgServiceImpl extends ServiceImpl<KfRoomMsgMapper, KfRoomMsg> implements IKfRoomMsgService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private RestTemplate restTemplate;
|
|
|
-
|
|
|
- @DubboReference(providedBy = ServerInfo.SERVER_SDK_DUBBO_NAME)
|
|
|
- private IWxApiServiceRpc wxApiServiceRpc;
|
|
|
|
|
|
- /**
|
|
|
- * 通过腾讯API给玩家发送消息
|
|
|
- */
|
|
|
- private String sendCustomMessageApi(GameAppletDTO gameAppletDTO, String openId, String msgType, Map<String, Object> msgMap) {
|
|
|
- //客服消息参数构造
|
|
|
- Map<String, Object> paramMap = new HashMap<>(3);
|
|
|
- paramMap.put("touser", openId);
|
|
|
- paramMap.put("msgtype", msgType);
|
|
|
- paramMap.put(msgType, msgMap);
|
|
|
- log.error("客服消息发送参数, paramMap : {}", JsonUtil.toString(paramMap));
|
|
|
- //获取接口token
|
|
|
- String accessToken = wxApiServiceRpc.getAccessToken(gameAppletDTO.getAppId(), gameAppletDTO.getAppSecret());
|
|
|
- URI uri = UriComponentsBuilder.fromHttpUrl("https://api.weixin.qq.com/cgi-bin/message/custom/send")
|
|
|
- .queryParam("access_token", accessToken)
|
|
|
- .build().toUri();
|
|
|
- // 发送请求
|
|
|
- String result = restTemplate.postForObject(uri, paramMap, String.class);
|
|
|
- log.error("客服消息发送结果, result : {}", result);
|
|
|
- return HttpStatusEnum.SUCCESS.getMsg();
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private KfMsgWebsocketHandler kfMsgWebsocketHandler;
|
|
|
|
|
|
- /**
|
|
|
- * 图片上传临时素材
|
|
|
- */
|
|
|
- private String uploadTempMedia(GameAppletDTO gameAppletDTO, String openId, String msgType, Map<String, Object> msgMap) {
|
|
|
- //客服消息参数构造
|
|
|
- Map<String, Object> paramMap = new HashMap<>(3);
|
|
|
- paramMap.put("touser", openId);
|
|
|
- paramMap.put("msgtype", msgType);
|
|
|
- paramMap.put(msgType, msgMap);
|
|
|
- log.error("客服消息发送参数, paramMap : {}", JsonUtil.toString(paramMap));
|
|
|
- //获取接口token
|
|
|
- String accessToken = wxApiServiceRpc.getAccessToken(gameAppletDTO.getAppId(), gameAppletDTO.getAppSecret());
|
|
|
- URI uri = UriComponentsBuilder.fromHttpUrl("https://api.weixin.qq.com/cgi-bin/media/upload")
|
|
|
- .queryParam("access_token", accessToken)
|
|
|
- .build().toUri();
|
|
|
- // 发送请求
|
|
|
- String result = restTemplate.postForObject(uri, paramMap, String.class);
|
|
|
- log.error("客服消息发送结果, result : {}", result);
|
|
|
- return HttpStatusEnum.SUCCESS.getMsg();
|
|
|
+ @Override
|
|
|
+ public void appletMsg(String postData) {
|
|
|
+ log.error("接收到SDK转发的小程序消息, postData : {}", postData);
|
|
|
+ KfAppletMsgDTO kfAppletMsgDTO = JsonUtil.toObj(postData, KfAppletMsgDTO.class);
|
|
|
+ //用户进入会话事件
|
|
|
+ if (Objects.equals(kfAppletMsgDTO.getMsgType(), KfAppletMsgDTO.MSG_TYPE_EVENT)
|
|
|
+ && Objects.equals(kfAppletMsgDTO.getEvent(), KfAppletMsgDTO.EVENT_USER_ENTER_TEMP_SESSION)) {
|
|
|
+ //创建用户信息
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //非玩家消息, 不做处理
|
|
|
+ if (kfAppletMsgDTO.getMsgId() == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //消息存储
|
|
|
+ KfRoomMsg kfRoomMsg = KfRoomMsg.builder()
|
|
|
+ .msgId(String.valueOf(kfAppletMsgDTO.getMsgId()))
|
|
|
+ .msgType(kfAppletMsgDTO.getMsgType())
|
|
|
+ .readStatus(Boolean.FALSE)
|
|
|
+ .msgOwner(KfRoomMsgOwnerEnum.KF_MSG_OWNER_USER.getValue())
|
|
|
+ .content(this.getMsgContent(kfAppletMsgDTO))
|
|
|
+ .source(postData)
|
|
|
+ .createTime(LocalDateTime.now())
|
|
|
+ .build();
|
|
|
+ super.save(kfRoomMsg);
|
|
|
+ // 消息转发到redis频道
|
|
|
+ kfMsgWebsocketHandler.pushMessage(JsonUtil.toString(BeanUtil.copy(kfRoomMsg, KfMsgDTO.class)));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传临时素材
|
|
|
- */
|
|
|
- public String mediaUpload(String accessToken, MultipartFile files) {
|
|
|
- URI uri = UriComponentsBuilder.fromHttpUrl("https://api.weixin.qq.com/cgi-bin/media/upload")
|
|
|
- .queryParam("access_token", accessToken)
|
|
|
- .queryParam("type", "image")
|
|
|
- .build().toUri();
|
|
|
- //最外层请求头
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
- LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
|
|
|
- //素材内容请求头
|
|
|
- HttpHeaders formHeader = new HttpHeaders();
|
|
|
- formHeader.setContentDispositionFormData("media", files.getOriginalFilename());
|
|
|
- formHeader.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
- HttpEntity<Resource> formEntity = new HttpEntity<>(files.getResource(), formHeader);
|
|
|
- map.add("media", formEntity);
|
|
|
- //http请求头
|
|
|
- HttpEntity<LinkedMultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, httpHeaders);
|
|
|
- Object result;
|
|
|
- try {
|
|
|
- result = restTemplate.postForObject(uri, httpEntity, Object.class);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("客服消息上传临时素材异常, e : {}", e.getMessage());
|
|
|
- throw new BaseException("上传临时素材异常");
|
|
|
- }
|
|
|
- if (result == null) {
|
|
|
-// log.error("企微上传临时素材异常, accessToken : {}, type : {}", accessToken, type);
|
|
|
- throw new BaseException("企微上传临时素材返回结果为空");
|
|
|
+ private String getMsgContent(KfAppletMsgDTO kfAppletMsgDTO) {
|
|
|
+ Map<String, String> contentMap = new HashMap<>(2);
|
|
|
+ if (Objects.equals(KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT.getValue(), kfAppletMsgDTO.getMsgType())) {
|
|
|
+ contentMap.put("text", kfAppletMsgDTO.getContent());
|
|
|
}
|
|
|
- KfUploadTempMediaDTO res = JsonUtil.toObj(JsonUtil.toString(result), KfUploadTempMediaDTO.class);
|
|
|
- if (res.isSuccess()) {
|
|
|
- return res.getMedia_id();
|
|
|
+ if (Objects.equals(KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE.getValue(), kfAppletMsgDTO.getMsgType())) {
|
|
|
+ contentMap.put("picUrl", kfAppletMsgDTO.getPicUrl());
|
|
|
+ contentMap.put("mediaId", kfAppletMsgDTO.getMediaId());
|
|
|
}
|
|
|
-// log.error("企微上传临时素材异常, accessToken : {}, type : {}, errmsg : {}", accessToken, type, res.getErrmsg());
|
|
|
- throw new BaseException("企微上传临时素材失败");
|
|
|
+ return JsonUtil.toString(contentMap);
|
|
|
}
|
|
|
|
|
|
}
|