UserController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.hssx.controller;
  2. import java.io.IOException;
  3. import java.security.KeyManagementException;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.NoSuchProviderException;
  6. import java.util.HashMap;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.commons.httpclient.HttpClient;
  9. import org.apache.commons.httpclient.methods.GetMethod;
  10. import org.apache.commons.httpclient.methods.PostMethod;
  11. import org.apache.commons.lang.StringEscapeUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONObject;
  18. import com.hssx.constant.Constant;
  19. import com.hssx.entity.User;
  20. import com.hssx.entity.UserExample;
  21. import com.hssx.mapper.UserMapper;
  22. import com.hssx.utils.HttpKit;
  23. import com.hssx.utils.HttpRespMsg;
  24. import com.sun.javafx.collections.MappingChange.Map;
  25. @Controller
  26. @RequestMapping("/user")
  27. public class UserController {
  28. @Autowired
  29. UserMapper usermapper;
  30. /**
  31. * 微信授权登录 参数: type:授权类型,0-微信,1-微博 code:平台返回的code值
  32. *
  33. * @return
  34. */
  35. @RequestMapping(value = "weiXinLogin")
  36. public void weiXinLogin(@RequestParam String code, @RequestParam Integer type, HttpServletResponse response)
  37. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  38. HttpRespMsg msg = new HttpRespMsg();
  39. User user = new User();
  40. user.setType(type);
  41. UserExample example = new UserExample();
  42. if (type == 0) {
  43. String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.WECHAT_APPID + "&secret="
  44. + Constant.WECHAT_APPSECRET + "&code=" + code + "&grant_type=authorization_code";
  45. String resp = HttpKit.get(url, true);
  46. resp = StringEscapeUtils.unescapeJava(resp);
  47. System.out.println(resp);
  48. JSONObject json = (JSONObject) JSON.parse(resp);
  49. if (!json.containsKey("errcode")) {
  50. String openId = json.getString("openid");
  51. String accessToken = json.getString("access_token");
  52. user.setVoucherId(openId);
  53. String url1 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId
  54. + "&lang=zh_CN";
  55. // 获取用户基本信息
  56. resp = HttpKit.get(url1, true);
  57. resp = StringEscapeUtils.unescapeJava(resp);
  58. System.out.println(resp);
  59. json = (JSONObject) JSON.parse(resp);
  60. if (!json.containsKey("errcode")) {
  61. user.setNickName(json.getString("nickname"));
  62. user.setHeaderPic(json.getString("headimgurl"));
  63. }
  64. System.out.println(user + "user");
  65. example.createCriteria().andVoucherIdEqualTo(openId).andTypeEqualTo(type);
  66. if (usermapper.countByExample(example) == 0) {
  67. usermapper.insert(user);
  68. } else {
  69. // 列表中已包含当前用户,
  70. user = usermapper.selectByExample(example).get(0);
  71. if (null == user.getNickName() && null == user.getHeaderPic()) {
  72. user.setNickName(json.getString("nickname"));
  73. user.setHeaderPic(json.getString("headimgurl"));
  74. usermapper.updateByPrimaryKeySelective(user);
  75. }
  76. }
  77. msg.data = user;
  78. } else {
  79. msg.setError(json.getString("errmsg"));
  80. }
  81. } else if (type == 1) {
  82. // String url2 =
  83. // "https://api.weibo.com/oauth2/access_token?client_id=" +
  84. // Constant.MICROBLOG_APPKEY
  85. // + "&client_secret=" + Constant.MICROBLOG_APPSECRET +
  86. // "&grant_type=authoriz";
  87. // HashMap<String, String> token = new HashMap<String, String>();
  88. // 本机运行时会报证书错误
  89. /*
  90. * ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
  91. * Protocol.registerProtocol("https", new Protocol("https", fcty,
  92. * 443));
  93. */
  94. PostMethod postMethod = new PostMethod("https://api.weibo.com/oauth2/access_token");
  95. postMethod.addParameter("grant_type", "authorization_code");
  96. postMethod.addParameter("code", code);
  97. postMethod.addParameter("redirect_uri", Constant.CALLBACKURL);
  98. postMethod.addParameter("client_id", Constant.MICROBLOG_APPKEY);
  99. postMethod.addParameter("client_secret", Constant.MICROBLOG_APPSECRET);
  100. HttpClient client = new HttpClient();
  101. try {
  102. client.executeMethod(postMethod);
  103. String responseDate = postMethod.getResponseBodyAsString();
  104. if (!responseDate.equals("") && responseDate.indexOf("access_token") != -1) {
  105. System.out.println("responseDate=======>"+responseDate);
  106. JSONObject jsonData = JSONObject.parseObject(responseDate);
  107. System.out.println("jsonData===>"+jsonData);
  108. String uid = jsonData.getString("uid");
  109. user.setVoucherId(uid);
  110. String accessToken = jsonData.getString("access_token");
  111. String url = "https://api.weibo.com/2/users/show.json?access_token=" + accessToken + "&uid=" + uid;
  112. GetMethod getMethod = new GetMethod(url);
  113. client = new HttpClient();
  114. try {
  115. client.executeMethod(getMethod);
  116. responseDate = getMethod.getResponseBodyAsString();
  117. jsonData = JSONObject.parseObject(responseDate);
  118. System.out.println("返回User jsonData===>"+jsonData);
  119. user.setNickName(jsonData.getString("name"));
  120. user.setHeaderPic(jsonData.getString("profile_image_url"));
  121. example.createCriteria().andVoucherIdEqualTo(uid).andTypeEqualTo(type);
  122. if (usermapper.countByExample(example) == 0) {
  123. usermapper.insert(user);
  124. } else {
  125. // 列表中已包含当前用户,
  126. user = usermapper.selectByExample(example).get(0);
  127. if (null == user.getNickName() && null == user.getHeaderPic()) {
  128. user.setNickName(jsonData.getString("name"));
  129. user.setHeaderPic(jsonData.getString("profile_image_url"));
  130. usermapper.updateByPrimaryKeySelective(user);
  131. }
  132. }
  133. System.out.println("微博user----->"+user);
  134. msg.data = user;
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. response.setContentType("application/json");
  144. response.setCharacterEncoding("UTF-8");
  145. response.getWriter().println(msg.toJSONStr());
  146. }
  147. }