微信现金红包跳坑版实战

官方给出开通现金红包的前期的步骤

1、值得注意提醒的地方是,需要在微信支付的商户平台----产品中心—现金红包-----产品设置中—配置调用IP地址(公网IP)

2、结合业务总体开发思维
1)用户是否关注了公众号----没有关注公众号----引导关注公众号----公众号上发放现金红包
2)用户关注了公众号—公众号上发放现金红包

在这说一下,小程序有自己的openid,公众号也有自己的openid。千万不要混淆。唯一联系是共有的UnionID。(关于怎么有UnionId,看下图。后面可以根据UnionId绑定同一账号)

小程序和公众号关联在一起,所以调用对应的授权登录接口,都有相应的UnionId。公众号也需要在这个页面添加喔

3、公众号提供了检测当前用户是否关注了当前公众号的API

在这里插入图片描述
微信的接口、基本都需要这个access_token,至于openid可以在自己的公众号配置上找到。
公众号有一个很重要 问题,需要配置Ip白名单。

官方的:
在这里插入图片描述
自己的代码

 @ApiOperation(value = "校验是否关注公众号", notes = "校验是否关注公众号")
    @GetMapping(value = "/checkAttention")
    public Result<Object> checkAttention(HttpServletRequest request){
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=*******&secret=*********";
        String token = HttpUtil.doGetJson(url).getString("access_token");
        JSONObject json = new JSONObject();
        String uid = getUid(request);
        SysUser sysUser =userService.getById(uid);
        String uus = CHECK+token+"&openid="+sysUser.getMpOpenId()+"&lang=zh_CN";
        String infoStr = HttpUtil.getResponseByGet(uus);
        JSONObject info= JSONObject.parseObject(infoStr);
        MPBaseInfo baseInfo = (MPBaseInfo)JSONObject.toJavaObject(info, MPBaseInfo.class);
        if(StringUtils.isBlank(baseInfo.getSubscribe()) || "0".equals(baseInfo.getSubscribe())){
            return  Result.error("用户尚未关注公众号");
        }
        return  Result.ok();
    }

HttpUtil

    public static JSONObject doGetJson(String url) {
        JSONObject jsonObject = null;
        String result = "";
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity, "UTF-8");
                jsonObject = JSONObject.parseObject(result);
                log.info("返回结果:"+jsonObject);
            }
        } catch (IOException e) {
            log.error("doGet error by:{}", url, e);
        } finally {
            httpGet.releaseConnection();
        }
        return jsonObject;
    }
    public static  String getResponseByGet(String url){
        String result="";
        try {
            URL url1 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
            conn.setDoOutput(true);
            // 腾讯地图使用GET
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            // 获取地址解析结果
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
        } catch (Exception e) {
            e.getMessage();
        }
        return  result;
    }

以上是获取用户token以及检验是否关注公众号了。接下面便是重头戏了。

4、Api证书准备以及放置

在这里插入图片描述
5、在这里插入图片描述
在这里插入图片描述

上代码

    public void sendRedPackets(String mpOpendId,String  money) {
        RequestHandler rh = new RequestHandler();
        rh.setParameter("nonce_str", getRandomString(30));//随机字符串,不长于32位
        rh.setParameter("mch_billno", getRandomString(28));//商户订单号(每个订单号必须唯一)组成:mch_id+yyyymmdd+10位一天内不能重复的数字。接口根据商户订单号支持重入,如出现超时可再调用。
        rh.setParameter("mch_id", APP_ID);//微信商户平台的账户id
        rh.setParameter("wxappid", MP_APP_ID);//公众号的appid
        rh.setParameter("send_name", "奥特曼打小怪兽");//商户名称
        rh.setParameter("re_openid", mpOpendId);//用户公众号的id
        rh.setParameter("total_amount", money);//付款金额 单位分
        rh.setParameter("total_num", "1");//红包发放总人数
        rh.setParameter("wishing", "欢迎下次光顾");//红包祝福语
        rh.setParameter("client_ip", ip);//Ip白名单
        rh.setParameter("act_name", "活动红包");//活动名称
        rh.setParameter("remark", "活动红包");//备注
        log.info("红包请求参数,{}",rh);
        sendRedPacket(rh);
    }
 public static String getRandomString(int length) {
        //产生随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //循环length次
        for (int i = 0; i < length; i++) {
            //产生0-2个随机数,既与a-z,A-Z,0-9三种可能
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                //如果number产生的是数字0;
                case 0:
                    //产生A-Z的ASCII码
                    result = Math.round(Math.random() * 25 + 65);
                    //将ASCII码转换成字符
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    //产生a-z的ASCII码
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    //产生0-9的数字
                    sb.append(String.valueOf
                            (new Random().nextInt(10)));
                    break;
            }
        }
        return sb.toString();
    }
   public void sendRedPacket(RequestHandler rh) {
        // TODO Auto-generated method stub
        Map<String, String> result = new HashMap<String, String>();
        String path = "/usr/apiclient_cert.p12";//上文所说的API证书路径
        try {
            String sign = rh.createMd5Sign("key", key);//key,秘钥和值根据微信加密规则加密签名
            rh.setParameter("sign", sign);//设置签名到请求参数中
            String data = rh.parseXML();//调用现金红包接口需要的数据XML格式
            String xmlResult = doPostByWXSSL("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", data, path, APP_ID);//现金红包接口返回的xml 、APP_ID 同上
            log.info("调用微信接口返回数据:" + xmlResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public String doPostByWXSSL(String url, String data, String certPath, String mchId) {
        StringBuffer message = new StringBuffer();
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File(certPath));//使用的证书
            keyStore.load(instream, mchId.toCharArray());
            // Trust own CA and all self-signed certs
            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, mchId.toCharArray())
                    .build();
            // Allow TLSv1 protocol only
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    new String[]{"TLSv1"},
                    null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();
            HttpPost httpost = new HttpPost(url);

            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            log.info("executing request" + httpost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                log.info("--------------------发送微信接收参数开始--------------------");
                log.info("请求响应状态:" + response.getStatusLine());
                if (entity != null) {
                    log.info("响应字节总长度: " + entity.getContentLength());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
                    String text;
                    while ((text = bufferedReader.readLine()) != null) {
                        message.append(text);
                    }
                }
                log.info("--------------------发送微信接收参数结束--------------------");
                EntityUtils.consume(entity);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                response.close();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            log.error("微信红包请求出错",e1.fillInStackTrace());
        }
        return message.toString();

    }

以为这就结束了?不不,其中关键的是openid的获取

官方术语:网页授权流程分为四步:

1、引导用户进入授权页面同意授权,获取code

2、通过code换取网页授权access_token(与基础支持中的access_token不同)

3、如果需要,开发者可以刷新网页授权access_token,避免过期

4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)
文档

code获取,需要公众号上先是配置网页授权页面,第二步放证书。回调域名的服务器上
在这里插入图片描述
到最后我们即拥有了唯一的UNIONID 也有了用户的公众号的openid,现金红包也算完成了。代码是丑陋了点。不要喷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值