Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Q
qk_backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
quanku
qk_backend
Commits
3d65d66d
Commit
3d65d66d
authored
Aug 24, 2023
by
muhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改 小程序缓存
parent
69ffc0c9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
19 deletions
+45
-19
ch-wx-api/src/main/java/com/wwdz/ch/wx/constant/CacheCodeConstants.java
...main/java/com/wwdz/ch/wx/constant/CacheCodeConstants.java
+13
-1
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
...pi/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
+30
-16
ch-wx-api/src/main/java/com/wwdz/ch/wx/manager/UserTokenManager.java
...rc/main/java/com/wwdz/ch/wx/manager/UserTokenManager.java
+2
-2
No files found.
ch-wx-api/src/main/java/com/wwdz/ch/wx/constant/CacheCodeConstants.java
View file @
3d65d66d
...
@@ -7,7 +7,19 @@ public class CacheCodeConstants {
...
@@ -7,7 +7,19 @@ public class CacheCodeConstants {
/**
/**
* 微信接口调用凭证access_token
* 微信接口调用凭证access_token
*/
*/
public
static
final
String
WX_ACCESS_TOKEN
=
"quanku:wx:access_token"
;
public
static
final
String
WX_ACCESS_TOKEN
=
"cjxc:wx:access_token"
;
/**
* 缓存用户登录验证码
* <p>{0}:手机号</p>
*/
public
static
final
String
USER_LOGIN_SMS_KEY
=
"cjxc:wx:user:sms:login:{0}"
;
/**
* 缓存用户修改密码验证码
* <p>{0}:手机号</p>
*/
public
static
final
String
USER_UPDATE_SMS_KEY
=
"cjxc:wx:user:sms:update:{0}"
;
/**
/**
* 短信验证码类型枚举
* 短信验证码类型枚举
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
View file @
3d65d66d
package
com.wwdz.ch.wx.impl
;
package
com.wwdz.ch.wx.impl
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.wwdz.ch.core.captcha.CaptchaCodeManager
;
import
com.wwdz.ch.core.consts.ResultCode
;
import
com.wwdz.ch.core.consts.ResultCode
;
import
com.wwdz.ch.core.notify.AliSmsSender
;
import
com.wwdz.ch.core.notify.AliSmsSender
;
import
com.wwdz.ch.core.notify.NotifyService
;
import
com.wwdz.ch.core.notify.NotifyService
;
...
@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID;
...
@@ -14,16 +13,17 @@ import com.wwdz.ch.core.util.UUID;
import
com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder
;
import
com.wwdz.ch.core.util.bcrypt.BCryptPasswordEncoder
;
import
com.wwdz.ch.db.dao.UserDao
;
import
com.wwdz.ch.db.dao.UserDao
;
import
com.wwdz.ch.db.domain.User
;
import
com.wwdz.ch.db.domain.User
;
import
com.wwdz.ch.db.dto.request.FavoritesRequestDto
;
import
com.wwdz.ch.wx.constant.CacheCodeConstants
;
import
com.wwdz.ch.wx.constant.CacheCodeConstants
;
import
com.wwdz.ch.wx.dao.UserInfo
;
import
com.wwdz.ch.wx.dao.UserInfo
;
import
com.wwdz.ch.wx.dao.UserToken
;
import
com.wwdz.ch.wx.dao.UserToken
;
import
com.wwdz.ch.db.dto.request.FavoritesRequestDto
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
import
com.wwdz.ch.wx.manager.UserTokenManager
;
import
com.wwdz.ch.wx.service.FavoritesService
;
import
com.wwdz.ch.wx.service.FavoritesService
;
import
com.wwdz.ch.wx.service.UserService
;
import
com.wwdz.ch.wx.service.UserService
;
import
com.wwdz.ch.wx.manager.UserTokenManager
;
import
com.wwdz.ch.wx.service.WxLoginService
;
import
com.wwdz.ch.wx.service.WxLoginService
;
import
com.wwdz.ch.wx.util.IpUtil
;
import
com.wwdz.ch.wx.util.IpUtil
;
import
com.wwdz.ch.wx.util.RedisUtil
;
import
com.wwdz.ch.wx.util.StringUtil
;
import
com.wwdz.ch.wx.util.StringUtil
;
import
com.wwdz.ch.wx.util.WxResponseCode
;
import
com.wwdz.ch.wx.util.WxResponseCode
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
...
@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
...
@@ -37,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
import
org.springframework.transaction.interceptor.TransactionAspectSupport
;
import
org.springframework.transaction.interceptor.TransactionAspectSupport
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.text.MessageFormat
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService {
...
@@ -58,6 +59,8 @@ public class UserServiceImpl implements UserService {
private
FavoritesService
favoritesService
;
private
FavoritesService
favoritesService
;
@Autowired
@Autowired
private
WxLoginService
wxLoginService
;
private
WxLoginService
wxLoginService
;
@Autowired
private
RedisUtil
redisUtil
;
@Value
(
"${avatar-url}"
)
@Value
(
"${avatar-url}"
)
private
String
avatarUrl
;
private
String
avatarUrl
;
...
@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService {
...
@@ -181,6 +184,16 @@ public class UserServiceImpl implements UserService {
logger
.
error
(
"请求验证码出错:{}"
,
WxResponseCode
.
AUTH_CAPTCHA_UNSUPPORT
.
desc
());
logger
.
error
(
"请求验证码出错:{}"
,
WxResponseCode
.
AUTH_CAPTCHA_UNSUPPORT
.
desc
());
return
Result
.
failed
(
WxResponseCode
.
AUTH_CAPTCHA_UNSUPPORT
.
desc
());
return
Result
.
failed
(
WxResponseCode
.
AUTH_CAPTCHA_UNSUPPORT
.
desc
());
}
}
String
key
=
""
;
if
(
dto
.
getSmsType
().
equals
(
CacheCodeConstants
.
SmsTypeEnum
.
LOGIN
.
getCode
()))
{
key
=
MessageFormat
.
format
(
CacheCodeConstants
.
USER_LOGIN_SMS_KEY
,
dto
.
getMobile
());
}
else
{
key
=
MessageFormat
.
format
(
CacheCodeConstants
.
USER_UPDATE_SMS_KEY
,
dto
.
getMobile
());
}
if
(
redisUtil
.
hasKey
(
key
))
{
return
Result
.
failed
(
"验证码请求过于频繁"
);
}
String
activeProfile
=
applicationContext
.
getEnvironment
().
getActiveProfiles
()[
0
];
String
activeProfile
=
applicationContext
.
getEnvironment
().
getActiveProfiles
()[
0
];
String
code
=
"123456"
;
String
code
=
"123456"
;
if
(!
StringUtils
.
equals
(
"dev"
,
activeProfile
))
{
if
(!
StringUtils
.
equals
(
"dev"
,
activeProfile
))
{
...
@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService {
...
@@ -190,16 +203,11 @@ public class UserServiceImpl implements UserService {
code
=
CharUtil
.
getRandomNum
(
6
);
code
=
CharUtil
.
getRandomNum
(
6
);
SmsResult
smsResult
=
aliSmsSender
.
sendWithTemplate
(
dto
.
getMobile
(),
code
);
SmsResult
smsResult
=
aliSmsSender
.
sendWithTemplate
(
dto
.
getMobile
(),
code
);
if
(
smsResult
!=
null
)
{
if
(
smsResult
!=
null
)
{
logger
.
info
(
"*****
腾讯
云短信发送->请求验证码,短信发送结果:{}"
,
JSONObject
.
toJSONString
(
smsResult
));
logger
.
info
(
"*****
阿里
云短信发送->请求验证码,短信发送结果:{}"
,
JSONObject
.
toJSONString
(
smsResult
));
}
}
}
}
boolean
successful
=
CaptchaCodeManager
.
addToCache
(
dto
.
getSmsType
()
+
dto
.
getMobile
(),
code
,
1
);
redisUtil
.
set
(
key
,
code
,
60
);
if
(!
successful
)
{
logger
.
error
(
"请求验证码出错:{}"
,
WxResponseCode
.
AUTH_CAPTCHA_FREQUENCY
.
desc
());
return
Result
.
failed
(
WxResponseCode
.
AUTH_CAPTCHA_FREQUENCY
.
desc
());
}
logger
.
info
(
"【请求结束】请求验证码成功"
);
logger
.
info
(
"【请求结束】请求验证码成功"
);
return
Result
.
success
();
return
Result
.
success
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService {
...
@@ -212,11 +220,12 @@ public class UserServiceImpl implements UserService {
@Override
@Override
public
Result
loginByMobile
(
UserRequestDto
dto
,
HttpServletRequest
request
)
{
public
Result
loginByMobile
(
UserRequestDto
dto
,
HttpServletRequest
request
)
{
try
{
try
{
String
mobileCode
=
CaptchaCodeManager
.
getCachedCaptcha
(
CacheCodeConstants
.
SmsTypeEnum
.
LOGIN
.
getCode
()
+
dto
.
getMobile
());
String
key
=
MessageFormat
.
format
(
CacheCodeConstants
.
USER_LOGIN_SMS_KEY
,
dto
.
getMobile
());
if
(
StringUtils
.
isBlank
(
mobileCode
))
{
Object
object
=
redisUtil
.
get
(
key
);
if
(
Objects
.
isNull
(
object
))
{
return
Result
.
failed
(
"验证码已过期"
);
return
Result
.
failed
(
"验证码已过期"
);
}
}
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
object
.
toString
()
))
{
return
Result
.
failed
(
"验证码不正确"
);
return
Result
.
failed
(
"验证码不正确"
);
}
}
...
@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService {
...
@@ -294,6 +303,8 @@ public class UserServiceImpl implements UserService {
result
.
put
(
"userInfo"
,
userInfo
);
result
.
put
(
"userInfo"
,
userInfo
);
logger
.
info
(
"【请求结束】手机号登录,响应结果:{}"
,
JSONObject
.
toJSONString
(
result
));
logger
.
info
(
"【请求结束】手机号登录,响应结果:{}"
,
JSONObject
.
toJSONString
(
result
));
// 清除验证码缓存
redisUtil
.
del
(
key
);
return
Result
.
success
(
result
);
return
Result
.
success
(
result
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
error
(
"手机号登录失败"
,
e
);
logger
.
error
(
"手机号登录失败"
,
e
);
...
@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService {
...
@@ -392,11 +403,12 @@ public class UserServiceImpl implements UserService {
if
(!
StringUtils
.
equals
(
user
.
getMobile
(),
dto
.
getBeforeMobile
()))
{
if
(!
StringUtils
.
equals
(
user
.
getMobile
(),
dto
.
getBeforeMobile
()))
{
return
Result
.
failed
(
"原手机号码不正确"
);
return
Result
.
failed
(
"原手机号码不正确"
);
}
}
String
mobileCode
=
CaptchaCodeManager
.
getCachedCaptcha
(
CacheCodeConstants
.
SmsTypeEnum
.
UPDATE
.
getCode
()
+
dto
.
getAfterMobile
());
String
key
=
MessageFormat
.
format
(
CacheCodeConstants
.
USER_UPDATE_SMS_KEY
,
dto
.
getAfterMobile
());
if
(
StringUtils
.
isBlank
(
mobileCode
))
{
Object
object
=
redisUtil
.
get
(
key
);
if
(
Objects
.
isNull
(
object
))
{
return
Result
.
failed
(
"验证码已过期"
);
return
Result
.
failed
(
"验证码已过期"
);
}
}
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
object
.
toString
()
))
{
return
Result
.
failed
(
"验证码不正确"
);
return
Result
.
failed
(
"验证码不正确"
);
}
}
if
(
userDao
.
isExistedByMobile
(
dto
.
getBeforeMobile
(),
dto
.
getAfterMobile
()))
{
if
(
userDao
.
isExistedByMobile
(
dto
.
getBeforeMobile
(),
dto
.
getAfterMobile
()))
{
...
@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService {
...
@@ -405,6 +417,8 @@ public class UserServiceImpl implements UserService {
user
.
setMobile
(
dto
.
getAfterMobile
());
user
.
setMobile
(
dto
.
getAfterMobile
());
userDao
.
updateById
(
user
);
userDao
.
updateById
(
user
);
logger
.
info
(
"【请求结束】用户修改手机号成功"
);
logger
.
info
(
"【请求结束】用户修改手机号成功"
);
// 清除验证码缓存
redisUtil
.
del
(
key
);
return
Result
.
success
();
return
Result
.
success
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
error
(
"用户修改手机号失败"
,
e
);
logger
.
error
(
"用户修改手机号失败"
,
e
);
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/manager/UserTokenManager.java
View file @
3d65d66d
...
@@ -24,9 +24,9 @@ public class UserTokenManager {
...
@@ -24,9 +24,9 @@ public class UserTokenManager {
private
Gson
gson
=
new
Gson
();
private
Gson
gson
=
new
Gson
();
private
static
final
String
TOKEN_PREFIX
=
"token_prefix:"
;
private
static
final
String
TOKEN_PREFIX
=
"
cjxc:wx:token:
token_prefix:"
;
private
static
final
String
TOKEN_ID_PREFIX
=
"token_id_prefix:"
;
private
static
final
String
TOKEN_ID_PREFIX
=
"
cjxc:wx:token:
token_id_prefix:"
;
private
static
Map
<
String
,
UserToken
>
tokenMap
=
new
HashMap
<>();
private
static
Map
<
String
,
UserToken
>
tokenMap
=
new
HashMap
<>();
private
static
Map
<
Long
,
UserToken
>
idMap
=
new
HashMap
<>();
private
static
Map
<
Long
,
UserToken
>
idMap
=
new
HashMap
<>();
private
static
long
SURVIVAL_TIME
=
86400
;
private
static
long
SURVIVAL_TIME
=
86400
;
...
...
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