Explorar o código

Merge branch 'master' of http://47.100.37.243:10080/wutt/game

sunyadv %!s(int64=5) %!d(string=hai) anos
pai
achega
ead17ffcae

+ 87 - 7
minigame/src/com/hssx/controller/NewsController.java

@@ -5,21 +5,30 @@ 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;
 
 /**
@@ -41,8 +50,8 @@ public class NewsController {
     /**
      * 添加分享信息
      *
-     * 传递的参数:
-     * 		userId:用户id message:寄语
+     * 传递的参数:userId:用户id message:寄语
+     *
      * @return
      * @throws IOException 
      */
@@ -69,9 +78,7 @@ public class NewsController {
     
     /**
      * 添加分享信息
-     *
-     * 传递的参数:
-     * 		id:海报消息id 
+     * 传递的参数:id:海报消息id
      * @return
      * @throws IOException 
      */
@@ -85,7 +92,80 @@ public class NewsController {
     	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);
+            weixinShare.setAppId(Constant.WECHAT_APPID);
+            msg.data = weixinShare;
+          } catch (Exception e) {	
+            e.printStackTrace();	
+            msg.setError("服务器异常!");
+          }	
+        }	
+        response.setContentType("application/json");
+    	response.setCharacterEncoding("UTF-8");
+    	response.getWriter().println(msg.toJSONStr());
+      }
+    	
 }
 

+ 104 - 64
minigame/src/com/hssx/controller/PrizeController.java

@@ -88,82 +88,83 @@ public class PrizeController {
 //		}
 		if (luckDrawCount <= 1) {
 			// 直接抽奖(每日默认2次抽奖机会)
-			synchronized (this) {
-				Random ran = new Random();
-				int num = ran.nextInt(60);
-				Prize prize = prizeMapper.selectByPrimaryKey(num);
-				if (prize == null) {
-					count = -1;
-				} else {
-					if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
-						count = -1;
-					} else {
-						if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
-							count = 0;
-						} else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
-							count = 1;
-						} else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
-							count = 2;
-						} else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
-							count = 3;
-						} else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
-							count = 4;
-						}
-						 prize.setIsSelected(1);
-						 prizeMapper.updateByPrimaryKeySelective(prize);
-						prizeRecord.setPrizeId(prize.getPrizeLevel());
-						prizeRecord.setPrize(prize.getName());
-						prizeRecordMapper.insert(prizeRecord);
-					}
-				}
-			}
-			// count = getLuckDraw(prizeRecord, count);
+//			synchronized (this) {
+//				Random ran = new Random();
+//				int num = ran.nextInt(60);
+//				Prize prize = prizeMapper.selectByPrimaryKey(num);
+//				if (prize == null) {
+//					count = -1;
+//				} else {
+//					if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
+//						count = -1;
+//					} else {
+//						if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
+//							count = 0;
+//						} else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
+//							count = 1;
+//						} else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
+//							count = 2;
+//						} else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
+//							count = 3;
+//						} else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
+//							count = 4;
+//						}
+//						 prize.setIsSelected(1);
+//						 prizeMapper.updateByPrimaryKeySelective(prize);
+//						prizeRecord.setPrizeId(prize.getPrizeLevel());
+//						prizeRecord.setPrize(prize.getName());
+//						prizeRecordMapper.insert(prizeRecord);
+//					}
+//				}
+//			}
+			map = luckDrawWay(count,prizeRecord,map);
 			// 添加抽奖记录
 			UserPrizeCount userPrizeCount = new UserPrizeCount();
 			userPrizeCount.setUserId(userId);
 			UserPrizeCountMapper.insert(userPrizeCount);
-			map.put("prizeLevel", count);
-			map.put("prizeRecord", prizeRecord);
+//			map.put("prizeLevel", count);
+//			map.put("prizeRecord", prizeRecord);
 			msg.data = map;
 		} else if (luckDrawCount == 2) {
 			int opportunityCount = prizeOpportunityMapper.selectCountByToday(userId);
 			if (opportunityCount > 0) {
 				// 已分享直接抽奖
-				synchronized (this) {
-					Random ran = new Random();
-					int num = ran.nextInt(60);// 50%中奖概率
-					Prize prize = prizeMapper.selectByPrimaryKey(num);
-					if (prize == null) {
-						count = -1;
-					} else {
-						if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
-							count = -1;
-						} else {
-							if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
-								count = 0;
-							} else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
-								count = 1;
-							} else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
-								count = 2;
-							} else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
-								count = 3;
-							} else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
-								count = 4;
-							}
-							prize.setIsSelected(1);
-							prizeMapper.updateByPrimaryKeySelective(prize);
-							prizeRecord.setPrizeId(prize.getPrizeLevel());
-							prizeRecord.setPrize(prize.getName());
-							prizeRecordMapper.insert(prizeRecord);
-						}
-					}
-				}
+				map = luckDrawWay(count,prizeRecord,map);
+//				synchronized (this) {
+//					Random ran = new Random();
+//					int num = ran.nextInt(60);// 50%中奖概率
+//					Prize prize = prizeMapper.selectByPrimaryKey(num);
+//					if (prize == null) {
+//						count = -1;
+//					} else {
+//						if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
+//							count = -1;
+//						} else {
+//							if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
+//								count = 0;
+//							} else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
+//								count = 1;
+//							} else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
+//								count = 2;
+//							} else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
+//								count = 3;
+//							} else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
+//								count = 4;
+//							}
+//							prize.setIsSelected(1);
+//							prizeMapper.updateByPrimaryKeySelective(prize);
+//							prizeRecord.setPrizeId(prize.getPrizeLevel());
+//							prizeRecord.setPrize(prize.getName());
+//							prizeRecordMapper.insert(prizeRecord);
+//						}
+//					}
+//				}
 				// 添加抽奖记录
 				UserPrizeCount userPrizeCount = new UserPrizeCount();
 				userPrizeCount.setUserId(userId);
 				UserPrizeCountMapper.insert(userPrizeCount);
-				map.put("prizeLevel", count);
-				map.put("prizeRecord", prizeRecord);
+//				map.put("prizeLevel", count);
+//				map.put("prizeRecord", prizeRecord);
 				msg.data = map;
 			} else {
 				msg.setError("抽奖次数已达上限,去分享即可获得抽奖机会");
@@ -176,6 +177,45 @@ public class PrizeController {
 		response.getWriter().println(msg.toJSONStr());
 
 	}
+	
+	//抽奖方法
+	public synchronized HashMap<String, Object> luckDrawWay (Integer count,PrizeRecord prizeRecord,HashMap<String, Object> map){
+		Random ran = new Random();
+		int num = ran.nextInt(60);// 50%中奖概率
+		Prize prize = prizeMapper.selectByPrimaryKey(num);
+		//以下是真实环境的抽奖原则
+//		PrizeExample exp = new PrizeExample();
+//		exp.setOrderByClause("RAND() LIMIT 1");
+//		Prize prize = prizeMapper.selectByExample(exp).get(0);
+		if (prize == null) {
+			count = -1;
+		} else {
+			if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
+				count = -1;
+			} else {
+				if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
+					count = 0;
+				} else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
+					count = 1;
+				} else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
+					count = 2;
+				} else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
+					count = 3;
+				} else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
+					count = 4;
+				}
+				prize.setIsSelected(1);
+				prizeMapper.updateByPrimaryKeySelective(prize);
+				prizeRecord.setPrizeId(prize.getPrizeLevel());
+				prizeRecord.setPrize(prize.getName());
+				prizeRecordMapper.insert(prizeRecord);
+			}
+		}
+		map.put("prizeLevel", count);
+		map.put("prizeRecord", prizeRecord);
+		return map;
+	}
+	
 
 	/**
 	 * 小游戏抽奖列表
@@ -212,7 +252,7 @@ public class PrizeController {
 	}
 
 	/**
-	 * 小游戏抽奖填写用户信息 参数:phone:电话 username:姓名 id:中奖纪录id
+	 * 小游戏抽奖填写用户信息 参数:phone:电话 userName:姓名 id:中奖纪录id
 	 * 
 	 * @return
 	 */

+ 1 - 1
minigame/src/com/hssx/controller/UserController.java

@@ -166,7 +166,7 @@ public class UserController {
 	
 	/**
 	 * 根据userId获取用户信息 id
-	 * @param iduser
+	 * 参数 :id:用户id
 	 * @param response
 	 * @throws IOException 
 	 */

+ 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.UserMapper;
 import com.hssx.service.NewsService;
-import com.hssx.utils.AccessToken;
 import com.hssx.utils.HttpKit;
 import com.hssx.utils.HttpRespMsg;
 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;
-    }
-}