Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
head_group
xyqb-user2
Commits
d6590c44
Commit
d6590c44
authored
Mar 16, 2017
by
zhouqian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get protocol
parent
8d3be4ea
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
117 additions
and
192 deletions
+117
-192
WeChatController.java
...group/xyqb/controller/external/user/WeChatController.java
+117
-192
No files found.
src/main/java/cn/quantgroup/xyqb/controller/external/user/WeChatController.java
View file @
d6590c44
...
@@ -5,7 +5,6 @@ import cn.quantgroup.xyqb.controller.IBaseController;
...
@@ -5,7 +5,6 @@ import cn.quantgroup.xyqb.controller.IBaseController;
import
cn.quantgroup.xyqb.entity.Merchant
;
import
cn.quantgroup.xyqb.entity.Merchant
;
import
cn.quantgroup.xyqb.entity.User
;
import
cn.quantgroup.xyqb.entity.User
;
import
cn.quantgroup.xyqb.entity.WechatUserInfo
;
import
cn.quantgroup.xyqb.entity.WechatUserInfo
;
import
cn.quantgroup.xyqb.event.UserinfoChangedEvent
;
import
cn.quantgroup.xyqb.model.AuthBean
;
import
cn.quantgroup.xyqb.model.AuthBean
;
import
cn.quantgroup.xyqb.model.JsonResult
;
import
cn.quantgroup.xyqb.model.JsonResult
;
import
cn.quantgroup.xyqb.model.webchat.AccessTokenResponse
;
import
cn.quantgroup.xyqb.model.webchat.AccessTokenResponse
;
...
@@ -244,62 +243,11 @@ public class WeChatController implements IBaseController {
...
@@ -244,62 +243,11 @@ public class WeChatController implements IBaseController {
)
{
)
{
try
{
try
{
String
schema
=
getProtocol
();
String
schema
=
getProtocol
();
try
{
extData
=
getExtData
(
extData
);
extData
=
new
String
(
Base64
.
decodeBase64
(
extData
),
"UTF-8"
);
}
catch
(
Exception
ex
)
{
extData
=
""
;
}
LOGGER
.
info
(
"从微信extdata版本接口进入:{}, extData:{}"
,
schema
,
extData
);
LOGGER
.
info
(
"从微信extdata版本接口进入:{}, extData:{}"
,
schema
,
extData
);
if
(
StringUtils
.
isEmpty
(
extData
))
{
if
(
StringUtils
.
isEmpty
(
extData
))
{
// 从code获取token
normalLogin
(
code
,
systemKey
,
response
,
schema
);
Merchant
merchant
=
merchantService
.
findMerchantByName
(
systemKey
);
AccessTokenResponse
token
=
wechatService
.
getToken
(
code
);
if
(
token
==
null
)
{
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
}
WechatUserInfo
userInfo
=
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
token
.
getOpenId
());
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
}
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
// welcome 首次登录
if
(
userInfoInDb
==
null
)
{
// 微信用户首次登录界面, 首先保存userInfo, 跳入到微信注册登录界面
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getNickName
()))
{
String
nickName
=
EmojiUtil
.
filter
(
userInfo
.
getNickName
());
userInfo
.
setNickName
(
nickName
);
}
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
Constants
.
Channel
.
WECHAT
);
return
;
}
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
Constants
.
Channel
.
WECHAT
);
return
;
}
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
if
(
user
==
null
)
{
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
Constants
.
Channel
.
WECHAT
);
return
;
}
// 已经关联了用户
// create session, 登进去,该怎么玩怎么玩。
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
""
,
schema
,
Constants
.
Channel
.
WECHAT
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setStatus
(
301
);
}
else
{
}
else
{
HashMap
<
String
,
Object
>
extDataObj
;
HashMap
<
String
,
Object
>
extDataObj
;
try
{
try
{
...
@@ -309,56 +257,31 @@ public class WeChatController implements IBaseController {
...
@@ -309,56 +257,31 @@ public class WeChatController implements IBaseController {
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
LOGGER
.
error
(
"解析extData发生错误"
,
ex
);
LOGGER
.
error
(
"解析extData发生错误"
,
ex
);
// 从code获取token
// 从code获取token
Merchant
merchant
=
merchantService
.
findMerchantByName
(
systemKey
);
normalLogin
(
code
,
systemKey
,
response
,
schema
);
AccessTokenResponse
token
=
wechatService
.
getToken
(
code
);
if
(
token
==
null
)
{
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
return
;
}
}
WechatUserInfo
userInfo
=
selfDefinedLogin
(
code
,
systemKey
,
response
,
schema
,
extDataObj
);
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
token
.
getOpenId
());
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
}
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
// welcome 首次登录
if
(
userInfoInDb
==
null
)
{
// 微信用户首次登录界面, 首先保存userInfo, 跳入到微信注册登录界面
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getNickName
()))
{
String
nickName
=
EmojiUtil
.
filter
(
userInfo
.
getNickName
());
userInfo
.
setNickName
(
nickName
);
}
}
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
}
catch
(
Exception
ex
)
{
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
LOGGER
.
error
(
"发生异常"
,
ex
);
Constants
.
Channel
.
WECHAT
);
throw
ex
;
return
;
}
}
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
Constants
.
Channel
.
WECHAT
);
return
;
}
}
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
if
(
user
==
null
)
{
private
String
getExtData
(
@PathVariable
(
value
=
"extdata"
)
String
extData
)
{
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
try
{
Constants
.
Channel
.
WECHAT
);
extData
=
new
String
(
Base64
.
decodeBase64
(
extData
),
"UTF-8"
);
return
;
}
catch
(
Exception
ex
)
{
extData
=
""
;
}
}
// 已经关联了用户
return
extData
;
// create session, 登进去,该怎么玩怎么玩。
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
""
,
schema
,
Constants
.
Channel
.
WECHAT
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setStatus
(
301
);
return
;
}
}
private
boolean
selfDefinedLogin
(
String
code
,
String
systemKey
,
HttpServletResponse
response
,
String
schema
,
HashMap
<
String
,
Object
>
extDataObj
)
{
Long
registerFrom
=
Long
.
valueOf
(
extDataObj
.
getOrDefault
(
"registerFrom"
,
"1"
).
toString
());
Long
registerFrom
=
Long
.
valueOf
(
extDataObj
.
getOrDefault
(
"registerFrom"
,
"1"
).
toString
());
String
redirect
=
(
String
)
extDataObj
.
getOrDefault
(
"redirect"
,
"redirect"
);
String
redirect
=
(
String
)
extDataObj
.
getOrDefault
(
"redirect"
,
"redirect"
);
LOGGER
.
info
(
"从微信登录,registerFrom:{}, redirect:{}"
,
registerFrom
,
redirect
);
LOGGER
.
info
(
"从微信登录,registerFrom:{}, redirect:{}"
,
registerFrom
,
redirect
);
...
@@ -367,7 +290,7 @@ public class WeChatController implements IBaseController {
...
@@ -367,7 +290,7 @@ public class WeChatController implements IBaseController {
if
(
token
==
null
)
{
if
(
token
==
null
)
{
// 让用户登录,不关联微信, 构造不关联微信的url
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
registerFrom
);
redirectNormalUrl
(
response
,
merchant
,
registerFrom
);
return
;
return
true
;
}
}
WechatUserInfo
userInfo
=
WechatUserInfo
userInfo
=
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
...
@@ -375,7 +298,7 @@ public class WeChatController implements IBaseController {
...
@@ -375,7 +298,7 @@ public class WeChatController implements IBaseController {
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
// 让用户登录,不关联微信, 构造不关联微信的url
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
registerFrom
);
redirectNormalUrl
(
response
,
merchant
,
registerFrom
);
return
;
return
true
;
}
}
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
// welcome 首次登录
// welcome 首次登录
...
@@ -387,44 +310,28 @@ public class WeChatController implements IBaseController {
...
@@ -387,44 +310,28 @@ public class WeChatController implements IBaseController {
}
}
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
registerFrom
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
registerFrom
);
return
;
return
true
;
}
}
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
registerFrom
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
registerFrom
);
return
;
return
true
;
}
}
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
if
(
user
==
null
)
{
if
(
user
==
null
)
{
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
registerFrom
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
registerFrom
);
return
;
return
true
;
}
}
// 已经关联了用户
// 已经关联了用户
// create session, 登进去,该怎么玩怎么玩。
// create session, 登进去,该怎么玩怎么玩。
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
redirect
,
schema
,
registerFrom
);
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
redirect
,
schema
,
registerFrom
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setStatus
(
301
);
response
.
setStatus
(
301
);
}
return
false
;
}
catch
(
Exception
ex
)
{
LOGGER
.
error
(
"发生异常"
,
ex
);
throw
ex
;
}
}
private
boolean
normalLogin
(
String
code
,
String
systemKey
,
HttpServletResponse
response
,
String
schema
)
{
}
/**
* 通过redirect_url获取code
*
* @param response
* @return
*/
@RequestMapping
(
"/receiveCode/{key}"
)
public
void
receiveCodeNoRedirect
(
String
code
,
@PathVariable
(
value
=
"key"
)
String
systemKey
,
HttpServletRequest
request
,
HttpServletResponse
response
)
{
String
schema
=
request
.
getScheme
();
LOGGER
.
info
(
"HTTP协议no redirect:"
+
schema
);
// 从code获取token
// 从code获取token
Merchant
merchant
=
merchantService
.
findMerchantByName
(
systemKey
);
Merchant
merchant
=
merchantService
.
findMerchantByName
(
systemKey
);
...
@@ -432,49 +339,67 @@ public class WeChatController implements IBaseController {
...
@@ -432,49 +339,67 @@ public class WeChatController implements IBaseController {
if
(
token
==
null
)
{
if
(
token
==
null
)
{
// 让用户登录,不关联微信, 构造不关联微信的url
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
return
true
;
}
}
WechatUserInfo
userInfo
=
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
token
.
getOpenId
());
WechatUserInfo
userInfo
=
wechatService
.
getWechatUserInfoFromWechatServer
(
token
.
getAccessToken
(),
token
.
getOpenId
());
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
if
(
userInfo
==
null
||
StringUtils
.
isEmpty
(
userInfo
.
getOpenId
()))
{
// 让用户登录,不关联微信, 构造不关联微信的url
// 让用户登录,不关联微信, 构造不关联微信的url
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
redirectNormalUrl
(
response
,
merchant
,
Constants
.
Channel
.
WECHAT
);
return
;
return
true
;
}
}
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
WechatUserInfo
userInfoInDb
=
wechatService
.
findWechatUserInfoFromDb
(
userInfo
.
getOpenId
());
// welcome 首次登录
// welcome 首次登录
if
(
userInfoInDb
==
null
)
{
if
(
userInfoInDb
==
null
)
{
// 微信用户首次登录界面, 首先保存userInfo, 跳入到微信注册登录界面
// 微信用户首次登录界面, 首先保存userInfo, 跳入到微信注册登录界面
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getNickName
()))
{
if
(
StringUtils
.
isNotBlank
(
userInfo
.
getNickName
()))
{
String
nickName
=
EmojiUtil
.
filter
(
userInfo
.
getNickName
());
String
nickName
=
EmojiUtil
.
filter
(
userInfo
.
getNickName
());
userInfo
.
setNickName
(
nickName
);
userInfo
.
setNickName
(
nickName
);
}
}
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
userInfo
=
wechatService
.
saveWechatUserInfo
(
userInfo
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
Constants
.
Channel
.
WECHAT
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfo
,
return
;
Constants
.
Channel
.
WECHAT
);
return
true
;
}
}
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
if
(
userInfoInDb
.
getUserId
()
==
null
)
{
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
// 用户已经微信登录了,但是没有关联信用钱包,跳转到注册页面
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
Constants
.
Channel
.
WECHAT
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
return
;
Constants
.
Channel
.
WECHAT
);
return
true
;
}
}
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
User
user
=
userService
.
findById
(
userInfoInDb
.
getUserId
());
if
(
user
==
null
)
{
if
(
user
==
null
)
{
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
Constants
.
Channel
.
WECHAT
);
redirectWechatLoginUrlWithoutLogin
(
response
,
merchant
,
userInfoInDb
,
return
;
Constants
.
Channel
.
WECHAT
);
return
true
;
}
}
// 已经关联了用户
// 已经关联了用户
// create session, 登进去,该怎么玩怎么玩。
// create session, 登进去,该怎么玩怎么玩。
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
""
,
schema
,
Constants
.
Channel
.
WECHAT
);
String
redirectUrl
=
createUserSession
(
user
,
merchant
,
""
,
schema
,
Constants
.
Channel
.
WECHAT
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setHeader
(
"Location"
,
redirectUrl
);
response
.
setStatus
(
301
);
response
.
setStatus
(
301
);
return
false
;
}
/**
* 通过redirect_url获取code
*
* @param response
* @return
*/
@RequestMapping
(
"/receiveCode/{key}"
)
public
void
receiveCodeNoRedirect
(
String
code
,
@PathVariable
(
value
=
"key"
)
String
systemKey
,
HttpServletRequest
request
,
HttpServletResponse
response
)
{
String
schema
=
request
.
getScheme
();
LOGGER
.
info
(
"HTTP协议no redirect:"
+
schema
);
// 从code获取token
normalLogin
(
code
,
systemKey
,
response
,
schema
);
}
}
private
String
createUserSession
(
User
user
,
Merchant
merchant
,
String
redirect
,
String
schema
,
Long
registerFrom
)
{
private
String
createUserSession
(
User
user
,
Merchant
merchant
,
String
redirect
,
String
schema
,
Long
registerFrom
)
{
//TODO 临时紧急上线修改的.下次上线的时候修复一下,不能这样写.
//TODO 临时紧急上线修改的.下次上线的时候修复一下,不能这样写.
String
url
=
"http://passport.xyqb.com"
;
String
url
=
schema
+
"//passport.xyqb.com"
;
if
(
"https:"
.
equals
(
schema
))
{
url
=
"https://passport.xyqb.com"
;
}
if
(
StringUtils
.
isEmpty
(
redirect
)
||
"redirect"
.
equals
(
redirect
))
{
if
(
StringUtils
.
isEmpty
(
redirect
)
||
"redirect"
.
equals
(
redirect
))
{
LOGGER
.
info
(
"微信登录:redirect为null,走正常流程."
);
LOGGER
.
info
(
"微信登录:redirect为null,走正常流程."
);
if
(
"baitiao"
.
equals
(
merchant
.
getName
()))
{
if
(
"baitiao"
.
equals
(
merchant
.
getName
()))
{
...
...
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