преди 5 години
родител
ревизия
8f442e1327

+ 83 - 2
minigame/src/com/hssx/controller/NewsController.java

@@ -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());
+      }
+    	
 }
 }
 
 

+ 47 - 0
minigame/src/com/hssx/entity/WeixinShare.java

@@ -0,0 +1,47 @@
+package com.hssx.entity;
+
+public class WeixinShare {
+	
+	private String appId;
+	
+	private Long timestamp;
+	
+	private String nonceStr;
+	
+	private String signature;
+
+	public String getAppId() {
+		return appId;
+	}
+
+	public void setAppId(String appId) {
+		this.appId = appId;
+	}
+
+	public Long getTimestamp() {
+		return timestamp;
+	}
+
+	public void setTimestamp(Long timestamp) {
+		this.timestamp = timestamp;
+	}
+
+	public String getNonceStr() {
+		return nonceStr;
+	}
+
+	public void setNonceStr(String nonceStr) {
+		this.nonceStr = nonceStr;
+	}
+
+	public String getSignature() {
+		return signature;
+	}
+
+	public void setSignature(String signature) {
+		this.signature = signature;
+	}
+	
+	
+
+}

+ 0 - 1
minigame/src/com/hssx/service/impl/NewsServiceImpl.java

@@ -21,7 +21,6 @@ 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.AccessToken;
 import com.hssx.utils.HttpKit;
 import com.hssx.utils.HttpKit;
 import com.hssx.utils.HttpRespMsg;
 import com.hssx.utils.HttpRespMsg;
 import com.hssx.utils.WechatAndMicroblogUtil;
 import com.hssx.utils.WechatAndMicroblogUtil;

+ 0 - 51
minigame/src/com/hssx/utils/AccessToken.java

@@ -1,51 +0,0 @@
-package com.hssx.utils;
-
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.hssx.constant.Constant;
-
-import java.io.IOException;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
-import java.security.NoSuchProviderException;
-import java.util.Date;
-
-import org.apache.commons.lang.StringEscapeUtils;
-
-/**
- * Author: 吴涛涛 cuiyi@itany.com
- * Date : 2019 - 09 - 17 10:04
- * Description:<描述>
- * Version: 1.0
- */
-public class AccessToken {
-
-    private static String wechatAppId = Constant.WECHAT_APPID;
-
-    private static String wechatSecret = Constant.WECHAT_APPSECRET;
-
-    private static String oldAccessToken = null;
-
-    private static Date time = new Date();//oldAccessToken值创建时间
-
-    private static Long expiresIn = 7200L*1000;//accessToken值有效时长 单位毫秒
-
-    public static String getNewAccessToken() throws Exception {
-        if(oldAccessToken == null || (oldAccessToken != null && System.currentTimeMillis() > time.getTime()+expiresIn)){
-            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wechatAppId + "&secret=" + wechatSecret;
-            String resp = HttpKit.get(url, true);
-            resp = StringEscapeUtils.unescapeJava(resp);
-            JSONObject json = (JSONObject) JSON.parse(resp);
-            //获取值赋值给全局变量
-            if (!json.containsKey("errcode")) {
-                String newAccessToken = json.getString("access_token");
-                String newExpiresIn = json.getString("expires_in");
-                oldAccessToken = newAccessToken;
-                expiresIn = Long.parseLong(newExpiresIn) * 1000;
-                time = new Date();
-                System.out.println("获取新access_token==>" + oldAccessToken);
-            }
-        }
-        return oldAccessToken;
-    }
-}