|
@@ -5,21 +5,30 @@ import java.io.IOException;
|
|
import java.security.KeyManagementException;
|
|
import java.security.KeyManagementException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchProviderException;
|
|
import java.security.NoSuchProviderException;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
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.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
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.News;
|
|
import com.hssx.entity.User;
|
|
import com.hssx.entity.User;
|
|
|
|
+import com.hssx.entity.WeixinShare;
|
|
import com.hssx.entity.vo.NewsVO;
|
|
import com.hssx.entity.vo.NewsVO;
|
|
import com.hssx.mapper.NewsMapper;
|
|
import com.hssx.mapper.NewsMapper;
|
|
import com.hssx.mapper.UserMapper;
|
|
import com.hssx.mapper.UserMapper;
|
|
import com.hssx.service.NewsService;
|
|
import com.hssx.service.NewsService;
|
|
|
|
+import com.hssx.utils.HttpKit;
|
|
import com.hssx.utils.HttpRespMsg;
|
|
import com.hssx.utils.HttpRespMsg;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -85,7 +94,79 @@ public class NewsController {
|
|
response.getWriter().println(msg.toJSONStr());
|
|
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());
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|