Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
ne-shield-miniappserver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 80981577
authored
Jun 30, 2022
by
levphon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
微信小程序C端账号体系
1 parent
3ce30ad0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
88 additions
and
65 deletions
pom.xml
src/main/java/cn/com/glsx/app/config/WebConfig.java
src/main/java/cn/com/glsx/app/modules/ThirdLoginController.java
src/main/java/cn/com/glsx/app/modules/converter/UserConverter.java
src/main/java/cn/com/glsx/app/modules/model/AppUser.java
src/main/java/cn/com/glsx/app/modules/service/LoginService.java
pom.xml
View file @
8098157
...
@@ -41,14 +41,14 @@
...
@@ -41,14 +41,14 @@
<dependency>
<dependency>
<groupId>
com.glsx
</groupId>
<groupId>
com.glsx
</groupId>
<artifactId>
ne-shield-auth-client
</artifactId>
<artifactId>
ne-shield-auth-client
</artifactId>
<version>
1.
1
.0-SNAPSHOT
</version>
<version>
1.
2
.0-SNAPSHOT
</version>
<scope>
compile
</scope>
<scope>
compile
</scope>
</dependency>
</dependency>
<!-- devicemonitor-api -->
<!-- devicemonitor-api -->
<dependency>
<dependency>
<groupId>
com.glsx
</groupId>
<groupId>
com.glsx
</groupId>
<artifactId>
ne-shield-devicemonitor-api
</artifactId>
<artifactId>
ne-shield-devicemonitor-api
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<version>
1.
2.
0-SNAPSHOT
</version>
<scope>
compile
</scope>
<scope>
compile
</scope>
</dependency>
</dependency>
</dependencies>
</dependencies>
...
...
src/main/java/cn/com/glsx/app/config/WebConfig.java
View file @
8098157
package
cn
.
com
.
glsx
.
app
.
config
;
package
cn
.
com
.
glsx
.
app
.
config
;
import
cn.com.glsx.auth.interceptor.
Web
RequestAuthorizeInterceptor
;
import
cn.com.glsx.auth.interceptor.
App
RequestAuthorizeInterceptor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
...
@@ -14,7 +14,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
...
@@ -14,7 +14,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public
class
WebConfig
implements
WebMvcConfigurer
{
public
class
WebConfig
implements
WebMvcConfigurer
{
@Autowired
@Autowired
private
Web
RequestAuthorizeInterceptor
authInterceptor
;
private
App
RequestAuthorizeInterceptor
authInterceptor
;
@Override
@Override
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
public
void
addResourceHandlers
(
ResourceHandlerRegistry
registry
)
{
...
...
src/main/java/cn/com/glsx/app/modules/ThirdLoginController.java
View file @
8098157
...
@@ -65,10 +65,13 @@ public class ThirdLoginController {
...
@@ -65,10 +65,13 @@ public class ThirdLoginController {
@GetMapping
(
value
=
"/wechatAuth"
)
@GetMapping
(
value
=
"/wechatAuth"
)
public
R
wechatAuth
(
@RequestParam
String
code
,
@RequestParam
Integer
accessMode
)
throws
WxErrorException
{
public
R
wechatAuth
(
@RequestParam
String
code
,
@RequestParam
Integer
accessMode
)
throws
WxErrorException
{
//获取openId、unionid、session_key
//获取openId、unionid、session_key
WxMaJscode2SessionResult
sessionInfo
=
wxMaService
.
getUserService
().
getSessionInfo
(
code
);
WxMaJscode2SessionResult
sessionInfo
=
null
;
//wxMaService.getUserService().getSessionInfo(code);
sessionInfo
=
new
WxMaJscode2SessionResult
();
sessionInfo
.
setOpenid
(
"ocDtn5a60SvMC33CbObTUUMVGOwE"
);
log
.
info
(
"微信code[{}]获取的Session信息为{}"
,
code
,
JSONUtil
.
toJsonStr
(
sessionInfo
));
log
.
info
(
"微信code[{}]获取的Session信息为{}"
,
code
,
JSONUtil
.
toJsonStr
(
sessionInfo
));
UserAuthRel
authRel
=
loginService
.
wechatAuth
(
sessionInfo
);
UserAuthRel
authRel
=
loginService
.
wechatAuth
(
sessionInfo
,
accessMode
);
Map
<
String
,
Object
>
rtnMap
=
thirdLogin
(
LoginType
.
WECHAT
,
accessMode
,
authRel
);
Map
<
String
,
Object
>
rtnMap
=
thirdLogin
(
LoginType
.
WECHAT
,
accessMode
,
authRel
);
...
@@ -107,12 +110,6 @@ public class ThirdLoginController {
...
@@ -107,12 +110,6 @@ public class ThirdLoginController {
//a、存在账号的openid发起免登录
//a、存在账号的openid发起免登录
//b、不存在账号的openid发起免登录
//b、不存在账号的openid发起免登录
AppUser
user
=
userService
.
getAppUserByAuthId
(
loginType
,
authRel
.
getAuthId
());
AppUser
user
=
userService
.
getAppUserByAuthId
(
loginType
,
authRel
.
getAuthId
());
if
(
user
!=
null
)
{
}
else
{
user
=
new
AppUser
();
// TODO: 2022/6/29 设置用户标识
}
user
.
setUserType
(
MiniUserType
.
SHARED
.
getType
());
user
.
setUserType
(
MiniUserType
.
SHARED
.
getType
());
String
token
=
userService
.
createToken
(
user
);
String
token
=
userService
.
createToken
(
user
);
...
...
src/main/java/cn/com/glsx/app/modules/converter/UserConverter.java
View file @
8098157
package
cn
.
com
.
glsx
.
app
.
modules
.
converter
;
package
cn
.
com
.
glsx
.
app
.
modules
.
converter
;
import
cn.com.glsx.app.modules.entity.User
;
import
cn.com.glsx.app.modules.model.AppUser
;
import
cn.com.glsx.app.modules.model.AppUser
;
import
cn.com.glsx.auth.model.SimpleUser
;
import
cn.com.glsx.auth.model.SimpleUser
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.Mapping
;
import
org.mapstruct.Mappings
;
import
org.mapstruct.factory.Mappers
;
import
org.mapstruct.factory.Mappers
;
@Mapper
@Mapper
...
@@ -10,6 +13,10 @@ public interface UserConverter {
...
@@ -10,6 +13,10 @@ public interface UserConverter {
UserConverter
INSTANCE
=
Mappers
.
getMapper
(
UserConverter
.
class
);
UserConverter
INSTANCE
=
Mappers
.
getMapper
(
UserConverter
.
class
);
@Mappings
(
@Mapping
(
source
=
"id"
,
target
=
"userId"
))
AppUser
user2au
(
User
user
);
@Mappings
(
@Mapping
(
source
=
"id"
,
target
=
"userId"
))
AppUser
su2au
(
SimpleUser
user
);
AppUser
su2au
(
SimpleUser
user
);
}
}
src/main/java/cn/com/glsx/app/modules/model/AppUser.java
View file @
8098157
...
@@ -6,20 +6,14 @@ import lombok.Data;
...
@@ -6,20 +6,14 @@ import lombok.Data;
public
class
AppUser
{
public
class
AppUser
{
/**
/**
*
用户id(对应后台
t_user表id)
*
看具体用户类型(userType):normal用户id(对应后台t_user表id),C端被分享用户用户id(对应后台t_cus
t_user表id)
*/
*/
private
Long
userId
;
private
Long
userId
;
/**
* C端用户id(对应后台t_cust_user表id)
*/
private
Long
cuserId
;
private
String
account
;
private
String
account
;
private
String
username
;
private
String
username
;
private
String
avatar
;
private
String
avatar
;
/**
/**
* 用户类型:normal
正常
用户,shared被分享用户,见枚举MiniUserType
* 用户类型:normal
常规
用户,shared被分享用户,见枚举MiniUserType
*/
*/
private
String
userType
;
private
String
userType
;
private
Integer
blacklist
;
private
Integer
blacklist
;
...
...
src/main/java/cn/com/glsx/app/modules/service/LoginService.java
View file @
8098157
...
@@ -3,6 +3,7 @@ package cn.com.glsx.app.modules.service;
...
@@ -3,6 +3,7 @@ package cn.com.glsx.app.modules.service;
import
cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult
;
import
cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult
;
import
cn.com.glsx.app.common.enums.AuthType
;
import
cn.com.glsx.app.common.enums.AuthType
;
import
cn.com.glsx.app.common.enums.LoginType
;
import
cn.com.glsx.app.common.enums.LoginType
;
import
cn.com.glsx.app.common.enums.MiniUserType
;
import
cn.com.glsx.app.common.exception.AppServerException
;
import
cn.com.glsx.app.common.exception.AppServerException
;
import
cn.com.glsx.app.modules.converter.UserConverter
;
import
cn.com.glsx.app.modules.converter.UserConverter
;
import
cn.com.glsx.app.modules.entity.*
;
import
cn.com.glsx.app.modules.entity.*
;
...
@@ -13,6 +14,7 @@ import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
...
@@ -13,6 +14,7 @@ import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import
cn.com.glsx.auth.api.AuthFeignClient
;
import
cn.com.glsx.auth.api.AuthFeignClient
;
import
cn.com.glsx.auth.model.SimpleUser
;
import
cn.com.glsx.auth.model.SimpleUser
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.core.enums.SysConstants
;
import
com.glsx.plat.core.web.R
;
import
com.glsx.plat.core.web.R
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -66,50 +68,10 @@ public class LoginService {
...
@@ -66,50 +68,10 @@ public class LoginService {
throw
new
AppServerException
(
userR
.
getMessage
());
throw
new
AppServerException
(
userR
.
getMessage
());
}
}
User
user
=
Optional
.
ofNullable
(
userMapper
.
selectByAccount
(
simpleUser
.
getAccount
())).
orElse
(
new
User
());
User
user
=
register
(
simpleUser
);
user
.
setLinkerId
(
simpleUser
.
getId
());
user
.
setAccount
(
simpleUser
.
getAccount
());
user
.
setBlacklist
(
0
);
userMapper
.
saveOrUpdate
(
user
);
UserLocalAuth
localAuth
=
userLocalAuthMapper
.
selectByUserId
(
user
.
getId
());
if
(
localAuth
==
null
)
{
localAuth
=
new
UserLocalAuth
();
localAuth
.
setAccount
(
loginBO
.
getAccount
());
String
encryptedPasswd
=
bCryptPasswordEncoder
.
encode
(
loginBO
.
getAccount
());
localAuth
.
setPassword
(
encryptedPasswd
);
userLocalAuthMapper
.
insertUseGeneratedKeys
(
localAuth
);
}
UserAuthRel
userAuthRel
=
userAuthRelMapper
.
selectByAuthTypeAndAuthId
(
AuthType
.
LOCAL
.
getType
(),
localAuth
.
getId
());
if
(
userAuthRel
==
null
)
{
userAuthRel
=
new
UserAuthRel
();
userAuthRel
.
setUserId
(
user
.
getId
());
userAuthRel
.
setAuthId
(
localAuth
.
getId
());
userAuthRel
.
setAuthType
(
AuthType
.
LOCAL
.
getType
());
userAuthRelMapper
.
insertUseGeneratedKeys
(
userAuthRel
);
}
//生成本地账号
AppUser
appUser
=
UserConverter
.
INSTANCE
.
su2au
(
simpleUser
);
UserLocalAccount
localAccount
=
userLocalAccountMapper
.
selectByAuthId
(
user
.
getId
());
appUser
.
setUserType
(
MiniUserType
.
NORMAL
.
getType
());
if
(
localAccount
==
null
)
{
localAccount
=
new
UserLocalAccount
();
//localAccount.setUserId(user.getId());
localAccount
.
setAuthId
(
localAuth
.
getId
());
localAccount
.
setUsername
(
simpleUser
.
getUsername
());
//生成平台unionId
String
unionId
=
null
;
//generateUnionId();
localAccount
.
setUnionId
(
unionId
);
localAccount
.
setCreatedDate
(
new
Date
());
userLocalAccountMapper
.
insert
(
localAccount
);
}
else
{
if
(
StringUtils
.
isNotEmpty
(
localAccount
.
getUsername
())
&&
!
localAccount
.
getUsername
().
equals
(
simpleUser
.
getUsername
()))
{
localAccount
.
setUsername
(
simpleUser
.
getUsername
());
localAccount
.
setUpdatedDate
(
new
Date
());
userLocalAccountMapper
.
updateByPrimaryKeySelective
(
localAccount
);
}
}
return
UserConverter
.
INSTANCE
.
su2au
(
simpleUser
);
}
else
if
(
loginBO
.
getLoginType
()
==
2
)
{
}
else
if
(
loginBO
.
getLoginType
()
==
2
)
{
//验证码校验
//验证码校验
//smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
//smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
...
@@ -117,6 +79,58 @@ public class LoginService {
...
@@ -117,6 +79,58 @@ public class LoginService {
return
null
;
return
null
;
}
}
/**
* c端用户账号注册
*
* @param simpleUser
*/
public
User
register
(
SimpleUser
simpleUser
)
{
User
user
=
Optional
.
ofNullable
(
userMapper
.
selectByAccount
(
simpleUser
.
getAccount
())).
orElse
(
new
User
());
user
.
setLinkerId
(
simpleUser
.
getId
());
user
.
setAccount
(
simpleUser
.
getAccount
());
user
.
setBlacklist
(
simpleUser
.
getBlacklist
());
userMapper
.
saveOrUpdate
(
user
);
UserLocalAuth
localAuth
=
userLocalAuthMapper
.
selectByUserId
(
user
.
getId
());
if
(
localAuth
==
null
)
{
localAuth
=
new
UserLocalAuth
();
localAuth
.
setAccount
(
user
.
getAccount
());
String
encryptedPasswd
=
bCryptPasswordEncoder
.
encode
(
user
.
getAccount
());
localAuth
.
setPassword
(
encryptedPasswd
);
userLocalAuthMapper
.
insertUseGeneratedKeys
(
localAuth
);
}
UserAuthRel
userAuthRel
=
userAuthRelMapper
.
selectByAuthTypeAndAuthId
(
AuthType
.
LOCAL
.
getType
(),
localAuth
.
getId
());
if
(
userAuthRel
==
null
)
{
userAuthRel
=
new
UserAuthRel
();
userAuthRel
.
setUserId
(
user
.
getId
());
userAuthRel
.
setAuthId
(
localAuth
.
getId
());
userAuthRel
.
setAuthType
(
AuthType
.
LOCAL
.
getType
());
userAuthRelMapper
.
insertUseGeneratedKeys
(
userAuthRel
);
}
//生成本地账号
UserLocalAccount
localAccount
=
userLocalAccountMapper
.
selectByAuthId
(
user
.
getId
());
if
(
localAccount
==
null
)
{
localAccount
=
new
UserLocalAccount
();
//localAccount.setUserId(user.getId());
localAccount
.
setAuthId
(
localAuth
.
getId
());
localAccount
.
setUsername
(
simpleUser
.
getUsername
());
//生成平台unionId
String
unionId
=
null
;
//generateUnionId();
localAccount
.
setUnionId
(
unionId
);
localAccount
.
setCreatedDate
(
new
Date
());
userLocalAccountMapper
.
insert
(
localAccount
);
}
else
{
if
(
StringUtils
.
isNotEmpty
(
localAccount
.
getUsername
())
&&
!
localAccount
.
getUsername
().
equals
(
simpleUser
.
getUsername
()))
{
localAccount
.
setUsername
(
simpleUser
.
getUsername
());
localAccount
.
setUpdatedDate
(
new
Date
());
userLocalAccountMapper
.
updateByPrimaryKeySelective
(
localAccount
);
}
}
return
user
;
}
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
UserAuthRel
qqAuth
(
QQOAuth2AccessToken
accessToken
)
{
public
UserAuthRel
qqAuth
(
QQOAuth2AccessToken
accessToken
)
{
UserThirdAuth
thirdAuth
=
userThirdAuthMapper
.
selectByOpenid
(
accessToken
.
getOpenId
());
UserThirdAuth
thirdAuth
=
userThirdAuthMapper
.
selectByOpenid
(
accessToken
.
getOpenId
());
...
@@ -147,7 +161,7 @@ public class LoginService {
...
@@ -147,7 +161,7 @@ public class LoginService {
}
}
@Transactional
(
rollbackFor
=
Exception
.
class
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
UserAuthRel
wechatAuth
(
WxMaJscode2SessionResult
sessionInfo
)
{
public
UserAuthRel
wechatAuth
(
WxMaJscode2SessionResult
sessionInfo
,
Integer
accessMode
)
{
UserThirdAuth
thirdAuth
=
userThirdAuthMapper
.
selectByOpenid
(
sessionInfo
.
getOpenid
());
UserThirdAuth
thirdAuth
=
userThirdAuthMapper
.
selectByOpenid
(
sessionInfo
.
getOpenid
());
if
(
thirdAuth
==
null
)
{
if
(
thirdAuth
==
null
)
{
thirdAuth
=
new
UserThirdAuth
();
thirdAuth
=
new
UserThirdAuth
();
...
@@ -157,10 +171,21 @@ public class LoginService {
...
@@ -157,10 +171,21 @@ public class LoginService {
userThirdAuthMapper
.
insertUseGeneratedKeys
(
thirdAuth
);
userThirdAuthMapper
.
insertUseGeneratedKeys
(
thirdAuth
);
}
}
User
user
=
null
;
if
(
MiniUserType
.
SHARED
.
getFlag
().
equals
(
accessMode
))
{
SimpleUser
simpleUser
=
new
SimpleUser
();
// simpleUser.setId();
// simpleUser.setAccount();
// simpleUser.setUsername();
simpleUser
.
setBlacklist
(
SysConstants
.
EnableStatus
.
enable
.
getCode
());
user
=
register
(
simpleUser
);
}
UserAuthRel
authRel
=
userAuthRelMapper
.
selectByAuthTypeAndAuthId
(
AuthType
.
THIRD
.
getType
(),
thirdAuth
.
getId
());
UserAuthRel
authRel
=
userAuthRelMapper
.
selectByAuthTypeAndAuthId
(
AuthType
.
THIRD
.
getType
(),
thirdAuth
.
getId
());
if
(
authRel
==
null
)
{
if
(
authRel
==
null
)
{
authRel
=
new
UserAuthRel
();
authRel
=
new
UserAuthRel
();
//authRel.setUserId(
null);
authRel
.
setUserId
(
user
!=
null
?
user
.
getId
()
:
null
);
authRel
.
setAuthId
(
thirdAuth
.
getId
());
authRel
.
setAuthId
(
thirdAuth
.
getId
());
authRel
.
setAuthType
(
AuthType
.
THIRD
.
getType
());
authRel
.
setAuthType
(
AuthType
.
THIRD
.
getType
());
userAuthRelMapper
.
insertUseGeneratedKeys
(
authRel
);
userAuthRelMapper
.
insertUseGeneratedKeys
(
authRel
);
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment