zoujiajian 1 год назад
Родитель
Сommit
151befba22

+ 5 - 0
netflix-service/src/main/java/com/cyksj/service/corp/WxCorpOps.java

@@ -237,4 +237,9 @@ public interface WxCorpOps {
 	void sendSatisfactionMsg(String msgId) throws Exception;
 
 	CorpKfUserInfo getCorpKfUserInfo(String externalUserid) throws Exception;
+
+	/**
+	 * 获取微信客服账号链接
+	 */
+	String getUrl(String openKfId, String scene) throws Exception;
 }

+ 36 - 44
netflix-service/src/main/java/com/cyksj/service/corp/impl/WxCorpOpsImpl.java

@@ -95,7 +95,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		if (StringUtils.isNotBlank(accessToken)) {
 			return accessToken;
 		}
-		return getKfAccessToken(corpId);
+		return getAccessToken(corpId);
 	}
 
 	@Override
@@ -650,39 +650,6 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		redisService.set(RedisKey.CORP_ACCESS_TOKEN_KEY + corpId, accessToken, token.getExpiresIn() - 300L);
 		return accessToken;
 	}
-
-	public String getKfAccessToken(String corpId) throws Exception {
-		WxCorpApp wxCorpApp = WeChatCorpFactory.getCorpConfigStrategy(corpId);
-		if (StringUtils.isBlank(wxCorpApp.getMsgSecret())) {
-			throw BusinessRuntimeException.getInstance("该企业会话存档权限尚未开通.请联系管理员配置api secret");
-		}
-		String accessToken = redisService.getStr(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId);
-		if(StringUtils.isBlank(accessToken)){
-			String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" +
-					"?corpid=" + corpId +"&corpsecret=" + wxCorpApp.getMsgSecret();
-
-			HttpResponse<String> res =
-					J11HttpC.custom()
-							.ofGet()
-							.url(url)
-							.send(HttpResponse.BodyHandlers.ofString());
-
-			if (200 != res.statusCode()) {
-				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + res.body());
-			}
-			JSONObject result = new JSONObject(res.body());
-
-			final Integer errcode = result.getInt("errcode");
-			if (null != errcode && 0 != errcode) {
-				throw BusinessRuntimeException.getInstance("获取企业微信access_token失败: " + result.getStr("errmsg") + "[" + errcode + "]");
-			}
-			accessToken = result.getStr("access_token");
-			redisService.set(RedisKey.CORP_MSG_ACCESS_TOKEN_KEY + corpId, accessToken, result.getInt("expires_in") - 300L);
-			return accessToken;
-		}
-		return accessToken;
-	}
-
 	public String js_ticket(String corpId) throws Exception {
 		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s", this.getAccessToken(corpId));
 		HttpResponse<byte[]> res =
@@ -833,7 +800,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public WxCpKfAccountAddResp addAccount(String corpId, WxCpKfAccountAdd add) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=" + getAccessToken(corpId);
 		HttpResponse<String> res =
 				J11HttpC.custom()
 						.ofPost()
@@ -854,7 +821,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public void delAccount(String corpId, WxCpKfAccountDel del) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/del?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/del?access_token=" + getAccessToken(corpId);
 		HttpResponse<String> res =
 				J11HttpC.custom()
 						.ofPost()
@@ -874,7 +841,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public void updAccount(String corpId, WxCpKfAccountUpd upd) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/update?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/account/update?access_token=" + getAccessToken(corpId);
 		HttpResponse<String> res =
 				J11HttpC.custom()
 						.ofPost()
@@ -894,7 +861,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public WxCpKfMsgListResp syncMsg(String corpId, String cursor, String token, Integer limit, String openKfId) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token=" + getAccessToken(corpId);
 
 		JSONObject json = new JSONObject();
 		json.putOpt("open_kfid", openKfId);
@@ -927,7 +894,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public WxCpKfMsgSendResp sendMsgOnEvent(String corpId, WxCorpKfMsgSendRequest request) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=" + getAccessToken(corpId);
 
 		HttpResponse<String> res =
 				J11HttpC.custom()
@@ -949,7 +916,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public CorpKfServicerResp getKfServicerList(String corpId,String openKfId) throws Exception {
-		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/list?access_token=%s&open_kfid=%s", getKfAccessToken(corpId), openKfId);
+		String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/list?access_token=%s&open_kfid=%s", getAccessToken(corpId), openKfId);
 		HttpResponse<String> res =
 				J11HttpC.custom()
 						.ofPost()
@@ -969,7 +936,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public void moveWaitingQueue(String corpId, String openKfId, String externalUserId) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/trans?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/trans?access_token=" + getAccessToken(corpId);
 		JSONObject request = new JSONObject();
 		request.putOpt("open_kfid", openKfId);
 		request.putOpt("external_userid", externalUserId);
@@ -994,7 +961,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public void transCorpKfSessionToServicer(String corpId, String openKfId, String userid, String externalUserId) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/trans?access_token=" + getKfAccessToken(corpId);
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/trans?access_token=" + getAccessToken(corpId);
 		JSONObject request = new JSONObject();
 		request.putOpt("open_kfid", openKfId);
 		request.putOpt("external_userid", externalUserId);
@@ -1020,7 +987,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public void sendSatisfactionMsg(String msgId) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=" + getKfAccessToken(yhlXWxCorpConfig.getCorpId());
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=" + getAccessToken(yhlXWxCorpConfig.getCorpId());
 		CorpKfSatisfactionMsg corpKfSatisfactionMsg = new CorpKfSatisfactionMsg();
 		corpKfSatisfactionMsg.setCode(msgId);
 		corpKfSatisfactionMsg.setMsgtype("msgtype");
@@ -1044,7 +1011,7 @@ public class WxCorpOpsImpl implements WxCorpOps {
 
 	@Override
 	public CorpKfUserInfo getCorpKfUserInfo(String externalUserid) throws Exception {
-		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token=" + getKfAccessToken(yhlXWxCorpConfig.getCorpId());
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token=" + getAccessToken(yhlXWxCorpConfig.getCorpId());
 		JSONObject params = new JSONObject();
 		params.putOpt("external_userid_list", List.of(externalUserid));
 		HttpResponse<String> res =
@@ -1064,4 +1031,29 @@ public class WxCorpOpsImpl implements WxCorpOps {
 		List<CorpKfUserInfo> customerList = re.getJSONArray("customer_list").toList(CorpKfUserInfo.class);
 		return customerList.isEmpty() ? null : customerList.get(0);
 	}
+
+	@Override
+	public String getUrl(String openKfId, String scene) throws Exception {
+		String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=" + getAccessToken(yhlXWxCorpConfig.getCorpId());
+		JSONObject params = new JSONObject();
+		params.putOpt("open_kfid", openKfId);
+		if (StrUtil.isNotEmpty(scene)) {
+			params.putOpt("scene", scene);
+		}
+		HttpResponse<String> res =
+				J11HttpC.custom()
+						.ofPost()
+						.url(url)
+						.headers(J11HttpC.ReqType.raw_json)
+						.body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(params), IoKit.Charsets.UTF_8.getCharset()))
+						.send(HttpResponse.BodyHandlers.ofString());
+		if (200 != res.statusCode()) {
+			throw BusinessRuntimeException.getInstance("微信客服账号链接信息: " + res.body());
+		}
+		JSONObject re = Jsons.parseObject(res.body(), JSONObject.class);
+		if (re.getInt("errcode") != 0) {
+			throw BusinessRuntimeException.getInstance("微信客服账号链接信息错误: " + re.getStr("errmsg"));
+		}
+		return re.getStr("url");
+	}
 }

+ 2 - 0
netflix-service/src/main/java/com/cyksj/service/corp/kf/CorpKfService.java

@@ -27,4 +27,6 @@ public interface CorpKfService {
 	void servicerCommute(long cmsUserId, String openKfId);
 
 	List<CorpKfAccountFollowDutyStatus> getDutyFollowByOpenKfId(long cmsUserId, String openKfId) throws Exception;
+
+	String getUrl(String openKfId, String scene) throws Exception;
 }

+ 5 - 0
netflix-service/src/main/java/com/cyksj/service/corp/kf/impl/CorpKfServiceImpl.java

@@ -171,4 +171,9 @@ public class CorpKfServiceImpl implements CorpKfService {
 		List<CorpKfAccountFollowDutyStatus> dutyStatuses = corpKfAccountFollowDutyStatusMapper.selectList(queryWrapper);
 		return dutyStatuses;
 	}
+
+	@Override
+	public String getUrl(String openKfId, String scene) throws Exception {
+		return wxCorpOps.getUrl(openKfId, scene);
+	}
 }

+ 8 - 0
netflix-web/src/main/java/com/cyksj/web/controller/manage/corp/wxkf/CorpKfController.java

@@ -82,6 +82,14 @@ public class CorpKfController {
 		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
 	}
 
+	/**
+	 * 获取客服账号链接
+	 */
+	@GetMapping("/get/url")
+	public Result<String> getUrl(String openKfId, String scene) throws Exception {
+		String url = corpKfService.getUrl(openKfId, scene);
+		return GatewayResponse.SUCCESS.newBuilder().toResult(url);
+	}
 
 	//===============================================账号会话设置========================================================================