package com.github.ouyangbob.wechat.controller;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.ouyangbob.request.RequestUtil;
import com.github.ouyangbob.wechat.WechatConstants;
import com.github.ouyangbob.wechat.authorize.WechatAuthorizeUtil;
import com.github.ouyangbob.wechat.qrcode.CheckQRCodeUtil;
import com.github.ouyangbob.wechat.service.IWechatUserService;
import com.github.ouyangbob.wechat.token.WechatAccessTokenUtil;
import com.github.ouyangbob.wechat.vo.UserInfo;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
@Controller
@RequestMapping("/wechat")
public class WechatAuthorizeController {
@Autowired
Properties properties;
@Autowired
private IWechatUserService wechatUserService;
@RequestMapping("index")
public String index(){
return "wechat/index";
}
@RequestMapping("authorize")
public String authorize(){
String redirectUrl=WechatAuthorizeUtil.generateAuthorizeUrl(properties.get("url.prefix").toString().concat("/wechat/authorize-callback.sc"));
return "redirect:".concat(redirectUrl);
}
@RequestMapping("authorize-callback")
public String authorizeCallback(String code,ModelMap modelMap){
UserInfo userInfo=WechatAuthorizeUtil.getUserInfo(WechatAuthorizeUtil.getAccessToken(code));
modelMap.put("userInfo", userInfo);
wechatUserService.saveAndFindWechatUser(userInfo);
return "wechat/userinfo";
}
@RequestMapping("test")
public String test(String code,ModelMap modelMap){
UserInfo userInfo=new UserInfo();
userInfo.setOpenId("omRivt-h26xTgmlFZGWNULdiQGvo");
userInfo.setNickName("大欧");
userInfo.setSex((short)1);
userInfo.setCity("朝阳");
userInfo.setProvince("北京");
userInfo.setCountry("中国");
userInfo.setHeadImgUrl("http://wx.qlogo.cn/mmopen/dIEdYQym9wPiam79kAkdxDNOjTruOOGTia07ia6ec8zEoZAnyS9m2iaxAfft6AwMCibVdzq3BwbdNFgnvH8iau4tBw4w/0");
modelMap.put("userInfo", userInfo);
wechatUserService.saveAndFindWechatUser(userInfo);
return "wechat/userinfo";
}
@RequestMapping("qrcode")
public String qrcode(HttpServletRequest request,ModelMap modelMap){
String noncestr=RandomStringUtils.randomAlphanumeric(5);
long timestamp=System.currentTimeMillis()/1000;
String jsapiTicket=WechatAccessTokenUtil.getJsapiTicket();
String url=request.getRequestURL().toString();
StringBuilder sb=new StringBuilder();
sb.append("jsapi_ticket=");
sb.append(jsapiTicket);
sb.append("&noncestr=");
sb.append(noncestr);
sb.append("×tamp=");
sb.append(timestamp);
sb.append("&url=");
sb.append(url);
String signature = Hashing.sha1().hashString(sb.toString(), Charsets.UTF_8).toString();
modelMap.put("noncestr", noncestr);
modelMap.put("timestamp", timestamp);
modelMap.put("jsapiTicket", jsapiTicket);
modelMap.put("appId", WechatConstants.APPID);
modelMap.put("signature", signature);
return "wechat/qrcode";
}
@ResponseBody
@RequestMapping("qrcode-check")
public String qrcodeCheck(String code,HttpServletRequest request,ModelMap modelMap){
String ip=RequestUtil.getIpAddr(request);
String msg=CheckQRCodeUtil.check(code, ip);
return msg;
}
}