xiaoyong931011
2022-09-07 29a8eeb43120d2019032f303bc9262308abb611b
src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java
@@ -158,42 +158,41 @@
    @Override
    public FebsResponse getQrCode(WxGenerateQrCodeDto wxGenerateQrCodeDto) {
        //这里调用的是上面的获取access_token方法
        String access_token = redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString();
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token;
        String scene = wxGenerateQrCodeDto.getScene();
        Map<String, String> param = new HashMap<>();
        param.put("scene",scene);
        //这里的page如果没有的话可以不写,默认是跳主页,如果写了没有的页面的话,会返回错误信息
        param.put("page",wxGenerateQrCodeDto.getPage());
        String json = JSON.toJSONString(param);
        ByteArrayInputStream inputStream = sendPost(url, json);
        try {
            System.out.println(inputStream);
            //这里判断的是返回的图片还是错误信息,一般错误信息不会大于200
            if (inputStream.available() <= 200){
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                int i;
                byte[] buffer = new byte[200];
                while ((i = inputStream.read(buffer)) != -1){
                    byteArrayOutputStream.write(buffer,0,i);
                }
                String str = new String(byteArrayOutputStream.toByteArray());
                System.out.println(str);
                //错误信息的格式在官方文档里有
                com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
                if ("41030".equals(jsonObject.getString("errcode"))){
                    return new FebsResponse().fail().data("所传page页面不存在,或者小程序没有发布");
                }else if ("45009".equals(jsonObject.getString("errcode"))){
                    return new FebsResponse().fail().data("调用分钟频率受限");
                }
                byteArrayOutputStream.close();
            }
            inputStream.close();
        }catch (Exception e){
            return new FebsResponse().fail().data("获取二维码失败");
        }
        return new FebsResponse().success().data(inputStream);
//        //这里调用的是上面的获取access_token方法
//        String access_token = redisUtils.get(WechatConfigure.WX_ACCESS_TOKEN_REDIS_KEY).toString();
//        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token;
//        String scene = wxGenerateQrCodeDto.getScene();
//        Map<String, String> param = new HashMap<>();
//        param.put("scene",scene);
//        //这里的page如果没有的话可以不写,默认是跳主页,如果写了没有的页面的话,会返回错误信息
//        param.put("page",wxGenerateQrCodeDto.getPage());
//        String json = JSON.toJSONString(param);
//        ByteArrayInputStream inputStream = sendPost(url, json);
//        try {
//            System.out.println(inputStream);
//            //这里判断的是返回的图片还是错误信息,一般错误信息不会大于200
//            if (inputStream.available() <= 200){
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                int i;
//                byte[] buffer = new byte[200];
//                while ((i = inputStream.read(buffer)) != -1){
//                    byteArrayOutputStream.write(buffer,0,i);
//                }
//                String str = new String(byteArrayOutputStream.toByteArray());
//                byteArrayOutputStream.close();
//                //错误信息
//                com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(str);
//                if ("41030".equals(jsonObject.getString("errcode"))){
//                    return new FebsResponse().fail().data("所传page页面不存在,或者小程序没有发布");
//                }else if ("45009".equals(jsonObject.getString("errcode"))){
//                    return new FebsResponse().fail().data("调用分钟频率受限");
//                }
//            }
//            inputStream.close();
//        }catch (Exception e){
//            return new FebsResponse().fail().data("获取二维码失败");
//        }
        return new FebsResponse().success();
    }
    @Override
@@ -298,11 +297,50 @@
        return wxTemplates;
    }
    @Autowired
    private MallGoodsMapper mallGoodsMapper;
    @Autowired
    private MallTeamLeaderMapper mallTeamLeaderMapper;
    @Override
    public FebsResponse generateQrCode(WxGenerateQrCodeDto wxGenerateQrCodeDto) {
        Integer type = wxGenerateQrCodeDto.getType();
        if(1 == type){
            long goodsId = StrUtil.isBlank(wxGenerateQrCodeDto.getTypeParam()) ? 0L : Long.parseLong(wxGenerateQrCodeDto.getTypeParam());
            MallGoods mallGoods = mallGoodsMapper.selectById(goodsId);
            if(ObjectUtil.isNotNull(mallGoods)){
                String wxCodeImg = mallGoods.getWxCodeImg();
                if(StrUtil.isNotBlank(wxCodeImg)){
                    return new FebsResponse().success().data(wxCodeImg);
                }
            }
        }
        if(2 == type){
            String uniqueCode = wxGenerateQrCodeDto.getTypeParam();
            MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectLeaderByUniqueCode(uniqueCode);
            if(ObjectUtil.isNotNull(mallTeamLeader)){
                String wxCodeImg = mallTeamLeader.getWxCodeImg();
                if(StrUtil.isNotBlank(wxCodeImg)){
                    return new FebsResponse().success().data(wxCodeImg);
                }
            }
        }
        String randomNum = MallUtils.getRandomNum(5);
        String imgName="/user_" + randomNum + "_acode_1.jpg";
        String codeImgPath = generateAcode(wxGenerateQrCodeDto.getScene(), wxGenerateQrCodeDto.getPage(), imgName, "800px", null);
        String codeImgPath = generateAcode(wxGenerateQrCodeDto.getScene(), wxGenerateQrCodeDto.getPage(), imgName, "400px", null);
        if(1 == type){
            long goodsId = StrUtil.isBlank(wxGenerateQrCodeDto.getTypeParam()) ? 0L : Long.parseLong(wxGenerateQrCodeDto.getTypeParam());
            MallGoods mallGoods = mallGoodsMapper.selectById(goodsId);
            mallGoods.setWxCodeImg(codeImgPath);
            mallGoodsMapper.updateById(mallGoods);
        }
        if(2 == type){
            String uniqueCode = wxGenerateQrCodeDto.getTypeParam();
            MallTeamLeader mallTeamLeader = mallTeamLeaderMapper.selectLeaderByUniqueCode(uniqueCode);
            mallTeamLeader.setWxCodeImg(codeImgPath);
            mallTeamLeaderMapper.updateById(mallTeamLeader);
        }
        return new FebsResponse().success().data(codeImgPath);
    }
@@ -363,7 +401,7 @@
    //图片上传路径
    public  static  final String IMG_UPLOAD_PATH="/mnt/sdc/webresource/groupbuy/wxcode";
    public  String generateAcode(String scene,String path,String imgName,String width, Integer type){
        String urlPrefix="http://hwfile.csxuncong.com/groupbuy/wxcode";
        String urlPrefix="https://hwfile.csxuncong.com/groupbuy/wxcode";
        String imgPath=IMG_UPLOAD_PATH+imgName;
        if(!FileUtil.exist(imgPath)){