Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Z
zhny
Project
Project
Details
Activity
Releases
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
王夏晖
zhny
Commits
162461e8
Commit
162461e8
authored
May 24, 2018
by
张大伟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加微信授权
parent
60eaa8ce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
0 deletions
+98
-0
WechatRedirectController.java
...ain/java/org/rcisoft/wechat/WechatRedirectController.java
+37
-0
WxPortalService.java
...main/java/org/rcisoft/wechat/service/WxPortalService.java
+61
-0
No files found.
src/main/java/org/rcisoft/wechat/WechatRedirectController.java
0 → 100644
View file @
162461e8
package
org
.
rcisoft
.
wechat
;
import
me.chanjar.weixin.mp.bean.result.WxMpUser
;
import
org.rcisoft.wechat.service.WxPortalService
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.servlet.ModelAndView
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletResponse
;
/**
* 网页授权微信回调接口
* @author David
*/
@Controller
@RequestMapping
(
"/wechat"
)
public
class
WechatRedirectController
{
@Resource
private
WxPortalService
service
;
@RequestMapping
(
value
=
{
"/index"
})
public
String
index
(
String
code
)
{
try
{
WxMpUser
wxUser
=
this
.
service
.
oauth2UserInfo
(
code
);
System
.
out
.
println
(
"微信返回的code: "
+
code
);
System
.
out
.
println
(
"WxPortalController授权完毕后获取到的OpenId是:"
+
wxUser
.
getOpenId
());
return
"redirect:http://weega.cn.tunnel.qydev.com/login.html?openId="
+
wxUser
.
getOpenId
();
}
catch
(
Exception
exception
)
{
exception
.
printStackTrace
();
return
"redirect:http://weega.cn.tunnel.qydev.com/error.html"
;
}
}
}
src/main/java/org/rcisoft/wechat/service/WxPortalService.java
0 → 100644
View file @
162461e8
package
org
.
rcisoft
.
wechat
.
service
;
import
me.chanjar.weixin.common.bean.WxJsapiSignature
;
import
me.chanjar.weixin.common.exception.WxErrorException
;
import
me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage
;
import
me.chanjar.weixin.mp.api.impl.WxMpServiceImpl
;
import
me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken
;
import
me.chanjar.weixin.mp.bean.result.WxMpUser
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.PostConstruct
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* 获取微信用户信息
* @author david
*/
@Service
public
class
WxPortalService
extends
WxMpServiceImpl
{
@Value
(
"${wechat.mp.appId}"
)
String
appId
;
@Value
(
"${wechat.mp.secret}"
)
String
secret
;
@Value
(
"${wechat.mp.token}"
)
String
token
;
@Value
(
"${wechat.mp.aesKey}"
)
String
aesKey
;
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
this
.
getClass
());
public
WxPortalService
()
{
}
@PostConstruct
public
void
init
()
{
WxMpInMemoryConfigStorage
config
=
new
WxMpInMemoryConfigStorage
();
config
.
setAppId
(
appId
);
config
.
setSecret
(
secret
);
config
.
setToken
(
token
);
config
.
setAesKey
(
aesKey
);
super
.
setWxMpConfigStorage
(
config
);
}
public
WxMpOAuth2AccessToken
oauth2AccessToken
(
String
code
)
throws
WxErrorException
{
WxMpOAuth2AccessToken
token
=
this
.
oauth2getAccessToken
(
code
);
System
.
out
.
println
(
"token:"
+
token
.
getAccessToken
()
+
"; code: "
+
code
);
return
token
;
}
public
WxMpUser
oauth2UserInfo
(
String
code
)
throws
WxErrorException
{
WxMpOAuth2AccessToken
token
=
this
.
oauth2AccessToken
(
code
);
return
token
!=
null
?
this
.
oauth2getUserInfo
(
token
,
(
String
)
null
):
null
;
}
public
WxJsapiSignature
getJsApiTicket
(
String
url
)
throws
WxErrorException
{
return
this
.
createJsapiSignature
(
url
);
}
}
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