Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
C
communication
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
communication
Commits
8b056a04
Commit
8b056a04
authored
Jul 01, 2021
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
语音服务修改 gengdidi
parent
c4b230cc
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
233 additions
and
80 deletions
+233
-80
pom.xml
pom.xml
+23
-7
LogInterceptor.java
...hong/communication/common/interceptor/LogInterceptor.java
+8
-16
ResultEnum.java
...va/com/zehong/communication/common/result/ResultEnum.java
+2
-1
SmsController.java
...va/com/zehong/communication/controller/SmsController.java
+14
-5
VoiceNotifyController.java
...ehong/communication/controller/VoiceNotifyController.java
+13
-5
SendBatchSms.java
...ain/java/com/zehong/communication/model/SendBatchSms.java
+19
-1
SendBatchVoiceNotify.java
.../com/zehong/communication/model/SendBatchVoiceNotify.java
+20
-1
SendSms.java
src/main/java/com/zehong/communication/model/SendSms.java
+21
-0
SendVoiceNotify.java
.../java/com/zehong/communication/model/SendVoiceNotify.java
+21
-0
SmsService.java
...ain/java/com/zehong/communication/service/SmsService.java
+16
-2
VoiceNotifyService.java
.../com/zehong/communication/service/VoiceNotifyService.java
+6
-4
SmsServiceImpl.java
...com/zehong/communication/service/impl/SmsServiceImpl.java
+18
-25
VoiceNotifyServiceImpl.java
...ng/communication/service/impl/VoiceNotifyServiceImpl.java
+16
-9
application.yml
src/main/resources/application.yml
+8
-4
log4j.properties
src/main/resources/log4j.properties
+28
-0
No files found.
pom.xml
View file @
8b056a04
...
...
@@ -53,15 +53,20 @@
</dependency>
<!--log日志-->
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
<version>
1.2
</version>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-logging
</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
log4j
</groupId>
<artifactId>
log4j
</artifactId>
<version>
1.
2.17
</version>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-
log4j
</artifactId>
<version>
1.
3.8.RELEASE
</version>
</dependency>
<!--commons-lang-->
<dependency>
...
...
@@ -92,6 +97,17 @@
<version>
1.18.4
</version>
<scope>
provided
</scope>
</dependency>
<!--validation-->
<dependency>
<groupId>
javax.validation
</groupId>
<artifactId>
validation-api
</artifactId>
<version>
1.1.0.Final
</version>
</dependency>
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<version>
5.4.1.Final
</version>
</dependency>
</dependencies>
...
...
src/main/java/com/zehong/communication/common/interceptor/LogInterceptor.java
View file @
8b056a04
...
...
@@ -20,8 +20,6 @@ import org.springframework.web.context.request.RequestContextHolder;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
java.util.Date
;
...
...
@@ -33,6 +31,7 @@ import java.util.UUID;
public
class
LogInterceptor
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
LogInterceptor
.
class
);
private
static
final
Logger
requestLogger
=
LoggerFactory
.
getLogger
(
"RequestLog"
);
@Autowired
private
RequestLogService
requestLogService
;
...
...
@@ -118,19 +117,12 @@ public class LogInterceptor {
* @param result 请求结果
*/
private
void
writeToLocalFile
(
String
params
,
String
result
)
{
try
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
FileWriter
writer
=
new
FileWriter
(
fileUrl
,
true
);
writer
.
write
(
"\n time:"
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
writer
.
write
(
" requestMapping:"
+
request
.
getRequestURL
().
toString
());
writer
.
write
(
" params:"
+
params
);
writer
.
write
(
" postTime:"
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
writer
.
write
(
" result:"
+
JSONObject
.
toJSONString
(
result
));
writer
.
flush
();
writer
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"写入本地文件出错:"
+
e
);
}
StringBuilder
requestLog
=
new
StringBuilder
();
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
requestLog
.
append
(
"requestMapping:"
).
append
(
request
.
getRequestURL
().
toString
())
.
append
(
" params:"
).
append
(
params
)
.
append
(
" postTime:"
).
append
(
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()))
.
append
(
" result:"
).
append
(
JSONObject
.
toJSONString
(
result
));
requestLogger
.
info
(
requestLog
.
toString
());
}
}
src/main/java/com/zehong/communication/common/result/ResultEnum.java
View file @
8b056a04
...
...
@@ -5,7 +5,8 @@ public enum ResultEnum {
* 请求成功
*/
SUCCESS
(
"0"
,
"接口请求成功!"
),
BUSSINESS_FAIL
(
"1"
,
"业务异常!"
),
FAIL
(
"2"
,
"接口请求失败!"
),
SYSTEM_EXCEPTION
(
"3"
,
"系统异常!"
)
,
NOT_IN_IPWHITE
(
"4"
,
"当前IP不在白名单中,请联系管理员"
),
SECRETKRY_ERROR
(
"5"
,
"secretKey不存在或秘钥不正确!"
);
,
NOT_IN_IPWHITE
(
"4"
,
"当前IP不在白名单中,请联系管理员"
),
SECRETKRY_ERROR
(
"5"
,
"secretKey不存在或秘钥不正确!"
)
,
VALIDATA_ERROR
(
"6"
,
""
);
private
String
code
;
private
String
msg
;
...
...
src/main/java/com/zehong/communication/controller/SmsController.java
View file @
8b056a04
...
...
@@ -10,11 +10,14 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.PropertySource
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.validation.Valid
;
/**
* 发送短信
* @author gengdidi
...
...
@@ -35,9 +38,12 @@ public class SmsController {
* @return Result
*/
@RequestMapping
(
value
=
"/sendSms"
,
method
=
RequestMethod
.
GET
)
public
Result
sendSms
(
@
RequestBody
SendSms
sendSms
){
public
Result
sendSms
(
@
Valid
@RequestBody
SendSms
sendSms
,
BindingResult
result
){
try
{
return
smsService
.
sendSms
(
sendSms
.
getPhoneNumber
());
if
(
result
.
hasErrors
()){
return
new
Result
(
ResultEnum
.
VALIDATA_ERROR
.
getCode
(),
result
.
getFieldError
().
getDefaultMessage
());
}
return
smsService
.
sendSms
(
sendSms
);
}
catch
(
ClientException
e
)
{
logger
.
info
(
"短信发送失败,phoneNumber:%"
,
sendSms
.
getPhoneNumber
(),
e
);
return
new
Result
(
ResultEnum
.
SYSTEM_EXCEPTION
);
...
...
@@ -50,11 +56,14 @@ public class SmsController {
* @return Result
*/
@RequestMapping
(
value
=
"/sendBatchSms"
,
method
=
RequestMethod
.
POST
)
public
Result
sendBatchSms
(
@
RequestBody
SendBatchSms
sendBatchSms
){
public
Result
sendBatchSms
(
@
Valid
@RequestBody
SendBatchSms
sendBatchSms
,
BindingResult
result
){
try
{
return
smsService
.
sendBatchSms
(
sendBatchSms
.
getPhoneNumbers
());
if
(
result
.
hasErrors
()){
return
new
Result
(
ResultEnum
.
VALIDATA_ERROR
.
getCode
(),
result
.
getFieldError
().
getDefaultMessage
());
}
return
smsService
.
sendBatchSms
(
sendBatchSms
);
}
catch
(
ClientException
e
)
{
logger
.
info
(
"批量发送短信失败,phoneNumbers:%"
,
sendBatchSms
.
getPhoneNumber
s
(),
e
);
logger
.
info
(
"批量发送短信失败,phoneNumbers:%"
,
sendBatchSms
.
getPhoneNumber
Json
(),
e
);
return
new
Result
(
ResultEnum
.
SYSTEM_EXCEPTION
);
}
}
...
...
src/main/java/com/zehong/communication/controller/VoiceNotifyController.java
View file @
8b056a04
...
...
@@ -8,8 +8,10 @@ import com.zehong.communication.service.VoiceNotifyService;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.*
;
import
javax.validation.Valid
;
import
java.io.IOException
;
import
java.net.URISyntaxException
;
...
...
@@ -31,9 +33,12 @@ public class VoiceNotifyController {
* @return String
*/
@RequestMapping
(
value
=
"/sendVoiceNotify"
,
method
=
RequestMethod
.
GET
)
public
Result
sendVoiceNotify
(
@
RequestBody
SendVoiceNotify
sendVoiceNotify
){
public
Result
sendVoiceNotify
(
@
Valid
@RequestBody
SendVoiceNotify
sendVoiceNotify
,
BindingResult
result
){
try
{
return
voiceNotifyService
.
sendVoiceNotify
(
sendVoiceNotify
.
getPhoneNumber
());
if
(
result
.
hasErrors
()){
return
new
Result
(
ResultEnum
.
VALIDATA_ERROR
.
getCode
(),
result
.
getFieldError
().
getDefaultMessage
());
}
return
voiceNotifyService
.
sendVoiceNotify
(
sendVoiceNotify
);
}
catch
(
IOException
|
URISyntaxException
e
)
{
logger
.
info
(
"语音发送失败!phoneNumber:%"
,
sendVoiceNotify
.
getPhoneNumber
(),
e
);
return
new
Result
(
ResultEnum
.
SYSTEM_EXCEPTION
);
...
...
@@ -46,11 +51,14 @@ public class VoiceNotifyController {
* @return String
*/
@RequestMapping
(
value
=
"/sendBatchVoiceNotify"
,
method
=
RequestMethod
.
GET
)
public
Result
sendBatchVoiceNotify
(
@
RequestBody
SendBatchVoiceNotify
sendBatchVoiceNotify
){
public
Result
sendBatchVoiceNotify
(
@
Valid
@RequestBody
SendBatchVoiceNotify
sendBatchVoiceNotify
,
BindingResult
result
){
try
{
return
voiceNotifyService
.
sendBatchVoiceNotify
(
sendBatchVoiceNotify
.
getPhoneNumbers
());
if
(
result
.
hasErrors
()){
return
new
Result
(
ResultEnum
.
VALIDATA_ERROR
.
getCode
(),
result
.
getFieldError
().
getDefaultMessage
());
}
return
voiceNotifyService
.
sendBatchVoiceNotify
(
sendBatchVoiceNotify
);
}
catch
(
IOException
|
URISyntaxException
e
)
{
logger
.
info
(
"群发语音失败!phoneNumbers:%"
,
sendBatchVoiceNotify
.
getPhoneNumber
s
(),
e
);
logger
.
info
(
"群发语音失败!phoneNumbers:%"
,
sendBatchVoiceNotify
.
getPhoneNumber
Json
(),
e
);
return
new
Result
(
ResultEnum
.
SYSTEM_EXCEPTION
);
}
}
...
...
src/main/java/com/zehong/communication/model/SendBatchSms.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
model
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.NotBlank
;
@Data
public
class
SendBatchSms
extends
BaseRequestModel
{
private
String
[]
phoneNumbers
;
/**
* 群发号码
* [\"13333333333\"]
*/
@NotBlank
(
message
=
"手机号码不能为空"
)
private
String
phoneNumberJson
;
/**
* 群发短信模板
*/
@NotBlank
(
message
=
"短信模板不能为空"
)
private
String
templateCode
;
/**
* [{\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"},{\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"}]
* 群发短信变量
*/
private
String
templateParamJson
;
}
src/main/java/com/zehong/communication/model/SendBatchVoiceNotify.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
model
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.NotBlank
;
@Data
public
class
SendBatchVoiceNotify
extends
BaseRequestModel
{
private
String
phoneNumbers
;
/**
* 语音群发号码
* "[133333333,133333333]"
*/
@NotBlank
(
message
=
"语音群发号码不能为空"
)
private
String
phoneNumberJson
;
/**
* 语音模板
*/
@NotBlank
(
message
=
"语音模板不能为空"
)
private
String
templateId
;
/**
*模板变量字符串
* [{\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"},{\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"}]
*/
private
String
paramJson
;
}
src/main/java/com/zehong/communication/model/SendSms.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
model
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.NotBlank
;
import
org.hibernate.validator.constraints.NotEmpty
;
import
javax.validation.constraints.Pattern
;
@Data
public
class
SendSms
extends
BaseRequestModel
{
/**
* 手机号码
*/
@NotEmpty
(
message
=
"手机号不能为空"
)
@Pattern
(
regexp
=
"^1[0-9]{10}$"
,
message
=
"手机号不合法"
)
private
String
phoneNumber
;
/**
* 短信模板
*/
@NotBlank
(
message
=
"短信模板不能为空"
)
private
String
templateCode
;
/**
* 模板变量
* {\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"}
*/
private
String
templateParam
;
}
src/main/java/com/zehong/communication/model/SendVoiceNotify.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
model
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.NotBlank
;
import
org.hibernate.validator.constraints.NotEmpty
;
import
javax.validation.constraints.Pattern
;
@Data
public
class
SendVoiceNotify
extends
BaseRequestModel
{
/**
* 手机号码
*/
@NotEmpty
(
message
=
"手机号不能为空"
)
@Pattern
(
regexp
=
"^1[0-9]{10}$"
,
message
=
"手机号不合法"
)
private
String
phoneNumber
;
/**
* 语音模板
*/
@NotBlank
(
message
=
"语音模板不能为空"
)
private
String
templateId
;
/**
* 模板变量字符串
* {\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"}
*/
private
String
param
;
}
src/main/java/com/zehong/communication/service/SmsService.java
View file @
8b056a04
...
...
@@ -2,10 +2,24 @@ package com.zehong.communication.service;
import
com.aliyuncs.exceptions.ClientException
;
import
com.zehong.communication.common.result.Result
;
import
com.zehong.communication.model.SendBatchSms
;
import
com.zehong.communication.model.SendSms
;
public
interface
SmsService
{
Result
sendSms
(
String
phoneNumber
)
throws
ClientException
;
/**
* 发送短信
* @param sendSms 发送短信实体
* @return Result
* @throws ClientException
*/
Result
sendSms
(
SendSms
sendSms
)
throws
ClientException
;
Result
sendBatchSms
(
String
[]
phoneNumbers
)
throws
ClientException
;
/**
* 批量发送短信
* @param sendBatchSms 批量发送短信实体
* @return Result
* @throws ClientException
*/
Result
sendBatchSms
(
SendBatchSms
sendBatchSms
)
throws
ClientException
;
}
src/main/java/com/zehong/communication/service/VoiceNotifyService.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
service
;
import
com.zehong.communication.common.result.Result
;
import
com.zehong.communication.model.SendBatchVoiceNotify
;
import
com.zehong.communication.model.SendVoiceNotify
;
import
java.io.IOException
;
import
java.net.URISyntaxException
;
...
...
@@ -9,15 +11,15 @@ public interface VoiceNotifyService {
/**
* 发送语音 被叫号码,限单个,仅支持11位国内号码
* @param
phoneNumber 手机号
* @param
sendVoiceNotify 实体
* @return String
*/
Result
sendVoiceNotify
(
S
tring
phoneNumber
)
throws
IOException
,
URISyntaxException
;
Result
sendVoiceNotify
(
S
endVoiceNotify
sendVoiceNotify
)
throws
IOException
,
URISyntaxException
;
/**
*批量发送语音 被叫号码,模版无变量多个则以英文半角分号“;”分割;模板带变量,多个号码以英文分号“;”分割,号码和变量以英文逗号“,”隔开, 多个变量以竖线“|”隔开,如 130XXXXXXXX,123|456;186XXXXXXXX,321|654(注意:文件语音模板类型 号码之间分隔用“,”隔开)
* @param
phoneNumbers 手机号
* @param
sendBatchVoiceNotify 实体
* @return String
*/
Result
sendBatchVoiceNotify
(
S
tring
phoneNumbers
)
throws
IOException
,
URISyntaxException
;
Result
sendBatchVoiceNotify
(
S
endBatchVoiceNotify
sendBatchVoiceNotify
)
throws
IOException
,
URISyntaxException
;
}
src/main/java/com/zehong/communication/service/impl/SmsServiceImpl.java
View file @
8b056a04
...
...
@@ -13,7 +13,10 @@ import com.aliyuncs.profile.DefaultProfile;
import
com.aliyuncs.profile.IClientProfile
;
import
com.zehong.communication.common.result.Result
;
import
com.zehong.communication.common.result.ResultEnum
;
import
com.zehong.communication.model.SendBatchSms
;
import
com.zehong.communication.model.SendSms
;
import
com.zehong.communication.service.SmsService
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
...
...
@@ -31,11 +34,8 @@ public class SmsServiceImpl implements SmsService {
@Value
(
"${signName}"
)
private
String
signName
;
@Value
(
"${templateCode}"
)
private
String
templateCode
;
@Value
(
"${signNameJson}"
)
private
String
S
ignNameJson
;
private
String
s
ignNameJson
;
/**
* 短信API产品名称(短信产品名固定,无需修改)
...
...
@@ -48,7 +48,7 @@ public class SmsServiceImpl implements SmsService {
private
final
String
DOMAIN
=
"dysmsapi.aliyuncs.com"
;
@Override
public
Result
sendSms
(
S
tring
phoneNumber
)
throws
ClientException
{
public
Result
sendSms
(
S
endSms
sendSms
)
throws
ClientException
{
//设置超时时间-可自行调整
System
.
setProperty
(
"sun.net.client.defaultConnectTimeout"
,
"10000"
);
System
.
setProperty
(
"sun.net.client.defaultReadTimeout"
,
"10000"
);
...
...
@@ -60,11 +60,15 @@ public class SmsServiceImpl implements SmsService {
IAcsClient
acsClient
=
new
DefaultAcsClient
(
profile
);
SendSmsRequest
request
=
getSmsRequest
();
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000”
request
.
setPhoneNumbers
(
phoneNumber
);
request
.
setPhoneNumbers
(
sendSms
.
getPhoneNumber
());
//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
request
.
setTemplateCode
(
sendSms
.
getTemplateCode
());
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
//友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
//参考:request.setTemplateParam("{\"变量1\":\"值1\",\"变量2\":\"值2\",\"变量3\":\"值3\"}")
request
.
setTemplateParam
(
"{\"cphone\":\""
+
phoneNumber
+
"\"}"
);
if
(
StringUtils
.
isNotBlank
(
sendSms
.
getTemplateParam
())){
request
.
setTemplateParam
(
sendSms
.
getTemplateParam
());
}
//请求失败这里会抛ClientException异常
SendSmsResponse
sendSmsResponse
=
acsClient
.
getAcsResponse
(
request
);
if
(
sendSmsResponse
.
getCode
()
!=
null
&&
sendSmsResponse
.
getCode
().
equals
(
"OK"
))
{
...
...
@@ -75,7 +79,7 @@ public class SmsServiceImpl implements SmsService {
}
@Override
public
Result
sendBatchSms
(
S
tring
[]
phoneNumber
s
)
throws
ClientException
{
public
Result
sendBatchSms
(
S
endBatchSms
sendBatchSm
s
)
throws
ClientException
{
//设置超时时间-可自行调整
System
.
setProperty
(
"sun.net.client.defaultConnectTimeout"
,
"10000"
);
System
.
setProperty
(
"sun.net.client.defaultReadTimeout"
,
"10000"
);
...
...
@@ -91,14 +95,16 @@ public class SmsServiceImpl implements SmsService {
request
.
setMethod
(
MethodType
.
POST
);
//必填:待发送手机号。支持JSON格式的批量调用,批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
//"[\"1500000000\",\"1500000001\"]"
request
.
setPhoneNumberJson
(
new
JSONArray
(
Arrays
.
asList
(
phoneNumbers
)).
toJSONString
());
request
.
setPhoneNumberJson
(
sendBatchSms
.
getPhoneNumberJson
());
//必填:短信签名-支持不同的号码发送不同的短信签名
request
.
setSignNameJson
(
S
ignNameJson
);
request
.
setSignNameJson
(
s
ignNameJson
);
//必填:短信模板-可在短信控制台中找到
request
.
setTemplateCode
(
templateCode
);
request
.
setTemplateCode
(
sendBatchSms
.
getTemplateCode
()
);
//必填:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
//友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
request
.
setTemplateParamJson
(
getTemplateParamJson
(
phoneNumbers
));
if
(
StringUtils
.
isNotBlank
(
sendBatchSms
.
getTemplateParamJson
())){
request
.
setTemplateParamJson
(
sendBatchSms
.
getTemplateParamJson
());
}
//可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCodeJson("[\"90997\",\"90998\"]");
//请求失败这里会抛ClientException异常
...
...
@@ -117,21 +123,8 @@ public class SmsServiceImpl implements SmsService {
request
.
setMethod
(
MethodType
.
POST
);
//必填:短信签名-可在短信控制台中找到
request
.
setSignName
(
signName
);
//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
request
.
setTemplateCode
(
templateCode
);
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request
.
setOutId
(
"yourOutId"
);
return
request
;
}
private
String
getTemplateParamJson
(
String
[]
phoneNumbers
){
StringBuilder
templateParamJson
=
new
StringBuilder
(
"["
);
for
(
String
phoneNumber
:
phoneNumbers
){
templateParamJson
.
append
(
"{\"cphone\":\""
);
templateParamJson
.
append
(
phoneNumber
);
templateParamJson
.
append
(
"\"}"
);
}
templateParamJson
.
append
(
"]"
);
return
templateParamJson
.
toString
();
}
}
src/main/java/com/zehong/communication/service/impl/VoiceNotifyServiceImpl.java
View file @
8b056a04
package
com
.
zehong
.
communication
.
service
.
impl
;
import
com.zehong.communication.common.result.Result
;
import
com.zehong.communication.model.SendBatchVoiceNotify
;
import
com.zehong.communication.model.SendVoiceNotify
;
import
com.zehong.communication.service.VoiceNotifyService
;
import
com.zehong.communication.util.HttpUtil
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
...
...
@@ -23,23 +26,29 @@ public class VoiceNotifyServiceImpl implements VoiceNotifyService {
@Value
(
"${appKey}"
)
private
String
appKey
;
@Value
(
"${templateId}"
)
private
String
templateId
;
@Value
(
"${batchVoiceNotifyURL}"
)
private
String
batchVoiceNotifyURL
;
@Override
public
Result
sendVoiceNotify
(
S
tring
phoneNumber
)
throws
IOException
,
URISyntaxException
{
public
Result
sendVoiceNotify
(
S
endVoiceNotify
sendVoiceNotify
)
throws
IOException
,
URISyntaxException
{
Map
<
String
,
Object
>
params
=
getParams
();
params
.
put
(
"mobile"
,
phoneNumber
);
params
.
put
(
"mobile"
,
sendVoiceNotify
.
getPhoneNumber
());
params
.
put
(
"templateId"
,
sendVoiceNotify
.
getTemplateId
());
if
(
StringUtils
.
isNotBlank
(
sendVoiceNotify
.
getParam
())){
params
.
put
(
"param"
,
sendVoiceNotify
.
getParam
());
}
return
HttpUtil
.
doPost
(
voiceNotifyURL
,
params
);
}
@Override
public
Result
sendBatchVoiceNotify
(
S
tring
phoneNumbers
)
throws
IOException
,
URISyntaxException
{
public
Result
sendBatchVoiceNotify
(
S
endBatchVoiceNotify
sendBatchVoiceNotify
)
throws
IOException
,
URISyntaxException
{
Map
<
String
,
Object
>
params
=
getParams
();
params
.
put
(
"mobile"
,
phoneNumbers
);
params
.
put
(
"mobile"
,
sendBatchVoiceNotify
.
getPhoneNumberJson
());
params
.
put
(
"templateId"
,
sendBatchVoiceNotify
.
getTemplateId
());
if
(
StringUtils
.
isNotBlank
(
sendBatchVoiceNotify
.
getParamJson
())){
params
.
put
(
"param"
,
sendBatchVoiceNotify
.
getParamJson
());
}
return
HttpUtil
.
doPost
(
batchVoiceNotifyURL
,
params
);
}
...
...
@@ -47,8 +56,6 @@ public class VoiceNotifyServiceImpl implements VoiceNotifyService {
Map
<
String
,
Object
>
params
=
new
HashMap
<>();
params
.
put
(
"appId"
,
appId
);
params
.
put
(
"appKey"
,
appKey
);
params
.
put
(
"templateId"
,
templateId
);
params
.
put
(
"param"
,
"2222,某某街道,报警器报警(手机号后四位,地址,原因"
);
return
params
;
}
}
src/main/resources/application.yml
View file @
8b056a04
...
...
@@ -29,7 +29,6 @@ mybatis:
voiceNotifyURL
:
https://api.253.com/open/notify/voice-notify
appId
:
N69N0WkT
appKey
:
IE5F7Jih
templateId
:
4371
#批量发送语音
batchVoiceNotifyURL
:
https://api.253.com/open/notify/batch-voice-notify
...
...
@@ -38,7 +37,6 @@ batchVoiceNotifyURL: https://api.253.com/open/notify/batch-voice-notify
accessKeyId
:
LTAI2xiZNF3iV2aV
accessKeySecret
:
bprEWwn1M0xgglRQCQEMYSPiYctDk4
signName
:
泽宏云
templateCode
:
SMS_162524468
#阿里批量发送短信
signNameJson
:
[
\"泽宏云\"
]
...
...
@@ -50,5 +48,11 @@ iPWhite: 192.168.2.23
secretKey
:
f225e66813e6d6a663daf919c1935dcb
#是否写入本地
isWriteLocal
:
false
fileUrl
:
E:\java\work\requireLog.text
\ No newline at end of file
isWriteLocal
:
true
fileUrl
:
E:\java\work\requireLog.text
logging
:
# 设置logback.xml位置
# config: classpath:logback.xml
# 设置log4j.properties位置
config
:
classpath:log4j.properties
\ No newline at end of file
src/main/resources/log4j.properties
0 → 100644
View file @
8b056a04
log4j.rootLogger
=
INFO,Console,File
log4j.appender.Console
=
org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target
=
System.out
log4j.appender.Console.layout
=
org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern
=
[%p] [%d{yyyy-MM-dd HH
\:
mm
\:
ss}][%c - %L]%m%n
log4j.appender.File
=
org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File
=
E:/java/work/info.log
#log4j.appender.File.File = /data/java/communication/log/info
log4j.appender.File.DatePattern
=
_yyyy-MM-dd'.log'
log4j.appender.File.Append
=
true
log4j.appender.File.File.MaxFileSize
=
30MB
log4j.appender.File.Threshold
=
ALL
log4j.appender.File.layout
=
org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern
=
[%p] [%d{yyyy-MM-dd HH
\:
mm
\:
ss}][%c - %L]%m%n
#请求日志
log4j.logger.RequestLog
=
INFO,S
log4j.additivity.RequestLog
=
false
log4j.appender.S
=
org.apache.log4j.DailyRollingFileAppender
log4j.appender.S.File
=
E:/java/work/log.log
#log4j.appender.S.File = /data/java/communication/log/requestLog
log4j.appender.S.DatePattern
=
_yyyy-MM-dd'.log'
log4j.appender.S.Append
=
true
log4j.appender.S.Threshold
=
info
log4j.appender.S.File.MaxFileSize
=
30MB
log4j.appender.S.layout
=
org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern
=
[%d{MM-dd HH:mm:ss}] [%p] [%c:%L] %m%n
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment