微信JS-SDK集成-JAVA

仲元凯
2023-12-01

微信公众号JS-SDK说明文档地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html

工具包tutool

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.0</version>
        </dependency>

	//公众号APPID
    String appId = "xxx";
    //公众号秘钥
    String secret = "xxx";
    
    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取随机字符串 Nonce Str
     *
     * @return String 随机字符串
     */
    public static String generateNonceStr() {
        char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }

    /**
     * access_token
     *
     * @return
     */
    public String accessToken() {
        //先查询缓存是否存在
        Object o = redisTemplate.opsForValue().get("wxgzh:accessToken");
        if (o != null) {
            return Convert.toStr(o);
        }
        String url = String.format(WxConstant.get_token, appId, secret);
        String access_token = HttpUtil.get(url);
        JSONObject obj = JSON.parseObject(access_token);

        String token = obj.getString("access_token");
        redisTemplate.opsForValue().set("wxgzh:accessToken", token, 3600, TimeUnit.SECONDS);

        return token;
    }

    /**
     * jsapi_ticket
     *
     * @return
     */
    public String jsapiTicket() {
        //先查询缓存是否存在
        Object o = redisTemplate.opsForValue().get("wxgzh:ticket");
        if (o != null) {
            return Convert.toStr(o);
        }
        String url = StrUtil.format(WxConstant.jsapi_ticket, accessToken());
        String result = HttpUtil.get(url);
        JSONObject obj = JSON.parseObject(result);
        String ticket = obj.getString("ticket");

        redisTemplate.opsForValue().set("wxgzh:ticket", ticket, 7200, TimeUnit.SECONDS);
        return ticket;
    }

    /**
     * 生成签名
     *
     * @return
     */
    public String signature(String nonceStr, long timestamp, String url) {
        String ticket = jsapiTicket();
        String sha1 = StrUtil.format("jsapi_ticket={}&noncestr={}&timestamp={}&url={}", ticket, nonceStr, timestamp, url);
        System.out.println("sha1 : " + sha1);
        return DigestUtils.sha1Hex(sha1);
    }

    @Override
    public Map<String, Object> jsSdk(String url) {
        String nonceStr = generateNonceStr();
        long timestamp = System.currentTimeMillis() / 1000;
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("app_id", appId);
        resultMap.put("timestamp", timestamp);
        resultMap.put("nonceStr", nonceStr);
        resultMap.put("signature", signature(nonceStr, timestamp, url));
        return resultMap;
    }
 类似资料: