SmsUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.cyksj.common.util;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.http.HttpResponse;
  4. import cn.hutool.http.Method;
  5. import cn.hutool.json.JSONObject;
  6. import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
  7. import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
  8. import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
  9. import com.tencentcloudapi.common.Credential;
  10. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  11. import com.tencentcloudapi.common.profile.ClientProfile;
  12. import com.tencentcloudapi.common.profile.HttpProfile;
  13. import com.tencentcloudapi.sms.v20210111.SmsClient;
  14. import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
  15. import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
  16. import com.tencentcloudapi.sms.v20210111.models.SendStatus;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. /**
  22. * @author chan
  23. * @date 2022/10/13 11:29 AM
  24. */
  25. public class SmsUtil {
  26. private static final String URL = "http://sms-api.luosimao.com/v1/send.json";
  27. private static final String BATCH_URL = "http://sms-api.luosimao.com/v1/send_batch.json";
  28. private static final String PASSWORD = "key-1bf2a2f243e18be640b2428ddf03ded9";
  29. private static final String LVSINCO_PASSWORD = "key-246cd85eb5603c3d4924531e5d5c4edc";
  30. private static final String TENCENT_SMS_SECRET_ID = "AKIDBB3jEqR0Y95zpfTix1vd6fMqAIahc4Ka";
  31. private static final String TENCENT_SMS_SECRET_KEY = "c6YtNm63rcQ1QdiGmEbmEPSTPF3e4KQK";
  32. private static final String TENCENT_SMS_SKD_APP_ID = "1400796314";
  33. private static final String TENCENT_SMS_SIGN_NAME = "崇宇信息科技";
  34. private static final String TENCENT_SMS_TEMPLATE_ID = "1705927";
  35. //国际腾讯云短信
  36. private static final String TENCENT_SMS_INTERNATIONAL_TEMPLATE_ID = "1705927";
  37. //账号修改密码国际短信通知
  38. private static final String TENCENT_SMS_ACCOUNT_INTERNATIONAL_TEMPLATE_ID = "2040574";
  39. private static final String TENCENT_CAPTCHA_SECRET_ID = "AKIDogNE1uQ3azlVH0582pS0gRD7HZ3aIAYs";
  40. private static final String TENCENT_CAPTCHA_SECRET_KEY = "Ig2SnIyeHqK5tmrw27kR4bJ727bAsjSX";
  41. //腾讯云图形认证密钥id
  42. private static final Long TENCENT_CAPTCHA_APP_ID = 197694921l;
  43. //腾讯云图形认证密钥key
  44. private static final String TENCENT_CAPTCHA_APP_SECRET_KEY = "vOd32NejzMM2HQX0WtKy7ItvK";
  45. private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);
  46. public static void main(String[] args) {
  47. send("15720916464", "尊敬的用户,您的验证码为123456,此验证码仅用于银河录像。【银河录像局】");
  48. }
  49. public static void send(String mobile, String msg) {
  50. HttpRequest request = new HttpRequest(URL);
  51. request.setMethod(Method.POST);
  52. request.basicAuth("api", PASSWORD);
  53. request.form("mobile", mobile);
  54. request.form("message", msg);
  55. HttpResponse execute = request.execute();
  56. System.out.println(execute.body());
  57. }
  58. public static Boolean sendLuoKey2Msg(String mobile, String msg) throws Exception {
  59. HttpRequest request = new HttpRequest(URL);
  60. request.setMethod(Method.POST);
  61. request.basicAuth("api", LVSINCO_PASSWORD);
  62. request.form("mobile", mobile);
  63. request.form("message", msg);
  64. HttpResponse execute = request.execute();
  65. JSONObject re = Jsons.parseObject(execute.body(), JSONObject.class);
  66. if (re.getInt("error") == 0) {
  67. log.info(execute.body());
  68. return true;
  69. }
  70. log.error(execute.body());
  71. return false;
  72. }
  73. public static Boolean batchSendLuoKey2Msg(List<String> mobile, String msg) throws Exception {
  74. HttpRequest request = new HttpRequest(BATCH_URL);
  75. request.setMethod(Method.POST);
  76. request.basicAuth("api", LVSINCO_PASSWORD);
  77. request.form("mobile_list", mobile.stream().collect(Collectors.joining(",")));
  78. request.form("message", msg);
  79. HttpResponse execute = request.execute();
  80. JSONObject re = Jsons.parseObject(execute.body(), JSONObject.class);
  81. if (re.getInt("error") == 0) {
  82. log.info(execute.body());
  83. return true;
  84. }
  85. log.error(execute.body());
  86. return false;
  87. }
  88. public static Boolean sendTencent(Integer cid, String mobile, String msg) {
  89. String templateId = TENCENT_SMS_TEMPLATE_ID;
  90. if (cid != 86) {
  91. templateId = TENCENT_SMS_INTERNATIONAL_TEMPLATE_ID;
  92. }
  93. return commonSendTencentSmsMsg(templateId, cid, mobile, msg);
  94. }
  95. public static Boolean sendAccountInternalSmsMsg(Integer cid, String mobile, String msg) {
  96. String templateId = TENCENT_SMS_ACCOUNT_INTERNATIONAL_TEMPLATE_ID;
  97. return commonSendTencentSmsMsg(templateId, cid, mobile, msg);
  98. }
  99. public static Boolean commonSendTencentSmsMsg(String templateId, Integer cid, String mobile, String msg) {
  100. try {
  101. /* 必要步骤:
  102. * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
  103. * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
  104. * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
  105. * 以免泄露密钥对危及你的财产安全。
  106. * SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi */
  107. Credential cred = new Credential(TENCENT_SMS_SECRET_ID, TENCENT_SMS_SECRET_KEY);
  108. HttpProfile httpProfile = new HttpProfile();
  109. httpProfile.setReqMethod("POST");
  110. /* SDK有默认的超时时间,非必要请不要进行调整
  111. * 如有需要请在代码中查阅以获取最新的默认值 */
  112. httpProfile.setConnTimeout(60);
  113. /* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com
  114. * 南京地域
  115. * */
  116. httpProfile.setEndpoint("sms.ap-nanjing.tencentcloudapi.com");
  117. /* 非必要步骤:
  118. * 实例化一个客户端配置对象,可以指定超时时间等配置 */
  119. ClientProfile clientProfile = new ClientProfile();
  120. /* SDK默认用TC3-HMAC-SHA256进行签名
  121. * 非必要请不要修改这个字段 */
  122. clientProfile.setSignMethod("HmacSHA256");
  123. clientProfile.setHttpProfile(httpProfile);
  124. /* 实例化要请求产品(以sms为例)的client对象
  125. * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  126. * 南京地域
  127. * */
  128. SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
  129. /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
  130. * 你可以直接查询SDK源码确定接口有哪些属性可以设置
  131. * 属性可能是基本类型,也可能引用了另一个数据结构
  132. * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
  133. SendSmsRequest req = new SendSmsRequest();
  134. /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
  135. // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
  136. req.setSmsSdkAppId(TENCENT_SMS_SKD_APP_ID);
  137. /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
  138. req.setSignName(TENCENT_SMS_SIGN_NAME);
  139. /* 模板 ID: 必须填写已审核通过的模板 ID */
  140. req.setTemplateId(templateId);
  141. /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
  142. String[] templateParamSet = {msg};
  143. req.setTemplateParamSet(templateParamSet);
  144. /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
  145. * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
  146. mobile = String.format("+%s%s", cid, mobile);
  147. String[] phoneNumberSet = {mobile};
  148. req.setPhoneNumberSet(phoneNumberSet);
  149. /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
  150. * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
  151. SendSmsResponse res = client.SendSms(req);
  152. // 输出json格式的字符串回包
  153. log.info(SendSmsResponse.toJsonString(res));
  154. SendStatus[] sendStatusSet = res.getSendStatusSet();
  155. SendStatus sendStatus = sendStatusSet[0];
  156. if ("Ok".equalsIgnoreCase(sendStatus.getCode())) {
  157. return true;
  158. }
  159. // 也可以取出单个值,你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
  160. // System.out.println(res.getRequestId());
  161. /* 当出现以下错误码时,快速解决方案参考
  162. * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  163. * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  164. * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  165. * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
  166. * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
  167. */
  168. } catch (TencentCloudSDKException e) {
  169. log.error("发送腾讯云短信错误,e:{}", e);
  170. }
  171. return false;
  172. }
  173. public static void checkTencentCaptcha(String ticket, String userIp,String randsStr) {
  174. try {
  175. // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
  176. // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
  177. // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
  178. Credential cred = new Credential(TENCENT_CAPTCHA_SECRET_ID, TENCENT_CAPTCHA_SECRET_KEY);
  179. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  180. HttpProfile httpProfile = new HttpProfile();
  181. // 推荐使用北极星,相关指引可访问如下链接
  182. // https://git.woa.com/tencentcloud-internal/tencentcloud-sdk-java#%E5%8C%97%E6%9E%81%E6%98%9F
  183. httpProfile.setEndpoint("captcha.tencentcloudapi.com");
  184. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  185. ClientProfile clientProfile = new ClientProfile();
  186. clientProfile.setHttpProfile(httpProfile);
  187. // 实例化要请求产品的client对象,clientProfile是可选的
  188. CaptchaClient client = new CaptchaClient(cred, "", clientProfile);
  189. // 实例化一个请求对象,每个接口都会对应一个request对象
  190. DescribeCaptchaResultRequest req = new DescribeCaptchaResultRequest();
  191. req.setCaptchaType(9L);
  192. req.setTicket(ticket);
  193. req.setUserIp(userIp);
  194. req.setRandstr(randsStr);
  195. req.setCaptchaAppId(TENCENT_CAPTCHA_APP_ID);
  196. req.setAppSecretKey(TENCENT_CAPTCHA_APP_SECRET_KEY);
  197. // 返回的resp是一个DescribeCaptchaResultResponse的实例,与请求对象对应
  198. DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);
  199. //1 OK 验证通过
  200. Long captchaCode = resp.getCaptchaCode();
  201. if (captchaCode != 0) {
  202. log.error("腾讯云图形验证错误:{}", resp.getCaptchaMsg());
  203. }
  204. } catch (TencentCloudSDKException e) {
  205. log.error("腾讯云图形认证异常:{}", StringUtil.getErrorText(e));
  206. }
  207. }
  208. }