package com.xtwl.reporter.weixin;import java.lang.reflect.Method;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.ui.Model;import org.springframework.web.servlet.mvc.annotation.IMethodIntercepterHolder;import org.springframework.web.servlet.mvc.annotation.IMethodInvokerIntercepter;import com.jfinal.kit.PropKit;import com.jfinal.weixin.sdk.api.ApiConfig;import com.jfinal.weixin.sdk.api.ApiConfigKit;import com.jfinal.weixin.sdk.api.SnsAccessToken;import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;import com.xtwl.reporter.business.StudentService;import com.xtwl.reporter.domain.Student;import com.xtwl.water.business.AdminInfoService;import com.xtwl.water.domain.AdminInfo;@Componentpublic
class
NeedOpenId
implements
IMethodInvokerIntercepter { @Autowired
private
AdminInfoService adminInfoService; @Autowired
private
StudentService studentService; @Override
public
Object invokeHandlerMethod(Method handlerMethod, Object handler,
HttpServletRequest request, HttpServletResponse response,
Model model, IMethodIntercepterHolder chain) throws Exception {
if
(handlerMethod.isAnnotationPresent(Openid.
class
)) {
ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
Openid access = handlerMethod.getAnnotation(Openid.
class
);
if
(access != null) {
System.out.println(
"需要检查openid"
);
String openid=
"oWDxdt8F5UTP8L3XV-KcmZdLmP2Q"
;
System.out.println(
"session中的openid 为:"
+ openid);
System.out.println(request.getRequestURL()+
"=======request.getRequestURL()2"
);
if
(openid != null && openid.length() > 5) {
return
todo(chain, handlerMethod, handler, request,
response, model, openid);
}
if
(openid == null || openid.length() < 5) {
String url = SnsAccessTokenApi.getAuthorizeURL(ApiConfigKit
.getApiConfig().getAppId(), request.getRequestURL().toString(), true);
String code = (String) request.getParameter(
"code"
);
System.out.println(
"code为:"
+ code +
" 这是微信返回的请求"
);
if
(code != null && code.length() > 1) {
SnsAccessToken sn = SnsAccessTokenApi
.getSnsAccessToken(ApiConfigKit.getApiConfig().getAppId(),ApiConfigKit.getApiConfig().getAppSecret(), code);
System.out.println(
"微信返回的openid:"
+ sn.getOpenid());
WeiXinSession.setOpenid(sn.getOpenid());
return
todo(chain, handlerMethod, handler, request,
response, model, sn.getOpenid());
}
else
{
System.out.println(
"重定向到微信获取openid:"
+ url);
return
"redirect:"
+ url;
}
}
}
}
return
chain.doChain(handlerMethod, handler, request, response, model);
}
private
Object todo(IMethodIntercepterHolder chain, Method handlerMethod,
Object handler, HttpServletRequest request,
HttpServletResponse response, Model model, String openid)
throws Exception {
try
{
System.out.println(request.getRequestURL()+
"=======request.getRequestURL()1"
);
if
(request.getRequestURL().toString().indexOf(
"/mobile/post_bind/"
)>0){
return
chain.doChain(handlerMethod, handler, request, response, model);
}
AdminInfo admin = adminInfoService.getItemByOpenid(openid);
System.out.println(admin +
"========数据库中的admin"
);
if
(admin == null || admin.getId() == null) {
System.out.println(
"redirect:/mobile/bind_phone"
);
return
"redirect:/mobile/bind_phone"
;
}
else
{
Student s = studentService.getItemById(admin.getOtherid());
WeiXinSession.setWeiXinAdminInfo(admin);
WeiXinSession.setWeiXinStudent(s);
}
return
chain.doChain(handlerMethod, handler, request, response, model);
}
catch
(Exception e) {
e.printStackTrace();
return
chain.doChain(handlerMethod, handler, request, response, model);
}
}
public
ApiConfig getApiConfig() {
ApiConfig ac =
new
ApiConfig();
ac.setToken(PropKit.get(
"token"
));
ac.setAppId(PropKit.get(
"appId"
));
ac.setAppSecret(PropKit.get(
"appSecret"
));
ac.setEncryptMessage(PropKit.getBoolean(
"encryptMessage"
, false));
ac.setEncodingAesKey(PropKit.get(
"encodingAesKey"
,
"setting it in config file"
));
return
ac;
}
}