From 30a8692a3aa23118d7c7c33c6d84327e5e199612 Mon Sep 17 00:00:00 2001 From: ZQN <364596817@qq.com> Date: 星期六, 22 六月 2024 17:28:45 +0800 Subject: [PATCH] 发送短信开关 --- project-system/src/main/java/com/project/system/sms/YPSmsApi.java | 41 +++++++++++++++++++++++++++-------------- 1 files changed, 27 insertions(+), 14 deletions(-) diff --git a/project-common/src/main/java/com/project/common/sms/YPSmsApi.java b/project-system/src/main/java/com/project/system/sms/YPSmsApi.java similarity index 79% rename from project-common/src/main/java/com/project/common/sms/YPSmsApi.java rename to project-system/src/main/java/com/project/system/sms/YPSmsApi.java index ce14141..5d39f36 100644 --- a/project-common/src/main/java/com/project/common/sms/YPSmsApi.java +++ b/project-system/src/main/java/com/project/system/sms/YPSmsApi.java @@ -1,7 +1,9 @@ -package com.project.common.sms; +package com.project.system.sms; import com.project.common.constant.Constants; import com.project.common.utils.SslUtils; +import com.project.system.service.ISysConfigService; +import lombok.RequiredArgsConstructor; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -10,6 +12,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; +import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; @@ -24,7 +27,13 @@ * @author Mr.Zhao */ +@Service +@RequiredArgsConstructor public class YPSmsApi { + + + private final ISysConfigService configService; + /** * 鏅鸿兘鍖归厤妯℃澘鍙戦�佸湴鍧� @@ -75,19 +84,18 @@ * @param mobile 銆�鎺ュ彈鐨勬墜鏈哄彿 * @param msg 銆�鐭俊鍐呭 */ - public static String sendSms(String mobile, String msg) + public String sendSms(String mobile, String msg) { - Map<String, String> params = new HashMap<>(3); - params.put("apikey", API_KEY); - params.put("text", msg); - params.put("mobile", mobile); - return post(YP_SMS_URI, params); + String smsFlag = configService.selectConfigByKey("sys.sms.flag"); + if ("1".equals(smsFlag)) { + Map<String, String> params = new HashMap<>(3); + params.put("apikey", API_KEY); + params.put("text", msg); + params.put("mobile", mobile); + return post(YP_SMS_URI, params); + } + return ""; } - - public static void main(String[] args) { - sendSms("18537821663", "銆愰噾鏄庢簮銆戞偍鐨勯獙璇佺爜鏄�1234"); - } - /** * 鍩轰簬HttpClient 4.3鐨勯�氱敤POST鏂规硶 @@ -116,8 +124,13 @@ } method.setEntity(new UrlEncodedFormEntity(paramList, Constants.UTF8)); } - response = client.execute(method); - HttpEntity entity = response.getEntity(); + if (client != null) { + response = client.execute(method); + } + HttpEntity entity = null; + if (response != null) { + entity = response.getEntity(); + } if (entity != null) { responseText = EntityUtils.toString(entity); } -- Gitblit v1.9.1