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
b25af5cf
Commit
b25af5cf
authored
Aug 10, 2023
by
muhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改 小程序登录接口
parent
4a8f66da
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
119 additions
and
40 deletions
+119
-40
ch-wx-api/src/main/java/com/wwdz/ch/wx/constant/CacheCodeConstants.java
...main/java/com/wwdz/ch/wx/constant/CacheCodeConstants.java
+9
-1
ch-wx-api/src/main/java/com/wwdz/ch/wx/entity/request/UserRequestDto.java
...in/java/com/wwdz/ch/wx/entity/request/UserRequestDto.java
+1
-0
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
...pi/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
+10
-4
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/WxLoginServiceImpl.java
...src/main/java/com/wwdz/ch/wx/impl/WxLoginServiceImpl.java
+81
-31
ch-wx-api/src/main/java/com/wwdz/ch/wx/service/WxLoginService.java
.../src/main/java/com/wwdz/ch/wx/service/WxLoginService.java
+3
-1
ch-wx-api/src/main/java/com/wwdz/ch/wx/web/WxUserController.java
...pi/src/main/java/com/wwdz/ch/wx/web/WxUserController.java
+15
-3
No files found.
ch-wx-api/src/main/java/com/wwdz/ch/wx/constant/CacheCode
Enum
.java
→
ch-wx-api/src/main/java/com/wwdz/ch/wx/constant/CacheCode
Constants
.java
View file @
b25af5cf
...
@@ -3,7 +3,15 @@ package com.wwdz.ch.wx.constant;
...
@@ -3,7 +3,15 @@ package com.wwdz.ch.wx.constant;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
public
class
CacheCodeEnum
{
public
class
CacheCodeConstants
{
/**
* 微信接口调用凭证access_token
*/
public
static
final
String
WX_ACCESS_TOKEN
=
"quanku:wx:access_token"
;
/**
* 短信验证码类型枚举
*/
public
enum
SmsTypeEnum
{
public
enum
SmsTypeEnum
{
LOGIN
(
"LOGIN:"
,
"登录"
),
LOGIN
(
"LOGIN:"
,
"登录"
),
UPDATE
(
"UPDATE:"
,
"修改手机号"
),
UPDATE
(
"UPDATE:"
,
"修改手机号"
),
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/entity/request/UserRequestDto.java
View file @
b25af5cf
...
@@ -20,4 +20,5 @@ public class UserRequestDto implements Entity {
...
@@ -20,4 +20,5 @@ public class UserRequestDto implements Entity {
private
String
smsType
;
private
String
smsType
;
private
String
encryptedData
;
private
String
encryptedData
;
private
String
iv
;
private
String
iv
;
private
Integer
type
;
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/UserServiceImpl.java
View file @
b25af5cf
...
@@ -13,7 +13,7 @@ import com.wwdz.ch.core.util.UUID;
...
@@ -13,7 +13,7 @@ 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.wx.constant.CacheCode
Enum
;
import
com.wwdz.ch.wx.constant.CacheCode
Constants
;
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.wx.entity.request.FavoritesRequestDto
;
import
com.wwdz.ch.wx.entity.request.FavoritesRequestDto
;
...
@@ -68,10 +68,16 @@ public class UserServiceImpl implements UserService {
...
@@ -68,10 +68,16 @@ public class UserServiceImpl implements UserService {
try
{
try
{
String
mobile
=
""
;
String
mobile
=
""
;
try
{
try
{
mobile
=
wxLoginService
.
getPhone
(
dto
.
getCode
()
);
mobile
=
wxLoginService
.
getPhone
(
dto
);
if
(
StringUtils
.
equals
(
"false"
,
mobile
))
{
if
(
StringUtils
.
equals
(
"false"
,
mobile
))
{
return
Result
.
failed
(
"获取access_token失败"
);
return
Result
.
failed
(
"获取access_token失败"
);
}
}
if
(
StringUtils
.
equals
(
"sessionFalse"
,
mobile
))
{
return
Result
.
failed
(
"获取sessionKey失败"
);
}
if
(
StringUtils
.
equals
(
"phoneFalse"
,
mobile
))
{
return
Result
.
failed
(
"获取手机号失败"
);
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取微信用户手机号失败"
,
e
);
logger
.
error
(
"获取微信用户手机号失败"
,
e
);
return
Result
.
failed
(
"获取微信用户手机号失败"
);
return
Result
.
failed
(
"获取微信用户手机号失败"
);
...
@@ -197,7 +203,7 @@ public class UserServiceImpl implements UserService {
...
@@ -197,7 +203,7 @@ 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
(
CacheCode
Enum
.
SmsTypeEnum
.
LOGIN
.
getCode
()
+
dto
.
getMobile
());
String
mobileCode
=
CaptchaCodeManager
.
getCachedCaptcha
(
CacheCode
Constants
.
SmsTypeEnum
.
LOGIN
.
getCode
()
+
dto
.
getMobile
());
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
return
Result
.
failed
(
"验证码不正确"
);
return
Result
.
failed
(
"验证码不正确"
);
}
}
...
@@ -371,7 +377,7 @@ public class UserServiceImpl implements UserService {
...
@@ -371,7 +377,7 @@ 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
(
CacheCode
Enum
.
SmsTypeEnum
.
UPDATE
.
getCode
()
+
dto
.
getAfterMobile
());
String
mobileCode
=
CaptchaCodeManager
.
getCachedCaptcha
(
CacheCode
Constants
.
SmsTypeEnum
.
UPDATE
.
getCode
()
+
dto
.
getAfterMobile
());
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
if
(!
StringUtils
.
equals
(
dto
.
getSmsCode
(),
mobileCode
))
{
return
Result
.
failed
(
"验证码不正确"
);
return
Result
.
failed
(
"验证码不正确"
);
}
}
...
...
ch-wx-api/src/main/java/com/wwdz/ch/wx/impl/WxLoginServiceImpl.java
View file @
b25af5cf
...
@@ -2,13 +2,19 @@ package com.wwdz.ch.wx.impl;
...
@@ -2,13 +2,19 @@ package com.wwdz.ch.wx.impl;
import
cn.binarywang.wx.miniapp.api.WxMaService
;
import
cn.binarywang.wx.miniapp.api.WxMaService
;
import
cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl
;
import
cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl
;
import
com.wwdz.ch.core.util.HttpUtil
;
import
cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult
;
import
cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo
;
import
com.alibaba.fastjson.JSON
;
import
com.wwdz.ch.core.util.OkHttpUtil
;
import
com.wwdz.ch.wx.constant.CacheCodeConstants
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
import
com.wwdz.ch.wx.service.WxLoginService
;
import
com.wwdz.ch.wx.service.WxLoginService
;
import
com.wwdz.ch.wx.util.RedisUtil
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.JsonUtils
;
import
com.xxdxxs.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -18,42 +24,86 @@ import java.util.Map;
...
@@ -18,42 +24,86 @@ import java.util.Map;
public
class
WxLoginServiceImpl
implements
WxLoginService
{
public
class
WxLoginServiceImpl
implements
WxLoginService
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WxMaServiceImpl
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WxMaServiceImpl
.
class
);
private
static
final
String
GET_ACCESS_TOKEN_URL
=
"https://api.weixin.qq.com/cgi-bin/stable_token"
;
private
static
final
String
appid
=
"wx5b4409448bf2c72b"
;
private
static
final
String
GET_PHONE_URL
=
"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
;
private
static
final
String
secret
=
"6fee0d7b2197845fed0a3fe8f5a9bd66"
;
@Value
(
"${dts.wx.app-id}"
)
// private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token";
private
String
appid
;
@Value
(
"${dts.wx.app-secret}"
)
private
static
final
String
GET_ACCESS_TOKEN_URL
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
;
private
String
secret
;
private
static
final
String
GET_PHONE_URL
=
"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
;
@Autowired
@Autowired
private
WxMaService
wxMaService
;
private
WxMaService
wxMaService
;
@Autowired
private
RedisUtil
redisUtil
;
@Override
@Override
public
String
getPhone
(
String
code
)
{
public
String
getPhone
(
UserRequestDto
dto
)
{
logger
.
info
(
"开始获取access_token----------------"
);
String
phone
=
""
;
// Map<String, String> tokenParam = new HashMap<>();
if
(
dto
.
getType
()
==
1
)
{
// tokenParam.put("grant_type", "client_credential");
logger
.
info
(
"开始获取access_token----------------"
);
// tokenParam.put("appid", "wx3618eab6c8b144af");
String
accessToken
=
""
;
// tokenParam.put("secret", "6fee0d7b2197845fed0a3fe8f5a9bd66");
if
(
redisUtil
.
hasKey
(
CacheCodeConstants
.
WX_ACCESS_TOKEN
))
{
// String tokenResult = HttpUtil.sendPost(GET_ACCESS_TOKEN_URL, tokenParam);
accessToken
=
redisUtil
.
get
(
CacheCodeConstants
.
WX_ACCESS_TOKEN
).
toString
();
// logger.info("获取access_token的结果是:" + tokenResult);
}
else
{
// String accessToken = JsonUtils.getValueByPath(tokenResult, "access_token");
try
{
String
accessToken
=
""
;
String
accessTokenUrl
=
String
.
format
(
GET_ACCESS_TOKEN_URL
,
appid
,
secret
);
try
{
String
res
=
OkHttpUtil
.
builder
().
url
(
accessTokenUrl
)
accessToken
=
wxMaService
.
getAccessToken
();
.
get
().
sync
();
}
catch
(
Exception
e
)
{
logger
.
info
(
"获取access_token结果:"
+
res
);
logger
.
error
(
"获取accessToken出错"
);
if
(!
res
.
contains
(
"access_token"
))
{
return
"false"
;
logger
.
error
(
"获取accessToken出错,返回结果是:"
+
res
);
}
return
"false"
;
}
accessToken
=
JsonUtils
.
getValueByPath
(
res
,
"access_token"
);
String
expiresIn
=
JsonUtils
.
getValueByPath
(
res
,
"expires_in"
);
redisUtil
.
set
(
CacheCodeConstants
.
WX_ACCESS_TOKEN
,
accessToken
,
Long
.
parseLong
(
expiresIn
));
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取accessToken出错"
,
e
);
return
"false"
;
}
}
logger
.
info
(
"开始获取phone-----------------------"
);
logger
.
info
(
"开始获取phone-----------------------"
);
String
url
=
String
.
format
(
GET_PHONE_URL
,
accessToken
);
String
url
=
String
.
format
(
GET_PHONE_URL
,
accessToken
);
Map
<
String
,
String
>
phoneParam
=
new
HashMap
<>();
Map
<
String
,
Object
>
phoneParam
=
new
HashMap
<>();
phoneParam
.
put
(
"code"
,
code
);
phoneParam
.
put
(
"code"
,
dto
.
getCode
());
String
phoneResult
=
HttpUtil
.
sendPost
(
url
,
phoneParam
);
String
phoneResult
=
OkHttpUtil
.
builder
().
url
(
url
)
logger
.
info
(
"获取phone的结果是:"
+
phoneResult
);
.
addParams
(
phoneParam
)
return
JsonUtils
.
getValueByPath
(
phoneResult
,
"phone_info/phoneNumber"
);
.
post
(
true
).
sync
();
logger
.
info
(
"获取phone的结果是:"
+
phoneResult
);
if
(!
phoneResult
.
contains
(
"phoneNumber"
))
{
logger
.
error
(
"获取手机号出错,返回结果是:"
+
phoneResult
);
return
"phoneFalse"
;
}
}
else
{
logger
.
info
(
"开始获取sessionKey-----------------------"
);
String
sessionKey
=
""
;
try
{
WxMaJscode2SessionResult
result
=
this
.
wxMaService
.
getUserService
().
getSessionInfo
(
dto
.
getCode
());
sessionKey
=
result
.
getSessionKey
();
logger
.
info
(
"获取sessionKey结果:"
+
JSON
.
toJSONString
(
result
));
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取用户登录信息失败"
,
e
);
return
"sessionFalse"
;
}
if
(
StringUtils
.
isEmpty
(
sessionKey
))
{
logger
.
error
(
"微信登录,调用官方接口失败:{}"
,
dto
.
getCode
());
return
"sessionFalse"
;
}
logger
.
info
(
"解析手机号加密信息-----------------------"
);
try
{
WxMaPhoneNumberInfo
wxMaPhoneNumberInfo
=
this
.
wxMaService
.
getUserService
().
getPhoneNoInfo
(
sessionKey
,
dto
.
getEncryptedData
(),
dto
.
getIv
());
logger
.
info
(
"解析手机号加密信息结果:"
+
JSON
.
toJSONString
(
wxMaPhoneNumberInfo
));
phone
=
wxMaPhoneNumberInfo
.
getPhoneNumber
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"解析手机号加密信息失败"
,
e
);
return
"phoneFalse"
;
}
}
return
phone
;
}
}
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/service/WxLoginService.java
View file @
b25af5cf
package
com.wwdz.ch.wx.service
;
package
com.wwdz.ch.wx.service
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
public
interface
WxLoginService
{
public
interface
WxLoginService
{
String
getPhone
(
String
code
);
String
getPhone
(
UserRequestDto
dto
);
}
}
ch-wx-api/src/main/java/com/wwdz/ch/wx/web/WxUserController.java
View file @
b25af5cf
...
@@ -7,7 +7,7 @@ import com.wwdz.ch.core.util.ResponseUtil;
...
@@ -7,7 +7,7 @@ import com.wwdz.ch.core.util.ResponseUtil;
import
com.wwdz.ch.db.domain.UserAccountObsolete
;
import
com.wwdz.ch.db.domain.UserAccountObsolete
;
import
com.wwdz.ch.db.service.DtsAccountService
;
import
com.wwdz.ch.db.service.DtsAccountService
;
import
com.wwdz.ch.wx.annotation.LoginUser
;
import
com.wwdz.ch.wx.annotation.LoginUser
;
import
com.wwdz.ch.wx.constant.CacheCode
Enum
;
import
com.wwdz.ch.wx.constant.CacheCode
Constants
;
import
com.wwdz.ch.wx.entity.request.KlDataRequestDto
;
import
com.wwdz.ch.wx.entity.request.KlDataRequestDto
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
import
com.wwdz.ch.wx.entity.request.UserRequestDto
;
import
com.wwdz.ch.wx.service.UserService
;
import
com.wwdz.ch.wx.service.UserService
;
...
@@ -99,9 +99,21 @@ public class WxUserController {
...
@@ -99,9 +99,21 @@ public class WxUserController {
if
(
StringUtils
.
isBlank
(
code
))
{
if
(
StringUtils
.
isBlank
(
code
))
{
return
Result
.
failed
(
"授权码不能为空"
);
return
Result
.
failed
(
"授权码不能为空"
);
}
}
if
(!
json
.
contains
(
"type"
))
{
return
Result
.
failed
(
"版本类型不能为空"
);
}
Integer
type
=
Integer
.
parseInt
(
JsonUtils
.
getValueByPath
(
json
,
"type"
));
UserRequestDto
dto
=
new
UserRequestDto
();
UserRequestDto
dto
=
new
UserRequestDto
();
dto
.
setCode
(
code
);
dto
.
setCode
(
code
);
dto
.
setType
(
type
);
if
(
type
==
2
)
{
if
(!
json
.
contains
(
"encryptedData"
)
||
!
json
.
contains
(
"iv"
))
{
return
Result
.
failed
(
"encryptedData或iv不能为空"
);
}
dto
.
setEncryptedData
(
JsonUtils
.
getValueByPath
(
json
,
"encryptedData"
));
dto
.
setIv
(
JsonUtils
.
getValueByPath
(
json
,
"iv"
));
}
return
userService
.
loginByWx
(
dto
,
request
);
return
userService
.
loginByWx
(
dto
,
request
);
}
}
...
@@ -117,7 +129,7 @@ public class WxUserController {
...
@@ -117,7 +129,7 @@ public class WxUserController {
UserRequestDto
dto
=
new
UserRequestDto
();
UserRequestDto
dto
=
new
UserRequestDto
();
dto
.
setMobile
(
mobile
);
dto
.
setMobile
(
mobile
);
dto
.
setSmsType
(
CacheCode
Enum
.
SmsTypeEnum
.
LOGIN
.
getCode
());
dto
.
setSmsType
(
CacheCode
Constants
.
SmsTypeEnum
.
LOGIN
.
getCode
());
return
userService
.
regCaptcha
(
dto
);
return
userService
.
regCaptcha
(
dto
);
}
}
...
@@ -233,7 +245,7 @@ public class WxUserController {
...
@@ -233,7 +245,7 @@ public class WxUserController {
UserRequestDto
dto
=
new
UserRequestDto
();
UserRequestDto
dto
=
new
UserRequestDto
();
dto
.
setMobile
(
mobile
);
dto
.
setMobile
(
mobile
);
dto
.
setSmsType
(
CacheCode
Enum
.
SmsTypeEnum
.
UPDATE
.
getCode
());
dto
.
setSmsType
(
CacheCode
Constants
.
SmsTypeEnum
.
UPDATE
.
getCode
());
return
userService
.
regCaptcha
(
dto
);
return
userService
.
regCaptcha
(
dto
);
}
}
...
...
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