xiaoyong931011
2022-09-07 11edfa154a6f6c48d06de201315b25978601229c
src/main/java/cc/mrbird/febs/pay/service/impl/XcxPayServiceImpl.java
@@ -26,13 +26,19 @@
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.toolkit.Base64;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@@ -41,6 +47,7 @@
import java.io.*;
import java.math.BigDecimal;
import java.net.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -150,6 +157,45 @@
    }
    @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());
//                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
    public BrandWCPayRequestData startPayment(MallOrderInfo mallOrderInfo) throws Exception {
        BigDecimal unit = new BigDecimal("100");
        BigDecimal money = new BigDecimal(mallOrderInfo.getAmount().toString());
@@ -251,12 +297,97 @@
        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 imgName="/user_" + randomNum + "_acode_1.jpg";
        String codeImgPath = generateAcode(wxGenerateQrCodeDto.getScene(), wxGenerateQrCodeDto.getPage(), imgName, "800px", 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);
    }
    public static ByteArrayInputStream sendPost(String URL, String json) {
        InputStream inputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        HttpPost httppost = new HttpPost(URL);
        httppost.addHeader("Content-type", "application/json; charset=utf-8");
        httppost.setHeader("Accept", "application/json");
        try {
            StringEntity s = new StringEntity(json, Charset.forName("UTF-8"));
            s.setContentEncoding("UTF-8");
            httppost.setEntity(s);
            org.apache.http.HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 获取相应实体
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                // 创建一个Buffer字符串
                byte[] buffer = new byte[1024];
                // 每次读取的字符串长度,如果为-1,代表全部读取完毕
                int len = 0;
                // 使用一个输入流从buffer里把数据读取出来
                while ((len = inputStream.read(buffer)) != -1) {
                    // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                    outStream.write(buffer, 0, len);
                }
                // 关闭输入流
                inputStream.close();
                // 把outStream里的数据写入内存
                byteArrayInputStream = new ByteArrayInputStream(outStream.toByteArray());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return byteArrayInputStream;
    }
@@ -270,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)){