|
|
@@ -2,11 +2,10 @@ package com.ruoyi.system.utils;
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Collections;
|
|
|
+import java.util.Base64;
|
|
|
|
|
|
public class MobileSMS {
|
|
|
public void getcode(String phone,String code){
|
|
|
@@ -24,48 +23,41 @@ public class MobileSMS {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送短信提醒(EngageLab)
|
|
|
- * 文档:https://www.engagelab.com/zh_CN/docs/NEWSMS/REST-API/API-SMS-Sending
|
|
|
+ * 发送 OTP 验证码(EngageLab OTP API)
|
|
|
+ * 参考:https://otp.api.engagelab.cc/v1/messages
|
|
|
+ *
|
|
|
* @param phone 手机号(含国家码,如 +8613700000000)
|
|
|
- * @param code 验证码/短信内容变量
|
|
|
+ * @param code 验证码
|
|
|
*/
|
|
|
- public void getTwCode(String phone, String code)
|
|
|
- {
|
|
|
- // EngageLab 短信发送地址
|
|
|
- String url = "https://smsapi.engagelab.com/v1/messages";
|
|
|
-
|
|
|
- // TODO 根据实际控制台配置替换为自己的 key / secret / 模板 ID
|
|
|
- String devKey = "d6kk5d4ao9hcpovohsl0";
|
|
|
- String devSecret = "t7bhxkdype5p6facdna4ehja";
|
|
|
- String templateId = "tw_foodie_user_register";
|
|
|
-
|
|
|
- // Basic 认证头:Authorization: Basic base64(dev_key:dev_secret)
|
|
|
- String auth = devKey + ":" + devSecret;
|
|
|
- String base64Auth = java.util.Base64.getEncoder()
|
|
|
- .encodeToString(auth.getBytes(StandardCharsets.UTF_8));
|
|
|
-
|
|
|
- // 组装请求体
|
|
|
- JSONObject body = new JSONObject();
|
|
|
- body.put("to", Collections.singletonList(phone));
|
|
|
-
|
|
|
- JSONObject template = new JSONObject();
|
|
|
- template.put("id", templateId);
|
|
|
-
|
|
|
- // 根据你在 EngageLab 模板中定义的变量名赋值
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- // 假设模板中有 {{code}} 变量;如果变量名不同,请相应修改 key
|
|
|
- params.put("code", code);
|
|
|
-
|
|
|
- template.put("params", params);
|
|
|
- body.put("template", template);
|
|
|
-
|
|
|
- String res = HttpRequest.post(url)
|
|
|
- .header("Authorization", "Basic " + base64Auth)
|
|
|
- .header("Content-Type", "application/json")
|
|
|
- .body(JSON.toJSONString(body))
|
|
|
- .execute()
|
|
|
- .body();
|
|
|
-
|
|
|
- System.out.println("发送短信结果:" + res);
|
|
|
+ public void getTwCode(String phone, String code) {
|
|
|
+ try {
|
|
|
+ String apiKey = "d6kk5d4ao9hcpovohsl0";
|
|
|
+ String apiSecret = "t7bhxkdype5p6facdna4ehja";
|
|
|
+ String authString = Base64.getEncoder()
|
|
|
+ .encodeToString((apiKey + ":" + apiSecret).getBytes(StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ JSONObject template = new JSONObject();
|
|
|
+ template.put("id", "tw_foodie_code");
|
|
|
+ template.put("language", "default");
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("code", code);
|
|
|
+ template.put("params", params);
|
|
|
+
|
|
|
+ JSONObject body = new JSONObject();
|
|
|
+ body.put("to", phone);
|
|
|
+ body.put("template", template);
|
|
|
+ String bodyString = body.toJSONString();
|
|
|
+
|
|
|
+ String res = HttpRequest.post("https://otp.api.engagelab.cc/v1/codes")
|
|
|
+ .header("Authorization", "Basic " + authString)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(bodyString)
|
|
|
+ .execute()
|
|
|
+ .body();
|
|
|
+
|
|
|
+ System.out.println("发送短信结果:" + res);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("OTP 发送异常:" + e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
}
|