Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cnooc_zydeepen-cggl_expert-manage-miniapp
Overview
Overview
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
孙德龙
cnooc_zydeepen-cggl_expert-manage-miniapp
Commits
337d1896
Commit
337d1896
authored
Nov 17, 2025
by
刘红梅
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
发送短信代码
parent
cb112b06
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
40 deletions
+43
-40
src/main/java/com/cnooc/expert/auth/service/SmsService.java
+3
-3
src/main/java/com/cnooc/expert/auth/service/impl/SmsServiceImpl.java
+32
-30
src/main/java/com/cnooc/expert/common/utils/SmsHttpUtil.java
+2
-1
src/main/java/com/cnooc/expert/system/entity/vo/SmsConfig.java
+1
-5
src/main/resources/application.yml
+5
-1
No files found.
src/main/java/com/cnooc/expert/auth/service/SmsService.java
View file @
337d1896
...
...
@@ -19,10 +19,10 @@ public interface SmsService {
*/
boolean
sendSmsCode
(
String
phone
);
boolean
send
Mas
SmsCode
(
String
phone
);
boolean
send
Gly
SmsCode
(
String
phone
);
boolean
send
MasSmsContent
(
String
phone
,
String
content
);
boolean
send
GlySmsContent
(
String
phone
,
String
content
,
String
code
);
void
asyncSend
MasSmsContent
(
String
phone
,
String
content
);
void
asyncSend
GlySmsContent
(
String
phone
,
String
content
,
String
code
);
}
src/main/java/com/cnooc/expert/auth/service/impl/SmsServiceImpl.java
View file @
337d1896
...
...
@@ -2,6 +2,7 @@ package com.cnooc.expert.auth.service.impl;
import
cn.hutool.core.util.RandomUtil
;
import
com.cnooc.expert.common.constant.TokenConstants
;
import
com.cnooc.expert.common.utils.JsonUtils
;
import
com.cnooc.expert.common.utils.SmsHttpUtil
;
import
com.cnooc.expert.common.utils.SmsUtil
;
import
com.cnooc.expert.system.entity.vo.SmsConfig
;
...
...
@@ -20,7 +21,7 @@ import java.util.concurrent.CompletableFuture;
@AllArgsConstructor
@Slf4j
public
class
SmsServiceImpl
implements
SmsService
{
public
static
final
String
SMS_CODE_CONTENT
=
"
福建通信行业融合创新服务平台,
您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。"
;
public
static
final
String
SMS_CODE_CONTENT
=
"您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。"
;
@Autowired
private
StringRedisTemplate
redisTemplate
;
private
final
SmsConfig
smsConfig
;
...
...
@@ -59,11 +60,11 @@ public class SmsServiceImpl implements SmsService {
//String storedCode = (String)redisTemplate.opsForValue().get(key);
System
.
out
.
println
(
"发送短信验证码:"
+
phone
+
" -> "
+
code
);
// 模拟发送短信,实际应调用第三方短信服务
return
true
;
//return send
Mas
SmsCode(phone);
//return send
Gly
SmsCode(phone);
}
@Override
public
boolean
send
Mas
SmsCode
(
String
phone
)
{
public
boolean
send
Gly
SmsCode
(
String
phone
)
{
// 生成验证码并构建缓存键
String
code
=
SmsUtil
.
generateVerificationCode
();
System
.
out
.
println
(
"发送短信验证码:"
+
phone
+
" -> "
+
code
);
...
...
@@ -76,7 +77,7 @@ public class SmsServiceImpl implements SmsService {
log
.
info
(
"云MAS业务平台 发送手机号: {},短信验证码:{}"
,
phone
,
code
);
System
.
out
.
println
(
"云MAS业务平台 短信内容: "
+
content
);
boolean
result
=
send
MasSmsContent
(
phone
,
content
);
boolean
result
=
send
GlySmsContent
(
phone
,
content
,
code
);
if
(
result
){
redisTemplate
.
opsForValue
().
set
(
key
,
code
,
2
*
60
);
// 存入 Redis,设置过期时间为2分钟
}
...
...
@@ -89,42 +90,37 @@ public class SmsServiceImpl implements SmsService {
}
@Override
public
boolean
send
MasSmsContent
(
String
phone
,
String
content
)
{
public
boolean
send
GlySmsContent
(
String
phone
,
String
content
,
String
code
)
{
try
{
log
.
info
(
"云MAS业务平台 短信内容: {}"
,
content
);
log
.
info
(
"云MAS业务平台 发送手机号: {},短信内容:{}"
,
phone
,
content
);
// 生成 MAC 签名
String
mac
=
SmsUtil
.
calculateMac
(
smsConfig
.
getEcName
(),
smsConfig
.
getApId
(),
smsConfig
.
getSecretKey
(),
phone
,
content
,
smsConfig
.
getSign
(),
smsConfig
.
getAddSerial
());
System
.
out
.
println
(
"云MAS业务平台 mac签名: "
+
mac
);
log
.
info
(
"云MAS业务平台 mac签名: {}"
,
mac
);
log
.
info
(
"管理云业务平台 短信内容: {}"
,
content
);
log
.
info
(
"管理云业务平台 发送手机号: {},短信内容:{}"
,
phone
,
content
);
// 构建请求体并转换为 Base64 编码的 JSON 字符串
String
encodedJson
=
SmsUtil
.
encodeRequestBodyToBase64
(
buildRequestBody
(
phone
,
content
,
mac
));
System
.
out
.
println
(
"
云MAS
业务平台 请求体Base64编码: "
+
encodedJson
);
log
.
info
(
"
云MAS
业务平台 请求体Base64编码: {}"
,
encodedJson
);
String
encodedJson
=
SmsUtil
.
encodeRequestBodyToBase64
(
buildRequestBody
(
phone
,
content
,
code
));
System
.
out
.
println
(
"
管理云
业务平台 请求体Base64编码: "
+
encodedJson
);
log
.
info
(
"
管理云
业务平台 请求体Base64编码: {}"
,
encodedJson
);
// 设置请求头并发送 POST 请求
String
response
=
SmsHttpUtil
.
sendPostRequest
(
smsConfig
.
getApiUrl
(),
encodedJson
);
System
.
out
.
println
(
"
云MAS
业务平台 响应体: "
+
response
);
log
.
info
(
"
云MAS
业务平台 响应体response: {}"
,
response
);
String
response
=
SmsHttpUtil
.
sendPostRequest
(
smsConfig
.
getApiUrl
(),
encodedJson
,
smsConfig
.
getAppCode
()
);
System
.
out
.
println
(
"
管理云
业务平台 响应体: "
+
response
);
log
.
info
(
"
管理云
业务平台 响应体response: {}"
,
response
);
// 解析响应并处理结果
return
SmsUtil
.
handleResponse
(
response
);
}
catch
(
Exception
e
)
{
log
.
error
(
"
云MAS
业务平台短信发送失败: {}"
,
e
.
getMessage
(),
e
);
log
.
error
(
"
管理云
业务平台短信发送失败: {}"
,
e
.
getMessage
(),
e
);
return
false
;
}
}
@Override
public
void
asyncSend
MasSmsContent
(
String
phone
,
String
content
){
public
void
asyncSend
GlySmsContent
(
String
phone
,
String
content
,
String
code
){
CompletableFuture
.
runAsync
(()
->
{
try
{
System
.
out
.
println
(
"======开始发送短信======"
);
send
MasSmsContent
(
phone
,
content
);
send
GlySmsContent
(
phone
,
content
,
code
);
System
.
out
.
println
(
"======发送短信结束======"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"asyncSendMasSmsContent异步执行失败"
,
e
);
...
...
@@ -136,16 +132,22 @@ public class SmsServiceImpl implements SmsService {
/**
* 构建请求体
*/
private
Map
<
String
,
String
>
buildRequestBody
(
String
phone
,
String
content
,
String
mac
)
{
private
Map
<
String
,
String
>
buildRequestBody
(
String
phone
,
String
content
,
String
code
)
{
Map
<
String
,
Object
>
sms
=
new
HashMap
<>();
Map
<
String
,
String
>
dataItems
=
new
HashMap
<>();
dataItems
.
put
(
"code"
,
code
);
sms
.
put
(
"template"
,
"短信验证码"
);
sms
.
put
(
"dataItems"
,
JsonUtils
.
toJsonString
((
dataItems
)));
sms
.
put
(
"type"
,
"1"
);
String
[]
recvNumArray
=
new
String
[
1
];
recvNumArray
[
0
]
=
phone
;
sms
.
put
(
"recvNum"
,
recvNumArray
);
sms
.
put
(
"content"
,
content
);
sms
.
put
(
"sendNum"
,
""
);
Map
<
String
,
String
>
requestBody
=
new
HashMap
<>();
requestBody
.
put
(
"ecName"
,
smsConfig
.
getEcName
());
requestBody
.
put
(
"apId"
,
smsConfig
.
getApId
());
requestBody
.
put
(
"mobiles"
,
phone
);
requestBody
.
put
(
"content"
,
content
);
requestBody
.
put
(
"secretKey"
,
smsConfig
.
getSecretKey
());
requestBody
.
put
(
"sign"
,
smsConfig
.
getSign
());
requestBody
.
put
(
"addSerial"
,
smsConfig
.
getAddSerial
());
requestBody
.
put
(
"mac"
,
mac
);
requestBody
.
put
(
"priority"
,
"high"
);
requestBody
.
put
(
"label"
,
"消息标签"
);
requestBody
.
put
(
"sms"
,
JsonUtils
.
toJsonString
(
sms
));
return
requestBody
;
}
...
...
src/main/java/com/cnooc/expert/common/utils/SmsHttpUtil.java
View file @
337d1896
...
...
@@ -16,11 +16,12 @@ public class SmsHttpUtil {
* @param requestBody 请求体 Map
* @return 响应字符串
*/
public
static
String
sendPostRequest
(
String
apiUrl
,
String
requestBody
)
{
public
static
String
sendPostRequest
(
String
apiUrl
,
String
requestBody
,
String
appCode
)
{
try
{
// 设置请求头
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
set
(
"Authorization"
,
"APPCODE "
+
appCode
);
// 构建请求实体
HttpEntity
<
String
>
requestEntity
=
new
HttpEntity
<>(
requestBody
,
headers
);
...
...
src/main/java/com/cnooc/expert/system/entity/vo/SmsConfig.java
View file @
337d1896
...
...
@@ -11,10 +11,6 @@ import org.springframework.stereotype.Component;
public
class
SmsConfig
{
private
String
apiUrl
;
private
String
ecName
;
private
String
apId
;
private
String
secretKey
;
private
String
sign
;
private
String
addSerial
;
private
String
appCode
;
}
src/main/resources/application.yml
View file @
337d1896
...
...
@@ -37,7 +37,11 @@ spring:
batch-size
:
16384
linger-ms
:
1
buffer-memory
:
33554432
sms
:
config
:
# 短信发送配置
api_url
:
https://gly.api.mcmptest.cnooc/msg/mcmp-msg-api/message
app_code
:
3F2504E04F8911D39A0C0305E82C3301
app
:
info
:
appId
:
10000
...
...
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