办学质量监测教学评价系统
winkey
2025-04-07 0153f004f437fdeda4d3eb421114ab8e69059d69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.ruoyi.system.controller.wxsingle;
 
 
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import org.ruoyi.system.cofing.WxCpConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
 
import java.nio.charset.StandardCharsets;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author <a href="https://github.com/0katekate0">Wang_Wong</a>
 */
//@RestController
//@RequiredArgsConstructor
//@RequestMapping("/wx/cp/js/{corpId}/{agentId}/getJsConf")
public class WxJsController {
    @PostMapping("/getJsConf")
    public Map getJsConf(
            @PathVariable String corpId,
            @PathVariable Integer agentId,
            String uri) throws WxErrorException {
 
        final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        if (wxCpService == null) {
            throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
        }
 
        WxJsapiSignature wxJsapiSignature = wxCpService.createJsapiSignature(uri);
        String signature = wxJsapiSignature.getSignature();
        String nonceStr = wxJsapiSignature.getNonceStr();
        long timestamp = wxJsapiSignature.getTimestamp();
 
        Map res = new HashMap<String, String>();
        res.put("appId", corpId); // 必填,企业微信的corpID
        res.put("timestamp", timestamp); // 必填,生成签名的时间戳
        res.put("nonceStr", nonceStr); // 必填,生成签名的随机串
        res.put("signature", signature); // 必填,签名,见 附录-JS-SDK使用权限签名算法
        return res;
    }
 
 
    public static String genNonce() {
        return bytesToHex(Long.toString(System.nanoTime()).getBytes(StandardCharsets.UTF_8));
    }
 
    public static String bytesToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }
 
}