Althars123 3 年之前
父节点
当前提交
29492a9d20
共有 1 个文件被更改,包括 75 次插入14 次删除
  1. 75 14
      twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

+ 75 - 14
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -7,7 +7,6 @@ import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.utils.StringUtils;
 import com.miaxis.common.utils.wx.MessageUtil;
-import com.miaxis.common.utils.wx.SHA1Util;
 import com.miaxis.feign.dto.WxMessageCusom;
 import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.school.domain.SchoolInfo;
@@ -24,16 +23,16 @@ import com.miaxis.wx.service.WxService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.RandomStringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import javax.servlet.http.HttpServletRequest;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -458,24 +457,86 @@ public class WxGzhServiceImpl implements IWxGzhService {
         //2、获取Ticket
         String jsapiStr = wxService.getticket(gzhToken, "jsapi");
         //3、时间戳和随机字符串
-        String nonce_str = RandomStringUtils.randomAlphanumeric(16);
-        long timestamp =  System.currentTimeMillis()/1000;
+//        String nonce_str = RandomStringUtils.randomAlphanumeric(16);
+//        long timestamp =  System.currentTimeMillis()/1000;
         JSONObject jsapiData = JSONObject.parseObject(jsapiStr);
         String ticket= jsapiData.getString("ticket");
-        //4、获取url
-        String str = "jsapi_ticket="+jsapiStr+"&noncestr="+nonce_str+"&timestamp="+timestamp+"&url="+url;
+        Map<String, String> ret = sign(ticket, url);
 
-        //6、将字符串进行sha1加密
-        String signature = SHA1Util.SHA1(str);
+
+
+//        //4、获取url
+//        String str = "jsapi_ticket="+jsapiStr+"&noncestr="+nonce_str+"&timestamp="+timestamp+"&url="+url;
+//
+//        //6、将字符串进行sha1加密
+//        String signature = SHA1Util.SHA1(str);
         JSONObject resultJSONObject = new JSONObject();
         resultJSONObject.put("ticket",ticket);
         resultJSONObject.put("appId",appid);
-        resultJSONObject.put("timestamp",String.valueOf(timestamp));
-        resultJSONObject.put("nonceStr",nonce_str);
-        resultJSONObject.put("signature",signature);
+        resultJSONObject.put("timestamp",ret.get("timestamp"));
+        resultJSONObject.put("nonceStr",ret.get("nonceStr"));
+        resultJSONObject.put("signature",ret.get("signature"));
         return resultJSONObject;
     }
 
+    public static Map<String, String> sign(String jsapi_ticket, String url) {
+        Map<String, String> ret = new HashMap<String, String>();
+        String nonce_str = create_nonce_str();
+        String timestamp = create_timestamp();
+        String string1;
+        String signature = "";
+
+        //注意这里参数名必须全部小写,且必须有序
+        string1 = "jsapi_ticket=" + jsapi_ticket +
+                "&noncestr=" + nonce_str +
+                "&timestamp=" + timestamp +
+                "&url=" + url;
+        System.out.println(string1);
+
+        try
+        {
+            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
+            crypt.reset();
+            crypt.update(string1.getBytes("UTF-8"));
+            signature = byteToHex(crypt.digest());
+        }
+        catch (NoSuchAlgorithmException e)
+        {
+            e.printStackTrace();
+        }
+        catch (UnsupportedEncodingException e)
+        {
+            e.printStackTrace();
+        }
+
+        ret.put("url", url);
+        ret.put("jsapi_ticket", jsapi_ticket);
+        ret.put("nonceStr", nonce_str);
+        ret.put("timestamp", timestamp);
+        ret.put("signature", signature);
+
+        return ret;
+    }
+
+    private static String byteToHex(final byte[] hash) {
+        Formatter formatter = new Formatter();
+        for (byte b : hash)
+        {
+            formatter.format("%02x", b);
+        }
+        String result = formatter.toString();
+        formatter.close();
+        return result;
+    }
+
+    private static String create_nonce_str() {
+        return UUID.randomUUID().toString();
+    }
+
+    private static String create_timestamp() {
+        return Long.toString(System.currentTimeMillis() / 1000);
+    }
+