Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
D
data-server
Project
Project
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
licc
data-server
Commits
45122387
Commit
45122387
authored
Mar 12, 2021
by
liqin
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixed
parent
12e752ce
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
62 deletions
+69
-62
LoginController.java
...n/wisenergy/web/admin/controller/app/LoginController.java
+69
-62
No files found.
wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/LoginController.java
View file @
45122387
...
...
@@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.*;
import
javax.imageio.ImageIO
;
import
javax.servlet.http.HttpServletRequest
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.StandardCharsets
;
...
...
@@ -32,7 +33,6 @@ import java.util.HashMap;
import
java.util.Map
;
/**
*
* Created by m1991 on 2021/3/2 13:35
*/
@Api
(
tags
=
"登录/注册"
)
...
...
@@ -42,7 +42,6 @@ import java.util.Map;
public
class
LoginController
{
@Autowired
private
RedisUtils
redisUtils
;
...
...
@@ -54,6 +53,7 @@ public class LoginController {
/**
* 手机登录接口
*
* @param userId
* @param sms
* @return
...
...
@@ -65,92 +65,98 @@ public class LoginController {
@ApiImplicitParam
(
name
=
"sms"
,
value
=
"验证码"
,
dataType
=
"String"
),
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户手机号"
,
required
=
true
,
dataType
=
"String"
)})
@RequestMapping
(
"/login"
)
public
Map
loginBySms
(
@RequestParam
String
userId
,
@RequestParam
String
sms
)
throws
Exception
{
User
users
=
null
;
Map
map
=
new
HashMap
();
String
key
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
SMS_PRIFIX
,
userId
,
Constants
.
Sms
.
CodeType
.
LOGIN_OR_REGISTER
+
""
);
String
redisCode
=
redisUtils
.
getValue
(
key
);
if
(
StringUtil
.
isBlank
(
redisCode
)
||
!
sms
.
equals
(
redisCode
))
{
map
.
put
(
"code"
,
"1003"
);
map
.
put
(
"msg"
,
"验证码错误"
);
public
Map
loginBySms
(
@RequestParam
String
userId
,
@RequestParam
String
sms
)
throws
Exception
{
User
users
=
null
;
Map
map
=
new
HashMap
();
String
key
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
SMS_PRIFIX
,
userId
,
Constants
.
Sms
.
CodeType
.
LOGIN_OR_REGISTER
+
""
);
String
redisCode
=
redisUtils
.
getValue
(
key
);
if
(
StringUtil
.
isBlank
(
redisCode
)
||
!
sms
.
equals
(
redisCode
))
{
map
.
put
(
"code"
,
"1003"
);
map
.
put
(
"msg"
,
"验证码错误"
);
// throw new BaseException(ResultEnum.FAIL_VERIFY);
return
map
;
}
redisUtils
.
delete
(
key
);
//根据手机号判断用户是否存在
//不存在则保存用户信息--修改为提示用户注册
users
=
usersService
.
queryUsersByPhone
(
userId
);
if
(
null
==
users
)
{
users
=
usersService
.
queryUsersByPhone
(
userId
);
if
(
null
==
users
)
{
//throw new BaseException(ResultEnum.FAIL_ACCOUNT_NOT_EXIST);
map
.
put
(
"code"
,
"1005"
);
map
.
put
(
"msg"
,
"账号不存在,请注册"
);
map
.
put
(
"code"
,
"1005"
);
map
.
put
(
"msg"
,
"账号不存在,请注册"
);
return
map
;
}
String
token
=
null
;
String
token
=
null
;
token
=
createToken
(
users
);
if
(!
StringUtil
.
isBlank
(
token
))
{
String
wyz
=
users
.
getInviteCode
();
Map
<
String
,
Object
>
map1
=
StringUtil
.
createSimpleMap
(
"token"
,
token
);
map1
.
put
(
"wyz"
,
wyz
);
map1
.
put
(
"code"
,
0
);
map1
.
put
(
"msg"
,
"成功!"
);
if
(!
StringUtil
.
isBlank
(
token
))
{
String
wyz
=
users
.
getInviteCode
();
Map
<
String
,
Object
>
map1
=
StringUtil
.
createSimpleMap
(
"token"
,
token
);
map1
.
put
(
"wyz"
,
wyz
);
map1
.
put
(
"code"
,
0
);
map1
.
put
(
"msg"
,
"成功!"
);
return
map1
;
}
return
(
Map
)
ResultUtils
.
returnFail
();
}
public
String
createToken
(
User
users
)
throws
Exception
{
String
token
=
StringUtil
.
createToken
();
public
String
createToken
(
User
users
)
throws
Exception
{
String
token
=
StringUtil
.
createToken
();
//保存token
String
tokenKey
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
TOKEN_PRIFIX
,
token
);
UsersDto
usersDto
=
new
UsersDto
();
BeanUtils
.
copyProperties
(
users
,
usersDto
);
redisUtils
.
set
(
tokenKey
,
JSONObject
.
toJSONString
(
usersDto
),
Constants
.
Duration
.
HALF_HOUR_INT
);
String
tokenKey
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
TOKEN_PRIFIX
,
token
);
UsersDto
usersDto
=
new
UsersDto
();
BeanUtils
.
copyProperties
(
users
,
usersDto
);
redisUtils
.
set
(
tokenKey
,
JSONObject
.
toJSONString
(
usersDto
),
Constants
.
Duration
.
HALF_HOUR_INT
);
return
token
;
}
@ApiOperation
(
value
=
"获取用户登录token信息"
,
notes
=
"获取用户登录token信息"
,
httpMethod
=
"POST"
,
produces
=
"application/json; charset=UTF-8"
)
@RequestMapping
(
"/info"
)
public
Map
info
(
HttpServletRequest
request
)
throws
Exception
{
String
token
=
request
.
getHeader
(
"token"
);
String
tokenKey
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
TOKEN_PRIFIX
,
token
);
String
userDtoJson
=
redisUtils
.
getValue
(
tokenKey
);
if
(
StringUtil
.
isBlank
(
userDtoJson
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"2001"
);
map
.
put
(
"msg"
,
"未登录"
);
public
Map
info
(
HttpServletRequest
request
)
throws
Exception
{
String
token
=
request
.
getHeader
(
"token"
);
String
tokenKey
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
TOKEN_PRIFIX
,
token
);
String
userDtoJson
=
redisUtils
.
getValue
(
tokenKey
);
if
(
StringUtil
.
isBlank
(
userDtoJson
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"2001"
);
map
.
put
(
"msg"
,
"未登录"
);
return
map
;
}
UsersDto
usersDto
=
JSONObject
.
parseObject
(
userDtoJson
,
UsersDto
.
class
);
UsersDto
usersDto
=
JSONObject
.
parseObject
(
userDtoJson
,
UsersDto
.
class
);
usersDto
.
setPassword
(
null
);
return
(
Map
)
ResultUtils
.
returnDataSuccess
(
userDtoJson
);
}
@ApiOperation
(
value
=
"二维码邀请注册"
,
notes
=
"二维码邀请注册"
,
httpMethod
=
"GET"
)
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户主键ID"
,
required
=
true
,
dataType
=
"
Stri
ng"
)
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户主键ID"
,
required
=
true
,
dataType
=
"
Lo
ng"
)
})
@GetMapping
(
value
=
"/registerByQrCode"
)
public
ResponseEntity
<
byte
[]>
registerByQrCode
(
@RequestParam
(
"userId"
)
Long
userId
,
HttpServletRequest
request
)
throws
IOException
{
public
ResponseEntity
<
byte
[]>
registerByQrCode
(
@RequestParam
(
"userId"
)
Long
userId
,
@RequestParam
(
value
=
"width"
,
required
=
false
,
defaultValue
=
"120"
)
int
width
,
@RequestParam
(
value
=
"height"
,
required
=
false
,
defaultValue
=
"120"
)
int
height
,
HttpServletRequest
request
)
throws
IOException
{
User
user
=
this
.
usersService
.
getUserById
(
userId
);
if
(
user
!=
null
)
{
String
regFullUrl
=
request
.
getScheme
()
+
"://"
+
request
.
getServerName
()
+
":"
+
request
.
getServerPort
()
String
regFullUrl
=
request
.
getScheme
()
+
"://"
+
request
.
getServerName
()
+
":"
+
request
.
getServerPort
()
+
"/front/register?beInvitedCode="
+
user
.
getInviteCode
();
final
InputStream
is
=
new
ClassPathResource
(
"templates/background.jpg"
).
getInputStream
();
BufferedImage
srcImage
=
ImageIO
.
read
(
is
);
QrConfig
config
=
new
QrConfig
(
130
,
130
);
QrConfig
config
=
new
QrConfig
(
width
,
height
);
config
.
setCharset
(
StandardCharsets
.
UTF_8
);
config
.
setMargin
(
0
);
BufferedImage
waterImage
=
QrCodeUtil
.
generate
(
regFullUrl
,
config
);
BufferedImage
waterImage
=
QrCodeUtil
.
generate
(
regFullUrl
,
config
);
byte
[]
bytes
=
ImageUtil
.
watermarkImageSimple1
(
srcImage
,
waterImage
);
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
ImageIO
.
write
(
waterImage
,
"jpg"
,
out
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
IMAGE_JPEG
);
headers
.
setContentLength
(
bytes
.
length
);
return
new
ResponseEntity
<>(
bytes
,
headers
,
HttpStatus
.
OK
);
return
new
ResponseEntity
<>(
out
.
toByteArray
()
,
headers
,
HttpStatus
.
OK
);
}
return
null
;
}
...
...
@@ -159,45 +165,46 @@ public class LoginController {
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户手机号"
,
required
=
true
,
dataType
=
"String"
),
@ApiImplicitParam
(
name
=
"beInvitedCode"
,
value
=
"推荐人邀请码"
,
required
=
false
,
dataType
=
"String"
),
@ApiImplicitParam
(
name
=
"sms"
,
value
=
"验证码"
,
required
=
true
,
dataType
=
"String"
)
@ApiImplicitParam
(
name
=
"sms"
,
value
=
"验证码"
,
required
=
true
,
dataType
=
"String"
)
})
@RequestMapping
(
"/register"
)
public
Map
register
(
@RequestParam
String
userId
,
@RequestParam
String
beInvitedCode
,
@RequestParam
String
sms
)
throws
Exception
{
User
users
=
null
;
String
key
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
SMS_PRIFIX
,
userId
,
Constants
.
Sms
.
CodeType
.
LOGIN_OR_REGISTER
+
""
);
String
redisCode
=
redisUtils
.
getValue
(
key
);
if
(
StringUtil
.
isBlank
(
redisCode
)
||
!
sms
.
equals
(
redisCode
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"1003"
);
map
.
put
(
"msg"
,
"验证码错误"
);
public
Map
register
(
@RequestParam
String
userId
,
@RequestParam
String
beInvitedCode
,
@RequestParam
String
sms
)
throws
Exception
{
User
users
=
null
;
String
key
=
StringUtil
.
formatKeyWithPrefix
(
Constants
.
RedisKey
.
PROJECT_PRIFIX
,
Constants
.
RedisKey
.
SMS_PRIFIX
,
userId
,
Constants
.
Sms
.
CodeType
.
LOGIN_OR_REGISTER
+
""
);
String
redisCode
=
redisUtils
.
getValue
(
key
);
if
(
StringUtil
.
isBlank
(
redisCode
)
||
!
sms
.
equals
(
redisCode
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"1003"
);
map
.
put
(
"msg"
,
"验证码错误"
);
return
map
;
}
redisUtils
.
delete
(
key
);
//判断phone是否符合输入类型
if
(!
userId
.
matches
(
Constants
.
RegConstant
.
PHONE_REGSTR
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"1008"
);
map
.
put
(
"msg"
,
"手机号码格式不正确"
);
if
(!
userId
.
matches
(
Constants
.
RegConstant
.
PHONE_REGSTR
))
{
Map
map
=
new
HashMap
();
map
.
put
(
"code"
,
"1008"
);
map
.
put
(
"msg"
,
"手机号码格式不正确"
);
return
map
;
}
return
usersService
.
userByZx
(
userId
,
beInvitedCode
);
return
usersService
.
userByZx
(
userId
,
beInvitedCode
);
}
/**
* 退出登录
*
* @param request
* @return
*/
@ApiOperation
(
value
=
"退出登录"
,
produces
=
"application/json"
,
notes
=
"退出登录"
)
@ApiImplicitParam
(
paramType
=
"header"
,
name
=
"token"
,
value
=
"用户token"
,
required
=
true
,
dataType
=
"String"
)
@ApiImplicitParam
(
paramType
=
"header"
,
name
=
"token"
,
value
=
"用户token"
,
required
=
true
,
dataType
=
"String"
)
@PostMapping
(
"/logout"
)
public
Result
logout
(
HttpServletRequest
request
)
{
log
.
info
(
"退出登录"
);
Result
result
=
ResultUtils
.
returnFail
();
String
token
=
request
.
getHeader
(
"token"
);
String
key
=
RedisKeyUtils
.
formatKeyWithPrefix
(
Constants
.
Redis
.
PREFIX_TOKEN
,
token
);
if
(
redisUtils
.
getValue
(
key
)
==
null
)
{
if
(
redisUtils
.
getValue
(
key
)
==
null
)
{
log
.
info
(
"要退出登录的用户未登录"
);
return
ResultUtils
.
returnResult
(
ResultEnum
.
FILE_NOT_LOGIN
);
}
...
...
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