Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 5ae97a31
authored
Jul 11, 2023
by
chamberone
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加用户登录和获取用户接口
1 parent
baf13f7e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
537 additions
and
439 deletions
project-interface/src/main/java/com/dituhui/pea/pojo/UserLoginDTO.java
project-interface/src/main/java/com/dituhui/pea/user/IUser.java
project-user/src/main/java/com/dituhui/pea/user/controller/RoleController.java
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
project-user/src/main/java/com/dituhui/pea/user/dao/UserDao.java
project-user/src/main/java/com/dituhui/pea/user/service/UserService.java
project-interface/src/main/java/com/dituhui/pea/pojo/UserLoginDTO.java
0 → 100644
View file @
5ae97a3
package
com
.
dituhui
.
pea
.
pojo
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
lombok.Data
;
@Data
public
class
UserLoginDTO
{
private
String
account
;
@JsonIgnore
private
String
password
;
private
String
nickname
;
private
String
email
;
private
String
phone
;
private
String
birthday
;
private
String
region
;
private
Integer
sex
;
private
String
avatar
;
private
String
wechat
;
private
String
token
;
}
project-interface/src/main/java/com/dituhui/pea/user/IUser.java
View file @
5ae97a3
package
com
.
dituhui
.
pea
.
user
;
package
com
.
dituhui
.
pea
.
user
;
import
com.dituhui.pea.common.Result
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
import
com.dituhui.pea.pojo.ThirdUserInfo
;
import
com.dituhui.pea.pojo.ThirdUserInfo
;
import
com.dituhui.pea.pojo.UserInfo
;
import
com.dituhui.pea.pojo.UserInfo
;
import
com.dituhui.pea.pojo.UserLoginDTO
;
import
com.dituhui.pea.pojo.WebResult
;
import
com.dituhui.pea.pojo.WebResult
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestParam
;
...
@@ -16,6 +19,12 @@ import org.springframework.web.bind.annotation.RequestParam;
...
@@ -16,6 +19,12 @@ import org.springframework.web.bind.annotation.RequestParam;
*/
*/
@FeignClient
(
value
=
"user"
)
@FeignClient
(
value
=
"user"
)
public
interface
IUser
{
public
interface
IUser
{
@RequestMapping
(
value
=
"/user/login"
,
method
=
RequestMethod
.
POST
)
public
Result
<?>
userLogin
(
@RequestBody
UserLoginDTO
user
);
@RequestMapping
(
value
=
"/user/userInfo"
,
method
=
RequestMethod
.
GET
)
public
Result
<?>
getUserInfo
(
@RequestHeader
(
name
=
"Authorization"
,
required
=
true
)
String
authorization
);
/**
/**
* 获取当前登陆用户信息
* 获取当前登陆用户信息
...
...
project-user/src/main/java/com/dituhui/pea/user/controller/RoleController.java
View file @
5ae97a3
...
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController;
...
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RestController;
/**
/**
* 角色控制层
* 角色控制层
* @author zl
*/
*/
@RestController
@RestController
@RefreshScope
@RefreshScope
...
...
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
View file @
5ae97a3
...
@@ -2,6 +2,8 @@ package com.dituhui.pea.user.controller;
...
@@ -2,6 +2,8 @@ package com.dituhui.pea.user.controller;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
com.dituhui.pea.common.Result
;
import
com.dituhui.pea.enums.RedisKeyGroup
;
import
com.dituhui.pea.enums.RedisKeyGroup
;
import
com.dituhui.pea.enums.StatusCodeEnum
;
import
com.dituhui.pea.enums.StatusCodeEnum
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
...
@@ -13,13 +15,15 @@ import org.apache.commons.lang.StringUtils;
...
@@ -13,13 +15,15 @@ import org.apache.commons.lang.StringUtils;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
/**
/**
* 用户控制层
* 用户控制层
*
*
* @author zl
* @since 2020-12-09 15:42:00
*/
*/
@RestController
@RestController
public
class
UserController
implements
IUser
{
public
class
UserController
implements
IUser
{
...
@@ -29,6 +33,14 @@ public class UserController implements IUser {
...
@@ -29,6 +33,14 @@ public class UserController implements IUser {
@Autowired
@Autowired
private
RedisService
redisService
;
private
RedisService
redisService
;
public
Result
<?>
userLogin
(
UserLoginDTO
user
)
{
return
userService
.
userLogin
(
user
.
getAccount
(),
user
.
getAccount
());
}
public
Result
<?>
getUserInfo
(
String
token
)
{
return
userService
.
getUserInfo
(
token
);
}
@Override
@Override
public
WebResult
<
UserInfo
>
getCurrentUserInfo
(
String
userToken
,
Boolean
needTeamInfo
)
{
public
WebResult
<
UserInfo
>
getCurrentUserInfo
(
String
userToken
,
Boolean
needTeamInfo
)
{
...
...
project-user/src/main/java/com/dituhui/pea/user/dao/UserDao.java
View file @
5ae97a3
package
com
.
dituhui
.
pea
.
user
.
dao
;
package
com
.
dituhui
.
pea
.
user
.
dao
;
import
java.util.List
;
import
java.util.List
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.JpaRepository
;
...
@@ -15,56 +14,72 @@ import com.dituhui.pea.user.entity.UserEntity;
...
@@ -15,56 +14,72 @@ import com.dituhui.pea.user.entity.UserEntity;
* @author zl
* @author zl
* @since 2020-12-09 15:34:42
* @since 2020-12-09 15:34:42
*/
*/
public
interface
UserDao
extends
JpaRepository
<
UserEntity
,
String
>,
public
interface
UserDao
extends
JpaRepository
<
UserEntity
,
String
>,
JpaSpecificationExecutor
<
UserEntity
>,
JpaSpecificationExecutor
<
UserEntity
>,
CrudRepository
<
UserEntity
,
String
>
{
CrudRepository
<
UserEntity
,
String
>
{
/**
* 根据手机号查询
*
* @param phoneNumber 手机号码
* @return
*/
UserEntity
findByPhone
(
String
phone
);
/**
/**
* 根据手机号查询
* 根据邮箱查询
* @param phoneNumber 手机号码
*
* @return
* @param email 邮箱
*/
* @return
UserEntity
findByPhone
(
String
phone
);
*/
UserEntity
findByEmail
(
String
email
);
/**
/**
* 根据邮箱查询
* 根据微信查询
* @param email 邮箱
*
* @return
* @param wechat 微信
*/
* @return
UserEntity
findByEmail
(
String
email
);
*/
List
<
UserEntity
>
findByWechat
(
String
wechat
);
/**
/**
* 根据微信查询
* 根据微信小程序查询
* @param wechat 微信
*
* @return
* @param wechatMiniProgram 微信小程序openId
*/
* @return
List
<
UserEntity
>
findByWechat
(
String
wechat
);
*/
List
<
UserEntity
>
findByWechatMiniProgram
(
String
wechatMiniProgram
);
/**
/**
* 根据微信小程序查询
* 根据账号查询
* @param wechatMiniProgram 微信小程序openId
*
* @return
* @param account 账号
*/
* @return
List
<
UserEntity
>
findByWechatMiniProgram
(
String
wechatMiniProgram
);
*/
UserEntity
findByAccount
(
String
account
);
/**
/**
* 根据账号查询
* 根据账号查询
* @param account 账号
*
* @return
* @param account 账号
*/
* @param password 加密密码
UserEntity
findByAccount
(
String
account
);
* @return
*/
UserEntity
findByAccountAndPassword
(
String
account
,
String
password
);
/**
/**
* 根据账号查询
* 根据账号查询
* @param account 账号
*
* @return
* @param account 账号
*/
* @return
List
<
UserEntity
>
findAllByAccount
(
String
account
);
*/
List
<
UserEntity
>
findAllByAccount
(
String
account
);
/**
/**
* 根据用户ID批量查询
* 根据用户ID批量查询
* @param userIds 用户ID集合
*
* @return
* @param userIds 用户ID集合
*/
* @return
List
<
UserEntity
>
findByIdIn
(
List
<
String
>
userIds
);
*/
List
<
UserEntity
>
findByIdIn
(
List
<
String
>
userIds
);
}
}
project-user/src/main/java/com/dituhui/pea/user/service/UserService.java
View file @
5ae97a3
...
@@ -12,11 +12,15 @@ import org.springframework.data.jpa.domain.Specification;
...
@@ -12,11 +12,15 @@ import org.springframework.data.jpa.domain.Specification;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
com.dituhui.pea.common.Result
;
import
com.dituhui.pea.enums.RedisKeyGroup
;
import
com.dituhui.pea.enums.StatusCodeEnum
;
import
com.dituhui.pea.enums.StatusCodeEnum
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
import
com.dituhui.pea.enums.ThirdPartyEnum
;
import
com.dituhui.pea.exception.BusinessException
;
import
com.dituhui.pea.exception.BusinessException
;
import
com.dituhui.pea.pojo.ThirdUserInfo
;
import
com.dituhui.pea.pojo.ThirdUserInfo
;
import
com.dituhui.pea.pojo.UserInfo
;
import
com.dituhui.pea.pojo.UserInfo
;
import
com.dituhui.pea.pojo.UserLoginDTO
;
import
com.dituhui.pea.user.commom.RedisService
;
import
com.dituhui.pea.user.constant.TextConstant
;
import
com.dituhui.pea.user.constant.TextConstant
;
import
com.dituhui.pea.user.dao.UserDao
;
import
com.dituhui.pea.user.dao.UserDao
;
import
com.dituhui.pea.user.entity.UserEntity
;
import
com.dituhui.pea.user.entity.UserEntity
;
...
@@ -27,404 +31,440 @@ import com.dituhui.pea.user.utils.TextHelper;
...
@@ -27,404 +31,440 @@ import com.dituhui.pea.user.utils.TextHelper;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.lang.Validator
;
import
cn.hutool.core.lang.Validator
;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.crypto.SecureUtil
;
import
cn.hutool.crypto.SecureUtil
;
/**
/**
* 用户业务层
* 用户业务层
*
@author zl
*
*/
*/
@Service
@Service
public
class
UserService
{
public
class
UserService
{
@Autowired
/**
UserDao
userDao
;
* 失效时间ms
*/
@Autowired
private
static
final
int
aliveTime
=
7200000
;
ThirdStrategyFactory
thirdStrategyFactory
;
@Autowired
public
String
getUserName
(
String
userId
)
{
UserDao
userDao
;
return
"Jown Snow "
+
userId
;
}
@Autowired
ThirdStrategyFactory
thirdStrategyFactory
;
/**
* 查询用户信息
@Autowired
* @param id 第三方唯一id
private
RedisService
redisService
;
* @param type 第三方类型
* @return
public
Result
<?>
userLogin
(
String
account
,
String
password
)
{
*/
UserEntity
user
=
userDao
.
findByAccountAndPassword
(
account
,
SecureUtil
.
md5
(
password
));
public
UserInfo
queryUserByThirdParty
(
String
id
,
ThirdPartyEnum
type
)
{
if
(
null
==
user
)
{
ThirdStrategy
strategy
=
thirdStrategyFactory
.
getStrategy
(
type
);
// 生成token
UserEntity
userEntity
=
strategy
.
getUser
(
id
);
UserLoginDTO
userDTO
=
BeanUtil
.
copyProperties
(
user
,
UserLoginDTO
.
class
);
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
// 生成token
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
String
uuid
=
IdUtil
.
simpleUUID
();
}
// 2小时
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
long
timestamp
=
System
.
currentTimeMillis
()
+
aliveTime
;
return
user
;
redisService
.
set
(
RedisKeyGroup
.
authToken
+
":"
+
uuid
,
user
,
timestamp
);
}
userDTO
.
setToken
(
uuid
);
return
Result
.
success
(
userDTO
);
/**
}
else
{
* 查询用户信息
return
Result
.
failed
(
"鉴权失败"
);
*
}
* @param phone 电话
}
* @return
*/
public
Result
<?>
getUserInfo
(
String
token
)
{
public
UserInfo
queryUserByPhone
(
String
phone
)
{
UserEntity
user
=
(
UserEntity
)
redisService
.
get
(
RedisKeyGroup
.
authToken
+
":"
+
token
);
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
return
Result
.
success
(
user
);
if
(
ObjectUtil
.
isEmpty
(
userEntity
))
{
}
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
}
public
String
getUserName
(
String
userId
)
{
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
return
"Jown Snow "
+
userId
;
return
user
;
}
}
/**
/**
* 查询用户信息
* 查询用户信息
*
*
* @param id 第三方唯一id
* @param account 账号
* @param type 第三方类型
* @return
* @return
*/
*/
public
UserInfo
queryTeamByAccount
(
String
account
)
{
public
UserInfo
queryUserByThirdParty
(
String
id
,
ThirdPartyEnum
type
)
{
UserEntity
userEntity
=
null
;
ThirdStrategy
strategy
=
thirdStrategyFactory
.
getStrategy
(
type
);
if
(
TextHelper
.
check
(
account
,
TextConstant
.
CHINA_PHONE
))
{
UserEntity
userEntity
=
strategy
.
getUser
(
id
);
//手机号码
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
userEntity
=
userDao
.
findByPhone
(
account
);
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
}
else
if
(
TextHelper
.
check
(
account
,
TextConstant
.
USERNAME
))
{
}
//用户名
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
userEntity
=
userDao
.
findByAccount
(
account
);
return
user
;
}
else
if
(
TextHelper
.
check
(
account
,
TextConstant
.
EMAIL
))
{
}
//邮箱
userEntity
=
userDao
.
findByEmail
(
account
);
/**
}
* 查询用户信息
if
(
userEntity
==
null
)
{
*
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
* @param phone 电话
}
* @return
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
*/
return
user
;
public
UserInfo
queryUserByPhone
(
String
phone
)
{
}
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
if
(
ObjectUtil
.
isEmpty
(
userEntity
))
{
/**
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
* 查询用户信息
}
* @param id 用户ID
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
* @return
return
user
;
*/
}
public
UserInfo
queryUserById
(
String
id
)
{
UserEntity
userEntity
=
userDao
.
findById
(
id
).
orElse
(
null
);
/**
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
* 查询用户信息
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
*
}
* @param account 账号
return
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
* @return
}
*/
public
UserInfo
queryTeamByAccount
(
String
account
)
{
/**
UserEntity
userEntity
=
null
;
* 保存用户信息
if
(
TextHelper
.
check
(
account
,
TextConstant
.
CHINA_PHONE
))
{
* @param userInfo
// 手机号码
* @return
userEntity
=
userDao
.
findByPhone
(
account
);
*/
}
else
if
(
TextHelper
.
check
(
account
,
TextConstant
.
USERNAME
))
{
@Transactional
(
rollbackFor
=
Exception
.
class
)
// 用户名
public
UserInfo
saveUser
(
UserInfo
userInfo
)
{
userEntity
=
userDao
.
findByAccount
(
account
);
// 校验用户信息
}
else
if
(
TextHelper
.
check
(
account
,
TextConstant
.
EMAIL
))
{
checkAccount
(
userInfo
);
// 邮箱
UserEntity
userEntity
=
BeanUtil
.
copyProperties
(
userInfo
,
UserEntity
.
class
);
userEntity
=
userDao
.
findByEmail
(
account
);
// 密码MD5加密
}
userEntity
.
setPassword
(
SecureUtil
.
md5
(
userInfo
.
getPassword
()));
if
(
userEntity
==
null
)
{
// 保存用户信息
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
userEntity
=
userDao
.
save
(
userEntity
);
}
return
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
UserInfo
user
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
}
return
user
;
}
/**
/**
* 注册用户
* 查询用户信息
* @param userInfo 用户信息
*
* @return
* @param id 用户ID
*/
* @return
@Transactional
(
rollbackFor
=
Exception
.
class
)
*/
public
UserInfo
register
(
UserInfo
userInfo
)
{
public
UserInfo
queryUserById
(
String
id
)
{
// 校验 [账号,手机号] 是否有重复
UserEntity
userEntity
=
userDao
.
findById
(
id
).
orElse
(
null
);
if
(
checkAccountOrPhone
(
userInfo
.
getPhone
()))
{
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
throw
new
BusinessException
(
StatusCodeEnum
.
ACCOUNT_EXISTS
);
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
}
}
// 校验邮箱
return
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getEmail
()))
{
}
if
(
checkEmail
(
userInfo
.
getEmail
(),
userInfo
.
getId
()))
{
throw
new
BusinessException
(
StatusCodeEnum
.
EMAIL_EXISTS
);
/**
}
* 保存用户信息
}
*
UserEntity
userEntity
=
BeanUtil
.
copyProperties
(
userInfo
,
UserEntity
.
class
);
* @param userInfo
userEntity
.
setCreatedTime
(
new
Date
());
* @return
userEntity
.
setCreatedBy
(
userEntity
.
getNickname
());
*/
// 密码MD5加密
@Transactional
(
rollbackFor
=
Exception
.
class
)
userEntity
.
setPassword
(
SecureUtil
.
md5
(
userInfo
.
getPassword
()));
public
UserInfo
saveUser
(
UserInfo
userInfo
)
{
// 保存用户信息
// 校验用户信息
userEntity
=
userDao
.
save
(
userEntity
);
checkAccount
(
userInfo
);
userInfo
.
setId
(
userEntity
.
getId
());
UserEntity
userEntity
=
BeanUtil
.
copyProperties
(
userInfo
,
UserEntity
.
class
);
return
userInfo
;
// 密码MD5加密
}
userEntity
.
setPassword
(
SecureUtil
.
md5
(
userInfo
.
getPassword
()));
// 保存用户信息
@Transactional
(
rollbackFor
=
Exception
.
class
)
userEntity
=
userDao
.
save
(
userEntity
);
public
UserInfo
thirdRegister
(
ThirdUserInfo
thirdUserInfo
)
{
return
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
// 组装用户信息
}
UserEntity
userEntity
=
new
UserEntity
();
userEntity
.
setAccount
(
initUserAccount
(
thirdUserInfo
.
getThirdType
()));
/**
userEntity
.
setNickname
(
thirdUserInfo
.
getThirdName
());
* 注册用户
userEntity
.
setAvatar
(
thirdUserInfo
.
getThirdAvatar
());
*
userEntity
.
setCreatedTime
(
new
Date
());
* @param userInfo 用户信息
ThirdPartyEnum
thirdPartyEnum
=
ThirdPartyEnum
.
valueOf
(
thirdUserInfo
.
getThirdType
());
* @return
switch
(
thirdPartyEnum
)
{
*/
case
WECHAT:
@Transactional
(
rollbackFor
=
Exception
.
class
)
userEntity
.
setWechat
(
thirdUserInfo
.
getThirdId
());
public
UserInfo
register
(
UserInfo
userInfo
)
{
break
;
// 校验 [账号,手机号] 是否有重复
case
WECHAT_MINI_PROGRAM:
if
(
checkAccountOrPhone
(
userInfo
.
getPhone
()))
{
userEntity
.
setWechatMiniProgram
(
thirdUserInfo
.
getThirdId
());
throw
new
BusinessException
(
StatusCodeEnum
.
ACCOUNT_EXISTS
);
userEntity
.
setPhone
(
thirdUserInfo
.
getPhone
());
}
default
:
// 校验邮箱
}
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getEmail
()))
{
if
(
StringUtils
.
isNotBlank
(
thirdUserInfo
.
getPhone
()))
{
if
(
checkEmail
(
userInfo
.
getEmail
(),
userInfo
.
getId
()))
{
// 查询手机号是否存在, 存在就和第三方账号绑定, 不存在就注册
throw
new
BusinessException
(
StatusCodeEnum
.
EMAIL_EXISTS
);
String
thirdId
=
thirdUserInfo
.
getThirdId
();
}
String
phone
=
thirdUserInfo
.
getPhone
();
}
UserInfo
user
=
bindThirdPartyUsers
(
thirdId
,
thirdPartyEnum
,
phone
);
UserEntity
userEntity
=
BeanUtil
.
copyProperties
(
userInfo
,
UserEntity
.
class
);
if
(
null
!=
user
)
{
userEntity
.
setCreatedTime
(
new
Date
());
return
user
;
userEntity
.
setCreatedBy
(
userEntity
.
getNickname
());
}
// 密码MD5加密
}
userEntity
.
setPassword
(
SecureUtil
.
md5
(
userInfo
.
getPassword
()));
// 保存用户信息
// 保存用户信息
userEntity
=
userDao
.
save
(
userEntity
);
userEntity
=
userDao
.
save
(
userEntity
);
// 对象转换
userInfo
.
setId
(
userEntity
.
getId
());
UserInfo
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
return
userInfo
;
userInfo
.
setId
(
userEntity
.
getId
());
}
return
userInfo
;
}
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
UserInfo
thirdRegister
(
ThirdUserInfo
thirdUserInfo
)
{
/**
// 组装用户信息
* 绑定三方用户
UserEntity
userEntity
=
new
UserEntity
();
* @param thirdId 第三方ID
userEntity
.
setAccount
(
initUserAccount
(
thirdUserInfo
.
getThirdType
()));
* @param thirdPartyEnum 第三方类型枚举
userEntity
.
setNickname
(
thirdUserInfo
.
getThirdName
());
* @param phone 手机号
userEntity
.
setAvatar
(
thirdUserInfo
.
getThirdAvatar
());
*/
userEntity
.
setCreatedTime
(
new
Date
());
public
UserInfo
bindThirdPartyUsers
(
String
thirdId
,
ThirdPartyEnum
thirdPartyEnum
,
String
phone
)
{
ThirdPartyEnum
thirdPartyEnum
=
ThirdPartyEnum
.
valueOf
(
thirdUserInfo
.
getThirdType
());
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
switch
(
thirdPartyEnum
)
{
if
(
null
==
userEntity
)
{
case
WECHAT:
return
null
;
userEntity
.
setWechat
(
thirdUserInfo
.
getThirdId
());
}
break
;
switch
(
thirdPartyEnum
)
{
case
WECHAT_MINI_PROGRAM:
case
WECHAT:
userEntity
.
setWechatMiniProgram
(
thirdUserInfo
.
getThirdId
());
userEntity
.
setWechat
(
thirdId
);
userEntity
.
setPhone
(
thirdUserInfo
.
getPhone
());
break
;
default
:
case
WECHAT_MINI_PROGRAM:
}
userEntity
.
setWechatMiniProgram
(
thirdId
);
if
(
StringUtils
.
isNotBlank
(
thirdUserInfo
.
getPhone
()))
{
default
:
// 查询手机号是否存在, 存在就和第三方账号绑定, 不存在就注册
}
String
thirdId
=
thirdUserInfo
.
getThirdId
();
userEntity
=
userDao
.
save
(
userEntity
);
String
phone
=
thirdUserInfo
.
getPhone
();
// 对象转换
UserInfo
user
=
bindThirdPartyUsers
(
thirdId
,
thirdPartyEnum
,
phone
);
UserInfo
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
if
(
null
!=
user
)
{
return
userInfo
;
return
user
;
}
}
}
// 保存用户信息
private
String
initUserAccount
(
String
thirdType
)
{
userEntity
=
userDao
.
save
(
userEntity
);
ThirdPartyEnum
thirdPartyEnum
=
ThirdPartyEnum
.
valueOf
(
thirdType
);
// 对象转换
String
prefix
=
""
;
UserInfo
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
switch
(
thirdPartyEnum
)
{
userInfo
.
setId
(
userEntity
.
getId
());
case
QQ:
return
userInfo
;
prefix
=
"QQ用户"
;
}
break
;
case
WECHAT:
/**
prefix
=
"微信用户"
;
* 绑定三方用户
break
;
*
case
WEIBO:
* @param thirdId 第三方ID
prefix
=
"微博用户"
;
* @param thirdPartyEnum 第三方类型枚举
break
;
* @param phone 手机号
case
WECHAT_MINI_PROGRAM:
*/
prefix
=
"小程序用户"
;
public
UserInfo
bindThirdPartyUsers
(
String
thirdId
,
ThirdPartyEnum
thirdPartyEnum
,
String
phone
)
{
default
:
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
}
if
(
null
==
userEntity
)
{
long
count
=
600
+
userDao
.
count
();
return
null
;
String
nickName
;
}
for
(
int
index
=
0
;
index
<
6
;
index
++)
{
switch
(
thirdPartyEnum
)
{
nickName
=
prefix
+
(
count
+
index
);
case
WECHAT:
UserEntity
userEntity
=
userDao
.
findByAccount
(
nickName
);
userEntity
.
setWechat
(
thirdId
);
if
(
ObjectUtil
.
isNotEmpty
(
userEntity
))
{
break
;
continue
;
case
WECHAT_MINI_PROGRAM:
}
userEntity
.
setWechatMiniProgram
(
thirdId
);
return
nickName
;
default
:
}
}
throw
new
BusinessException
(
StatusCodeEnum
.
FAILED
);
userEntity
=
userDao
.
save
(
userEntity
);
}
// 对象转换
UserInfo
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
return
userInfo
;
/**
}
* 更新用户信息
* @param userInfo
private
String
initUserAccount
(
String
thirdType
)
{
* @return
ThirdPartyEnum
thirdPartyEnum
=
ThirdPartyEnum
.
valueOf
(
thirdType
);
*/
String
prefix
=
""
;
@Transactional
(
rollbackFor
=
Exception
.
class
)
switch
(
thirdPartyEnum
)
{
public
UserInfo
updateUser
(
UserInfo
userInfo
)
{
case
QQ:
// 校验用户信息
prefix
=
"QQ用户"
;
checkAccount
(
userInfo
);
break
;
// 查询用户信息是否存在
case
WECHAT:
UserEntity
userEntity
=
userDao
.
findById
(
userInfo
.
getId
()).
orElse
(
null
);
prefix
=
"微信用户"
;
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
break
;
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
case
WEIBO:
}
prefix
=
"微博用户"
;
assembleUpdateUserInfo
(
userInfo
,
userEntity
);
break
;
userEntity
=
userDao
.
save
(
userEntity
);
case
WECHAT_MINI_PROGRAM:
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
prefix
=
"小程序用户"
;
return
userInfo
;
default
:
}
}
long
count
=
600
+
userDao
.
count
();
String
nickName
;
for
(
int
index
=
0
;
index
<
6
;
index
++)
{
/**
nickName
=
prefix
+
(
count
+
index
);
* 删除用户信息
UserEntity
userEntity
=
userDao
.
findByAccount
(
nickName
);
* @param userId 用户ID
if
(
ObjectUtil
.
isNotEmpty
(
userEntity
))
{
*/
continue
;
@Transactional
(
rollbackFor
=
Exception
.
class
)
}
public
void
deleteUser
(
String
userId
){
return
nickName
;
// 查询用户信息是否存在
}
UserEntity
userEntity
=
userDao
.
findById
(
userId
).
orElse
(
null
);
throw
new
BusinessException
(
StatusCodeEnum
.
FAILED
);
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
}
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
}
/**
// 删除用户
* 更新用户信息
userDao
.
deleteById
(
userId
);
*
}
* @param userInfo
* @return
/**
*/
* 校验 [账号 手机号] 是否存在
@Transactional
(
rollbackFor
=
Exception
.
class
)
* @param phone 手机号
public
UserInfo
updateUser
(
UserInfo
userInfo
)
{
* @return
// 校验用户信息
*/
checkAccount
(
userInfo
);
private
Boolean
checkAccountOrPhone
(
String
phone
)
{
// 查询用户信息是否存在
List
<
UserEntity
>
userEntities
=
userDao
.
findAll
((
Specification
<
UserEntity
>)
(
root
,
query
,
cb
)
->
{
UserEntity
userEntity
=
userDao
.
findById
(
userInfo
.
getId
()).
orElse
(
null
);
Path
<
String
>
accountField
=
root
.
get
(
"account"
);
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
Path
<
String
>
phoneField
=
root
.
get
(
"phone"
);
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
Predicate
accountCondition
=
cb
.
equal
(
accountField
,
phone
);
}
Predicate
phoneCondition
=
cb
.
equal
(
phoneField
,
phone
);
assembleUpdateUserInfo
(
userInfo
,
userEntity
);
query
.
where
(
cb
.
or
(
accountCondition
,
phoneCondition
));
userEntity
=
userDao
.
save
(
userEntity
);
return
null
;
userInfo
=
BeanUtil
.
copyProperties
(
userEntity
,
UserInfo
.
class
);
});
return
userInfo
;
return
CollUtil
.
isNotEmpty
(
userEntities
);
}
}
/**
* 删除用户信息
/**
*
* 校验账号
* @param userId 用户ID
* @param userInfo 用户信息
*/
*/
@Transactional
(
rollbackFor
=
Exception
.
class
)
private
void
checkAccount
(
UserInfo
userInfo
)
{
public
void
deleteUser
(
String
userId
)
{
// 校验账号
// 查询用户信息是否存在
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getAccount
()))
{
UserEntity
userEntity
=
userDao
.
findById
(
userId
).
orElse
(
null
);
if
(
checkAccount
(
userInfo
.
getAccount
(),
userInfo
.
getId
()))
{
if
(
ObjectUtil
.
isNull
(
userEntity
))
{
throw
new
BusinessException
(
StatusCodeEnum
.
ACCOUNT_EXISTS
);
throw
new
BusinessException
(
StatusCodeEnum
.
USER_DOES_NOT_EXIST
);
}
}
}
// 删除用户
// 校验手机号
userDao
.
deleteById
(
userId
);
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getPhone
()))
{
}
// 校验手机格式是否正确
if
(!
Validator
.
isMobile
(
userInfo
.
getPhone
()))
{
/**
throw
new
BusinessException
(
StatusCodeEnum
.
COMMON_PARAM_ERROR
);
* 校验 [账号 手机号] 是否存在
}
*
if
(
checkPhone
(
userInfo
.
getPhone
(),
userInfo
.
getId
()))
{
* @param phone 手机号
throw
new
BusinessException
(
StatusCodeEnum
.
MOBILE_NUMBER_EXISTS
);
* @return
}
*/
}
private
Boolean
checkAccountOrPhone
(
String
phone
)
{
// 校验邮箱
List
<
UserEntity
>
userEntities
=
userDao
.
findAll
((
Specification
<
UserEntity
>)
(
root
,
query
,
cb
)
->
{
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getEmail
()))
{
Path
<
String
>
accountField
=
root
.
get
(
"account"
);
// 校验邮箱格式是否正确
Path
<
String
>
phoneField
=
root
.
get
(
"phone"
);
if
(!
Validator
.
isEmail
(
userInfo
.
getEmail
()))
{
Predicate
accountCondition
=
cb
.
equal
(
accountField
,
phone
);
throw
new
BusinessException
(
StatusCodeEnum
.
COMMON_PARAM_ERROR
);
Predicate
phoneCondition
=
cb
.
equal
(
phoneField
,
phone
);
}
query
.
where
(
cb
.
or
(
accountCondition
,
phoneCondition
));
if
(
checkEmail
(
userInfo
.
getEmail
(),
userInfo
.
getId
()))
{
return
null
;
throw
new
BusinessException
(
StatusCodeEnum
.
EMAIL_EXISTS
);
});
}
return
CollUtil
.
isNotEmpty
(
userEntities
);
}
}
}
/**
* 校验账号
*
/**
* @param userInfo 用户信息
* 校验邮箱是否存在
*/
* @param email 邮箱
private
void
checkAccount
(
UserInfo
userInfo
)
{
* @return
// 校验账号
*/
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getAccount
()))
{
private
Boolean
checkEmail
(
String
email
,
String
id
)
{
if
(
checkAccount
(
userInfo
.
getAccount
(),
userInfo
.
getId
()))
{
UserEntity
userEntity
=
userDao
.
findByEmail
(
email
);
throw
new
BusinessException
(
StatusCodeEnum
.
ACCOUNT_EXISTS
);
if
(
ObjectUtil
.
isNotNull
(
userEntity
)){
}
// 判断用户ID是否一致
}
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
// 校验手机号
}
else
{
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getPhone
()))
{
return
false
;
// 校验手机格式是否正确
}
if
(!
Validator
.
isMobile
(
userInfo
.
getPhone
()))
{
}
throw
new
BusinessException
(
StatusCodeEnum
.
COMMON_PARAM_ERROR
);
}
/**
if
(
checkPhone
(
userInfo
.
getPhone
(),
userInfo
.
getId
()))
{
* 校验账号是否存在
throw
new
BusinessException
(
StatusCodeEnum
.
MOBILE_NUMBER_EXISTS
);
* @param account 账号
}
* @return
}
*/
// 校验邮箱
private
Boolean
checkAccount
(
String
account
,
String
id
){
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getEmail
()))
{
UserEntity
userEntity
=
userDao
.
findByAccount
(
account
);
// 校验邮箱格式是否正确
if
(
ObjectUtil
.
isNotNull
(
userEntity
)){
if
(!
Validator
.
isEmail
(
userInfo
.
getEmail
()))
{
// 判断用户ID是否一致
throw
new
BusinessException
(
StatusCodeEnum
.
COMMON_PARAM_ERROR
);
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
}
}
else
{
if
(
checkEmail
(
userInfo
.
getEmail
(),
userInfo
.
getId
()))
{
return
false
;
throw
new
BusinessException
(
StatusCodeEnum
.
EMAIL_EXISTS
);
}
}
}
}
}
/**
/**
* 校验手机号是否存在
* 校验邮箱是否存在
* @param phone 账号
*
* @param id 用户Id
* @param email 邮箱
* @return
* @return
*/
*/
private
Boolean
checkPhone
(
String
phone
,
String
id
){
private
Boolean
checkEmail
(
String
email
,
String
id
)
{
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
UserEntity
userEntity
=
userDao
.
findByEmail
(
email
);
if
(
ObjectUtil
.
isNotNull
(
userEntity
)){
if
(
ObjectUtil
.
isNotNull
(
userEntity
))
{
// 判断用户ID是否一致
// 判断用户ID是否一致
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
}
/**
/**
* 组装更新用户信息
* 校验账号是否存在
* @param formUserInfo
*
* @param toUserEntity
* @param account 账号
*/
* @return
private
void
assembleUpdateUserInfo
(
UserInfo
formUserInfo
,
UserEntity
toUserEntity
)
{
*/
// 更新用户信息
private
Boolean
checkAccount
(
String
account
,
String
id
)
{
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getAccount
()))
{
UserEntity
userEntity
=
userDao
.
findByAccount
(
account
);
toUserEntity
.
setAccount
(
formUserInfo
.
getAccount
());
if
(
ObjectUtil
.
isNotNull
(
userEntity
))
{
}
// 判断用户ID是否一致
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getPassword
()))
{
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
toUserEntity
.
setPassword
(
SecureUtil
.
md5
(
formUserInfo
.
getPassword
()));
}
else
{
}
return
false
;
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getNickname
()))
{
}
toUserEntity
.
setNickname
(
formUserInfo
.
getNickname
());
}
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getEmail
()))
{
toUserEntity
.
setEmail
(
formUserInfo
.
getEmail
());
/**
}
* 校验手机号是否存在
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getPhone
()))
{
*
toUserEntity
.
setPhone
(
formUserInfo
.
getPhone
());
* @param phone 账号
}
* @param id 用户Id
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getAvatar
()))
{
* @return
toUserEntity
.
setAvatar
(
formUserInfo
.
getAvatar
());
*/
}
private
Boolean
checkPhone
(
String
phone
,
String
id
)
{
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getExtra
()))
{
UserEntity
userEntity
=
userDao
.
findByPhone
(
phone
);
toUserEntity
.
setExtra
(
formUserInfo
.
getExtra
());
if
(
ObjectUtil
.
isNotNull
(
userEntity
))
{
}
// 判断用户ID是否一致
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getBirthday
()))
{
return
!
StringUtils
.
equals
(
userEntity
.
getId
(),
id
);
toUserEntity
.
setBirthday
(
formUserInfo
.
getBirthday
());
}
else
{
}
return
false
;
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getRegion
()))
{
}
toUserEntity
.
setRegion
(
formUserInfo
.
getRegion
());
}
}
toUserEntity
.
setSex
(
formUserInfo
.
getSex
());
/**
}
* 组装更新用户信息
*
* @param formUserInfo
* @param toUserEntity
*/
private
void
assembleUpdateUserInfo
(
UserInfo
formUserInfo
,
UserEntity
toUserEntity
)
{
// 更新用户信息
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getAccount
()))
{
toUserEntity
.
setAccount
(
formUserInfo
.
getAccount
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getPassword
()))
{
toUserEntity
.
setPassword
(
SecureUtil
.
md5
(
formUserInfo
.
getPassword
()));
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getNickname
()))
{
toUserEntity
.
setNickname
(
formUserInfo
.
getNickname
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getEmail
()))
{
toUserEntity
.
setEmail
(
formUserInfo
.
getEmail
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getPhone
()))
{
toUserEntity
.
setPhone
(
formUserInfo
.
getPhone
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getAvatar
()))
{
toUserEntity
.
setAvatar
(
formUserInfo
.
getAvatar
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getExtra
()))
{
toUserEntity
.
setExtra
(
formUserInfo
.
getExtra
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getBirthday
()))
{
toUserEntity
.
setBirthday
(
formUserInfo
.
getBirthday
());
}
if
(
StringUtils
.
isNotBlank
(
formUserInfo
.
getRegion
()))
{
toUserEntity
.
setRegion
(
formUserInfo
.
getRegion
());
}
toUserEntity
.
setSex
(
formUserInfo
.
getSex
());
}
}
}
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