NewsController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Date;
  7. import java.util.UUID;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.codec.digest.DigestUtils;
  10. import org.apache.commons.lang.StringEscapeUtils;
  11. import org.springframework.beans.BeanUtils;
  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.ResponseBody;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONObject;
  18. import com.hssx.constant.Constant;
  19. import com.hssx.entity.News;
  20. import com.hssx.entity.User;
  21. import com.hssx.entity.WeixinShare;
  22. import com.hssx.entity.vo.NewsVO;
  23. import com.hssx.mapper.NewsMapper;
  24. import com.hssx.mapper.UserMapper;
  25. import com.hssx.service.NewsService;
  26. import com.hssx.utils.HttpKit;
  27. import com.hssx.utils.HttpRespMsg;
  28. /**
  29. * @author 吴涛涛
  30. * @since 2019-09-17
  31. */
  32. @Controller
  33. @RequestMapping("/news")
  34. public class NewsController {
  35. @Autowired
  36. NewsMapper newsMapper;
  37. @Autowired
  38. NewsService newsService;
  39. @Autowired
  40. UserMapper usermapper;
  41. /**
  42. * 添加分享信息
  43. *
  44. * 传递的参数:
  45. * userId:用户id message:寄语
  46. * @return
  47. * @throws IOException
  48. */
  49. @RequestMapping("/addNews")
  50. public void addUserNews(NewsVO newsVO,HttpServletResponse response) throws IOException{
  51. HttpRespMsg msg = new HttpRespMsg();
  52. User user = usermapper.selectByPrimaryKey(newsVO.getUserId());
  53. // 微信授权的链接
  54. try {
  55. News news = new News();
  56. BeanUtils.copyProperties(newsVO, news);
  57. news.setNickName(user.getNickName());
  58. news.setHeaderPic(user.getHeaderPic());
  59. news.setType(user.getType());
  60. newsMapper.insert(news);
  61. msg.data = news;
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. response.setContentType("application/json");
  66. response.setCharacterEncoding("UTF-8");
  67. response.getWriter().println(msg.toJSONStr());
  68. }
  69. /**
  70. * 添加分享信息
  71. *
  72. * 传递的参数:
  73. * id:海报消息id
  74. * @return
  75. * @throws IOException
  76. */
  77. @RequestMapping("/show")
  78. public void showUserNews(News news,HttpServletResponse response) throws IOException{
  79. HttpRespMsg msg = new HttpRespMsg();
  80. news = newsMapper.selectByPrimaryKey(news.getId());
  81. msg.data = news;
  82. response.setContentType("application/json");
  83. response.setCharacterEncoding("UTF-8");
  84. response.getWriter().println(msg.toJSONStr());
  85. }
  86. /**
  87. * 分享链接返回给前端的参数
  88. * url 这里的URL指的是需要分享的那个页面地址,建议这里不要写成固定地址,而是获取当前地址.
  89. * 传递的参数:
  90. * @return
  91. * @throws NoSuchProviderException
  92. * @throws NoSuchAlgorithmException
  93. * @throws KeyManagementException
  94. * @throws IOException
  95. */
  96. @RequestMapping("/shareLinks")
  97. public void shareLinks(String url,HttpServletResponse response) throws Exception{
  98. HttpRespMsg msg = new HttpRespMsg();
  99. // 创建通过Api获取Token的链接与参数
  100. // String requestTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
  101. // requestTokenUrl = requestTokenUrl.replace("APPID", appid);
  102. // requestTokenUrl = requestTokenUrl.replace("SECRET", secret);
  103. // String resp = HttpKit.get(requestTokenUrl, true);
  104. // resp = StringEscapeUtils.unescapeJava(resp);
  105. // System.out.println(resp);
  106. // JSONObject jsonObjectToken = (JSONObject) JSON.parse(resp);
  107. //获取access_token
  108. // String newAccessToken = AccessToken.getNewAccessToken();
  109. String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Constant.WECHAT_APPID + "&secret=" + Constant.WECHAT_APPSECRET;
  110. String resp = HttpKit.get(url1, true);
  111. resp = StringEscapeUtils.unescapeJava(resp);
  112. JSONObject json = (JSONObject) JSON.parse(resp);
  113. //获取值赋值给全局变量
  114. String newAccessToken="";
  115. if (!json.containsKey("errcode")) {
  116. newAccessToken = json.getString("access_token");
  117. String newExpiresIn = json.getString("expires_in");
  118. }
  119. Long createDate = new Date().getTime()/1000;// 创建日期赋值为当前日期
  120. // if(StrKit.notNull(jsonObjectToken)){
  121. // // 创建日期赋值为当前日期
  122. // Long createDate = new Date().getTime()/1000;
  123. // // 获取Token值
  124. // access_token = jsonObjectToken.getString("access_token");
  125. // // 获取Token有效期值
  126. // expires_in = jsonObjectToken.getLong("expires_in");
  127. // }
  128. String requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
  129. requestUrl = requestUrl.replace("ACCESS_TOKEN", newAccessToken);
  130. // 获取凭证
  131. resp = HttpKit.get(requestUrl, true);
  132. resp = StringEscapeUtils.unescapeJava(resp);
  133. System.out.println(resp);
  134. json = (JSONObject) JSON.parse(resp);
  135. // JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
  136. if(!"".equals(json)){
  137. try {
  138. String ticket = json.getString("ticket");
  139. String nonceStr = UUID.randomUUID().toString().replaceAll("-", "");
  140. Long timestamp = new Date().getTime()/1000;
  141. String param = "jsapi_ticket="+ticket+"&noncestr="+nonceStr+"×tamp="+timestamp+"&url="+url;
  142. String signature = DigestUtils.shaHex(param);
  143. WeixinShare weixinShare = new WeixinShare();
  144. weixinShare.setNonceStr(nonceStr);;
  145. weixinShare.setTimestamp(timestamp);
  146. weixinShare.setSignature(signature);
  147. msg.data = weixinShare;
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. msg.setError("服务器异常!");
  151. }
  152. }
  153. response.setContentType("application/json");
  154. response.setCharacterEncoding("UTF-8");
  155. response.getWriter().println(msg.toJSONStr());
  156. }
  157. }