|
@@ -0,0 +1,128 @@
|
|
|
+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.service.IKfRoomMsgService;
|
|
|
+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 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.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author : lingfeng
|
|
|
+ * @time : 2024-02-23
|
|
|
+ * @description : 客服聊天房间消息
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@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();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图片上传临时素材
|
|
|
+ */
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传临时素材
|
|
|
+ */
|
|
|
+ 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("企微上传临时素材返回结果为空");
|
|
|
+ }
|
|
|
+ KfUploadTempMediaDTO res = JsonUtil.toObj(JsonUtil.toString(result), KfUploadTempMediaDTO.class);
|
|
|
+ if (res.isSuccess()) {
|
|
|
+ return res.getMedia_id();
|
|
|
+ }
|
|
|
+// log.error("企微上传临时素材异常, accessToken : {}, type : {}, errmsg : {}", accessToken, type, res.getErrmsg());
|
|
|
+ throw new BaseException("企微上传临时素材失败");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|