|
@@ -7,7 +7,6 @@ import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.xjrsoft.common.enums.WeChatType;
|
|
|
-import com.xjrsoft.common.exception.MyException;
|
|
|
import com.xjrsoft.module.organization.dto.WeChatDepartDto;
|
|
|
import com.xjrsoft.module.organization.dto.WeChatUserDto;
|
|
|
import com.xjrsoft.module.system.vo.WeChatUserInfo;
|
|
@@ -15,8 +14,13 @@ import lombok.Data;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Formatter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
@Component
|
|
|
@Data
|
|
@@ -181,4 +185,61 @@ public class WeChatUtil {
|
|
|
System.out.printf(result);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ public String getTicket(String type) {
|
|
|
+ String token = this.getToken(WeChatType.WEWEB);
|
|
|
+ HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("access_token", token);
|
|
|
+ paramMap.put("type", type);
|
|
|
+ String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket", paramMap);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.containsKey("ticket")) {
|
|
|
+ //throw new MyException(result);
|
|
|
+ return jsonObject.get("ticket").toString();
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public HashMap<String, Object> SdkSign(String url,String type) throws Exception {
|
|
|
+ String nonceStr = UUID.randomUUID().toString();
|
|
|
+ String timestamp = Long.toString(System.currentTimeMillis() / 1000);
|
|
|
+ String signature = "";
|
|
|
+ String jsapiTicket = getTicket(type);
|
|
|
+
|
|
|
+ String string1 = "jsapi_ticket=" + jsapiTicket +
|
|
|
+ "&noncestr=" + nonceStr +
|
|
|
+ "×tamp=" + timestamp +
|
|
|
+ "&url=" + url;
|
|
|
+ try {
|
|
|
+ MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
|
|
+ crypt.reset();
|
|
|
+ crypt.update(string1.getBytes("UTF-8"));
|
|
|
+ signature = byteToHex(crypt.digest());
|
|
|
+
|
|
|
+ HashMap<String, Object> resultMap = new HashMap<>();
|
|
|
+ resultMap.put("url",url);
|
|
|
+// resultMap.put("jsapi_ticket",jsapiTicket);
|
|
|
+ resultMap.put("nonceStr",nonceStr);
|
|
|
+ resultMap.put("timestamp",timestamp);
|
|
|
+ resultMap.put("signature",signature);
|
|
|
+
|
|
|
+ return resultMap;
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private 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;
|
|
|
+ }
|
|
|
}
|