Browse Source

添加 公众号 Config

DESKTOP-USV654P\pc 1 year ago
parent
commit
55043861f5

+ 62 - 0
src/main/java/com/xjrsoft/common/utils/WeChatUtil.java

@@ -15,8 +15,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 +186,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 +
+                "&timestamp=" + 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;
+    }
 }

+ 6 - 0
src/main/java/com/xjrsoft/module/organization/controller/WechatController.java

@@ -63,5 +63,11 @@ public class WechatController {
         return  R.ok(departmentService.Ipage(dto));
     }
 
+    @GetMapping(value = "/js-config")
+    @ApiOperation(value="前端jssdk页面配置需要用到的配置参数")
+    public  R getWeixinJs () throws Exception {
+        return R.ok(WeChatService.getJsConfig());
+    }
+
 
 }

+ 12 - 0
src/main/java/com/xjrsoft/module/organization/dto/WeChatJsConfigDto.java

@@ -0,0 +1,12 @@
+package com.xjrsoft.module.organization.dto;
+
+import lombok.Data;
+
+@Data
+public class WeChatJsConfigDto {
+
+    private String url;
+    private  String nonceStr;
+    private String timestamp;
+    private  String signature;
+}

+ 3 - 0
src/main/java/com/xjrsoft/module/organization/service/IWeChatService.java

@@ -1,6 +1,7 @@
 package com.xjrsoft.module.organization.service;
 
 
+import com.xjrsoft.module.organization.dto.WeChatJsConfigDto;
 import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
 
 public interface IWeChatService {
@@ -9,4 +10,6 @@ public interface IWeChatService {
     void sendTemplateMessage(WeChatSendMessageDto dto);
 
     boolean updateInfo();
+
+    WeChatJsConfigDto getJsConfig() throws Exception;
 }

+ 12 - 0
src/main/java/com/xjrsoft/module/organization/service/impl/WeChatServiceImgl.java

@@ -9,6 +9,7 @@ import com.xjrsoft.common.enums.DeleteMark;
 import com.xjrsoft.common.enums.EnabledMark;
 import com.xjrsoft.common.utils.WeChatUtil;
 import com.xjrsoft.module.organization.dto.WeChatDepartDto;
+import com.xjrsoft.module.organization.dto.WeChatJsConfigDto;
 import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
 import com.xjrsoft.module.organization.dto.WeChatUserDto;
 import com.xjrsoft.module.organization.entity.Department;
@@ -185,4 +186,15 @@ public class WeChatServiceImgl implements IWeChatService {
         List<WeChatDepartDto> departmentList = weChatUtil.getDepartmentList(dept_id);
         deptDepartlist.addAll(departmentList);
     }
+
+    public WeChatJsConfigDto getJsConfig()  throws Exception {
+        WeChatJsConfigDto weChatJsConfigDto = new WeChatJsConfigDto();
+        HashMap<String, Object> resultMap = weChatUtil.SdkSign("https://zhxy.cqtlzjzx.com/app", "jsapi");
+        weChatJsConfigDto.setTimestamp(resultMap.get("timestamp").toString());
+        weChatJsConfigDto.setUrl(resultMap.get("url").toString());
+        weChatJsConfigDto.setSignature(resultMap.get("signature").toString());
+        weChatJsConfigDto.setNonceStr(resultMap.get("nonceStr").toString());
+        return weChatJsConfigDto;
+    }
+
 }