SmsUtil.java 13 KB

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