123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package com.hssx.controller;
- import java.io.IOException;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.NoSuchProviderException;
- import java.util.Date;
- import java.util.UUID;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.codec.digest.DigestUtils;
- import org.apache.commons.lang.StringEscapeUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.hssx.constant.Constant;
- import com.hssx.entity.News;
- import com.hssx.entity.User;
- import com.hssx.entity.WeixinShare;
- import com.hssx.entity.vo.NewsVO;
- import com.hssx.mapper.NewsMapper;
- import com.hssx.mapper.UserMapper;
- import com.hssx.service.NewsService;
- import com.hssx.utils.HttpKit;
- import com.hssx.utils.HttpRespMsg;
- /**
- * @author 吴涛涛
- * @since 2019-09-17
- */
- @Controller
- @RequestMapping("/news")
- public class NewsController {
- @Autowired
- NewsMapper newsMapper;
- @Autowired
- NewsService newsService;
- @Autowired
- UserMapper usermapper;
-
- /**
- * 添加分享信息
- *
- * 传递的参数:
- * userId:用户id message:寄语
- * @return
- * @throws IOException
- */
- @RequestMapping("/addNews")
- public void addUserNews(NewsVO newsVO,HttpServletResponse response) throws IOException{
- HttpRespMsg msg = new HttpRespMsg();
- User user = usermapper.selectByPrimaryKey(newsVO.getUserId());
- // 微信授权的链接
- try {
- News news = new News();
- BeanUtils.copyProperties(newsVO, news);
- news.setNickName(user.getNickName());
- news.setHeaderPic(user.getHeaderPic());
- news.setType(user.getType());
- newsMapper.insert(news);
- msg.data = news;
- } catch (Exception e) {
- e.printStackTrace();
- }
- response.setContentType("application/json");
- response.setCharacterEncoding("UTF-8");
- response.getWriter().println(msg.toJSONStr());
- }
-
- /**
- * 添加分享信息
- *
- * 传递的参数:
- * id:海报消息id
- * @return
- * @throws IOException
- */
- @RequestMapping("/show")
- public void showUserNews(News news,HttpServletResponse response) throws IOException{
- HttpRespMsg msg = new HttpRespMsg();
- news = newsMapper.selectByPrimaryKey(news.getId());
- msg.data = news;
- response.setContentType("application/json");
- response.setCharacterEncoding("UTF-8");
- response.getWriter().println(msg.toJSONStr());
- }
-
- /**
- * 分享链接返回给前端的参数
- * url 这里的URL指的是需要分享的那个页面地址,建议这里不要写成固定地址,而是获取当前地址.
- * 传递的参数:
- * @return
- * @throws NoSuchProviderException
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws IOException
- */
- @RequestMapping("/shareLinks")
- public void shareLinks(String url,HttpServletResponse response) throws Exception{
- HttpRespMsg msg = new HttpRespMsg();
- // 创建通过Api获取Token的链接与参数
- // String requestTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
- // requestTokenUrl = requestTokenUrl.replace("APPID", appid);
- // requestTokenUrl = requestTokenUrl.replace("SECRET", secret);
- // String resp = HttpKit.get(requestTokenUrl, true);
- // resp = StringEscapeUtils.unescapeJava(resp);
- // System.out.println(resp);
- // JSONObject jsonObjectToken = (JSONObject) JSON.parse(resp);
- //获取access_token
- // String newAccessToken = AccessToken.getNewAccessToken();
-
- String url1 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Constant.WECHAT_APPID + "&secret=" + Constant.WECHAT_APPSECRET;
- String resp = HttpKit.get(url1, true);
- resp = StringEscapeUtils.unescapeJava(resp);
- JSONObject json = (JSONObject) JSON.parse(resp);
- //获取值赋值给全局变量
- String newAccessToken="";
- if (!json.containsKey("errcode")) {
- newAccessToken = json.getString("access_token");
- String newExpiresIn = json.getString("expires_in");
- }
- Long createDate = new Date().getTime()/1000;// 创建日期赋值为当前日期
- // if(StrKit.notNull(jsonObjectToken)){
- // // 创建日期赋值为当前日期
- // Long createDate = new Date().getTime()/1000;
- // // 获取Token值
- // access_token = jsonObjectToken.getString("access_token");
- // // 获取Token有效期值
- // expires_in = jsonObjectToken.getLong("expires_in");
- // }
- String requestUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
- requestUrl = requestUrl.replace("ACCESS_TOKEN", newAccessToken);
- // 获取凭证
- resp = HttpKit.get(requestUrl, true);
- resp = StringEscapeUtils.unescapeJava(resp);
- System.out.println(resp);
- json = (JSONObject) JSON.parse(resp);
- // JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
- if(!"".equals(json)){
- try {
- String ticket = json.getString("ticket");
- String nonceStr = UUID.randomUUID().toString().replaceAll("-", "");
- Long timestamp = new Date().getTime()/1000;
- String param = "jsapi_ticket="+ticket+"&noncestr="+nonceStr+"×tamp="+timestamp+"&url="+url;
- String signature = DigestUtils.shaHex(param);
- WeixinShare weixinShare = new WeixinShare();
- weixinShare.setNonceStr(nonceStr);;
- weixinShare.setTimestamp(timestamp);
- weixinShare.setSignature(signature);
- msg.data = weixinShare;
- } catch (Exception e) {
- e.printStackTrace();
- msg.setError("服务器异常!");
- }
- }
- response.setContentType("application/json");
- response.setCharacterEncoding("UTF-8");
- response.getWriter().println(msg.toJSONStr());
- }
-
- }
|