微信相关接口调用(一)

1:小程序接口调用(获取openid)

private String openidUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

    private String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

    public String getOpenid(String jsCode) {
        String appid = coreProperties.getMiniapp().getAppid();
        String secret = coreProperties.getMiniapp().getAppsecret();
        String url = String.format(openidUrl, appid, secret, jsCode);
        String result = Https.get(url);
        MiniOpenid mini = Jsons.toObject(result, MiniOpenid.class);
        if (mini.getErrcode() != null && !SUCCESS_CODE.equals(mini.getErrcode())) {
            throw new ServiceException("获取openid失败");
        }
        return mini.getOpenid();
    }


    public String getAccessToken() {
        String accessToken = redisCache.get("accessToken");
        if (StringUtils.isNotBlank(accessToken)) {
            return accessToken;
        }
        String appid = coreProperties.getMiniapp().getAppid();
        String secret = coreProperties.getMiniapp().getAppsecret();
        String url = String.format(accessTokenUrl, appid, secret);
        String result = Https.get(url);
        accessToken = Jsons.getValue(result, "access_token");
        if (!StringUtils.isNotBlank(accessToken)) {
            throw new ServiceException("获取accessToken失败");
        }
        redisCache.set("accessToken", accessToken, 7000L, TimeUnit.SECONDS);
        return accessToken;
    }

2:获取微信公众号的openid

/**
     * 公众号授权
     */
    public void accessSign(String code,String state){
        // 前面为业务逻辑
        ShipOwnerRegister shipOwnerRegister = shipOwnerRegisterService.getByToken(state);
        String phone = shipOwnerRegister.getPhone();
        String appid = coreproperties.getWechat().getAppid();
        String appsecret = coreproperties.getWechat().getAppsecret();
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid
                + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
        String resp = Https.get(url);
        log.info("wechat-resp:" + resp);
        String openid = Jsons.getValue(resp, "openid");
        log.info("wechat-openid:" + openid);
        redisCache.set(ACCESS_TOKEN, getAccessToken(), 7200L, TimeUnit.SECONDS);
        // 保存openid到ship_owner_register中去
        shipOwnerRegister.setWechatOpenId(openid);
        shipOwnerRegisterService.updateEX(shipOwnerRegister);
    }
/**
     * 获得公众号的access_token
     */
    private String getAccessToken() {
        /*if (StringUtils.isNotBlank(redisCache.get("driverApi:ACCESS_TOKEN"))) {
            return redisCache.get("driverApi:ACCESS_TOKEN");
        }*/
        String appid = coreProperties.getWechat().getAppid();
        String appsecret = coreProperties.getWechat().getAppsecret();
        log.info("appid:" + appid + "----appsecret:" + appsecret);
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid
                + "&secret=" + appsecret;
        String resp = Https.get(url);
        log.info("accessToken-resp:" + resp);
        String accessToken = Jsons.getValue(resp, "access_token");
        log.info("accessToken-token:" + accessToken);
        redisCache.set("driverApi:ACCESS_TOKEN", accessToken, 7200L, TimeUnit.SECONDS);
        return accessToken;
    }

3:发送微信模板消息

/**
     * 发送模板消息
     */
    public void sendToWeChat(BigDecimal price,String cargoName,BigDecimal capacity,String startPort,String endPort,String remarks,String weopenId){
        if (StringUtils.isBlank(weopenId))
            return;
        String access_token = getAccessToken();
        String priceStr = price!=null?price.toString():"";
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token;
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touser",weopenId);
        params.put("template_id", coreProperties.getWechat().getTemplate());
        params.put("first",MessageData.builder().value("运费单价:"+priceStr+"元").color("#173177").build());
        params.put("keyword1",MessageData.builder().value(cargoName).color("#173177").build());
        params.put("keyword2",MessageData.builder().value(capacity!=null?capacity.toString():"").color("#173177").build());
        params.put("keyword3",MessageData.builder().value(startPort).color("#173177").build());
        params.put("keyword4",MessageData.builder().value(endPort).color("#173177").build());
        params.put("keyword5",MessageData.builder().value("").color("#173177").build());
        params.put("remark",MessageData.builder().value("备注:"+remarks).color("#173177").build());
        // 将map转成json
        String response = Https.postJson(url,params);
        log.info("微信返回信息:"+response);
    }

@Data
    public static class MessageData {

        private String value;

        private String color;

    }

4:根据公众号openid获得用户信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值