193 lines
7.9 KiB
Java
193 lines
7.9 KiB
Java
package com.ruoyi.system.ControllerUtil;
|
||
|
||
import com.alibaba.fastjson2.JSON;
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.winnerlook.model.*;
|
||
import com.winnerlook.util.Base64;
|
||
import com.winnerlook.util.MD5Util;
|
||
|
||
import java.util.LinkedHashMap;
|
||
|
||
/**
|
||
* 云信小号相关API工具类
|
||
* <p>
|
||
* 封装了云信小号的AX、AXB绑定、解绑、语音通知等接口的调用方法。
|
||
* 统一了签名、鉴权、请求体组装等流程,便于业务系统直接调用。
|
||
* </p>
|
||
* <p>
|
||
* 依赖:
|
||
* - fastjson2 用于JSON序列化/反序列化
|
||
* - com.winnerlook.model.* 需包含请求/响应体相关POJO
|
||
* - com.winnerlook.util.Base64、MD5Util 用于加密
|
||
* </p>
|
||
* <p>
|
||
* 使用前请确保相关依赖和配置已正确引入。
|
||
* </p>
|
||
*/
|
||
public class YunXinPhoneUtilAPI {
|
||
|
||
private static final String APP_ID = "256339";
|
||
private static final String TOKEN = "ecf5ae04c52b4d85a0e4255d7d0ebb71";
|
||
|
||
// 云信接口地址常量
|
||
private static final String UNBIND_URL = "https://101.37.133.245:11008/voice/1.0.0/middleNumberUnbind";
|
||
private static final String NOTIFY_URL = "https://101.37.133.245:11008/voice/1.0.0/notify";
|
||
private static final String AXB_URL = "https://101.37.133.245:11008/voice/1.0.0/middleNumberAXB";
|
||
|
||
// 回调地址常量(可通过set方法动态设置)
|
||
private static String AXB_CALLBACK_URL = "";
|
||
private static String NOTIFY_CALLBACK_URL = "";
|
||
|
||
|
||
// 回调地址常量(可通过set方法动态设置)
|
||
private static String templateID = "404168";
|
||
|
||
/**
|
||
* 设置AXB绑定的回调URL
|
||
*/
|
||
public static void setAxbCallbackUrl(String callbackUrl) {
|
||
AXB_CALLBACK_URL = callbackUrl;
|
||
}
|
||
|
||
/**
|
||
* 设置语音通知的回调URL
|
||
*/
|
||
public static void setNotifyCallbackUrl(String callbackUrl) {
|
||
NOTIFY_CALLBACK_URL = callbackUrl;
|
||
}
|
||
|
||
/**
|
||
* 语音通知接口调用
|
||
* @param phone 通知目标手机号
|
||
|
||
* @return VoiceResponseResult 结果对象
|
||
* @throws Exception 异常
|
||
* <p>回调地址使用 NOTIFY_CALLBACK_URL 常量,如需动态设置请调用 setNotifyCallbackUrl 方法</p>
|
||
*/
|
||
public static VoiceResponseResult httpsAxbTransfer(String phone) throws Exception {
|
||
VoiceResponseResult resultObj = new VoiceResponseResult();
|
||
String url = NOTIFY_URL;
|
||
try {
|
||
// 组装请求体
|
||
VoiceNotifyBody transfer = new VoiceNotifyBody();
|
||
transfer.setCalleeNumber(phone);
|
||
transfer.setTemplateId(templateID);
|
||
transfer.setTemplateArgs(new LinkedHashMap<String, String>()); // 可根据需要传递参数
|
||
transfer.setCallbackUrl(NOTIFY_CALLBACK_URL);
|
||
|
||
// 获取当前时间戳
|
||
long timestamp = System.currentTimeMillis();
|
||
// 生成鉴权参数
|
||
String authorization = Base64.encodeBase64(APP_ID + ":" + timestamp);
|
||
String sig = MD5Util.getMD5(APP_ID + TOKEN + timestamp);
|
||
// 拼接最终URL
|
||
url = url + "/" + APP_ID + "/" + sig;
|
||
String body = JSON.toJSONString(transfer);
|
||
|
||
// 调用HTTPS服务
|
||
HttpsService httpsService = new HttpsService();
|
||
String result = httpsService.doPost(url, authorization, body, "UTF-8");
|
||
JSONObject rjson = JSONObject.parseObject(result);
|
||
resultObj.setCallId(rjson.getString("callId"));
|
||
resultObj.setResult(rjson.getString("result"));
|
||
resultObj.setMessage(rjson.getString("message"));
|
||
} catch (Exception e) {
|
||
System.out.println("[YunXinPhoneUtilAPI] 语音通知异常: " + e.getMessage());
|
||
throw e;
|
||
}
|
||
return resultObj;
|
||
}
|
||
|
||
/**
|
||
* AXB绑定接口调用
|
||
* 提前绑定小号号码、号码A、号码B的关系,A、B呼叫小号即可互相通话。
|
||
* @param minphone 隐私小号(X号)
|
||
* @param phoneA 号码A
|
||
* @param phoneB 号码B
|
||
* @return VoiceResponseResult 结果对象
|
||
* @throws Exception 异常
|
||
* <p>回调地址使用 AXB_CALLBACK_URL 常量,如需动态设置请调用 setAxbCallbackUrl 方法</p>
|
||
*/
|
||
public static VoiceResponseResult httpsPrivacyBindAxb(String minphone, String phoneA, String phoneB) throws Exception {
|
||
String url = AXB_URL;
|
||
VoiceResponseResult resultObj = new VoiceResponseResult();
|
||
try {
|
||
PrivacyBindBodyAxb bindBodyAxb = new PrivacyBindBodyAxb();
|
||
// 组装请求体
|
||
bindBodyAxb.setMiddleNumber(minphone);
|
||
bindBodyAxb.setBindNumberA(phoneA);
|
||
bindBodyAxb.setBindNumberB(phoneB);
|
||
bindBodyAxb.setCallRec(0); // 1:开启录音,0:关闭
|
||
bindBodyAxb.setMaxBindingTime(3600); // 有效时长(秒),为空表示永久
|
||
bindBodyAxb.setPassthroughCallerToA(0); // 0:不透传; 1:透传
|
||
bindBodyAxb.setPassthroughCallerToB(0); // 0:不透传; 1:透传
|
||
bindBodyAxb.setCallbackUrl(AXB_CALLBACK_URL);
|
||
|
||
long timestamp = System.currentTimeMillis();
|
||
String authorization = Base64.encodeBase64(APP_ID + ":" + timestamp);
|
||
String sig = MD5Util.getMD5(APP_ID + TOKEN + timestamp);
|
||
url = url + "/" + APP_ID + "/" + sig;
|
||
String body = JSON.toJSONString(bindBodyAxb);
|
||
|
||
HttpsService httpsService = new HttpsService();
|
||
String result = httpsService.doPost(url, authorization, body, "UTF-8");
|
||
JSONObject rjson = JSONObject.parseObject(result);
|
||
resultObj.setCallId(rjson.getString("callId"));
|
||
resultObj.setResult(rjson.getString("result"));
|
||
resultObj.setMessage(rjson.getString("message"));
|
||
System.out.println(result);
|
||
} catch (Exception e) {
|
||
System.out.println("[YunXinPhoneUtilAPI] AXB绑定异常: " + e.getMessage());
|
||
throw e;
|
||
}
|
||
return resultObj;
|
||
}
|
||
|
||
/**
|
||
* 解绑接口调用
|
||
* 解绑指定小号与A、B号码的绑定关系
|
||
* @param phoneA 号码A
|
||
* @param phoneB 号码B
|
||
* @param phone 隐私小号(X号)
|
||
* @return VoiceResponseResult 结果对象
|
||
* @throws Exception 异常
|
||
*/
|
||
public static VoiceResponseResult httpsPrivacyUnbind(String phoneA, String phoneB, String phone) throws Exception {
|
||
VoiceResponseResult resultObj = new VoiceResponseResult();
|
||
String url = UNBIND_URL;
|
||
try {
|
||
PrivacyUnbindBody unbindBody = new PrivacyUnbindBody();
|
||
long timestamp = System.currentTimeMillis();
|
||
unbindBody.setMiddleNumber(phone);
|
||
unbindBody.setBindNumberA(phoneA);
|
||
unbindBody.setBindNumberB(phoneB);
|
||
String authorization = Base64.encodeBase64(APP_ID + ":" + timestamp);
|
||
String sig = MD5Util.getMD5(APP_ID + TOKEN + timestamp);
|
||
url = url + "/" + APP_ID + "/" + sig;
|
||
String body = JSON.toJSONString(unbindBody);
|
||
HttpsService httpsService = new HttpsService();
|
||
String result = httpsService.doPost(url, authorization, body, "UTF-8");
|
||
JSONObject rjson = JSONObject.parseObject(result);
|
||
resultObj.setResult(rjson.getString("result"));
|
||
resultObj.setMessage(rjson.getString("message"));
|
||
|
||
} catch (Exception e) {
|
||
System.out.println("[YunXinPhoneUtilAPI] 解绑异常: " + e.getMessage());
|
||
throw e;
|
||
}
|
||
return resultObj;
|
||
}
|
||
|
||
/**
|
||
* main方法示例:解绑操作
|
||
*/
|
||
public static void main(String[] args) throws Exception {
|
||
// 设置回调地址(如有需要)
|
||
setAxbCallbackUrl("http://your-callback-url.com/axb");
|
||
setNotifyCallbackUrl("http://your-callback-url.com/notify");
|
||
// 示例:解绑操作
|
||
VoiceResponseResult res = httpsPrivacyUnbind("15270824290", "18339212639", "13279237164");
|
||
System.out.println("解绑结果:" + JSON.toJSONString(res));
|
||
}
|
||
}
|