111
k94314517
2023-11-08 2bb847e3b8bdc2692d88ad9df0d4ad54a3ccd890
server/services/src/main/java/com/doumee/core/wx/WxMiniUtilService.java
@@ -1,5 +1,6 @@
package com.doumee.core.wx;
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.Constants;
import com.doumee.core.constants.ResponseStatus;
@@ -7,22 +8,31 @@
import com.doumee.core.utils.ID;
import com.doumee.dao.business.RefundMapper;
import com.doumee.dao.business.TransactionsMapper;
import com.doumee.dao.business.model.Locks;
import com.doumee.dao.business.model.Refund;
import com.doumee.dao.business.model.Transactions;
import com.doumee.dao.business.web.request.RefundDTO;
import com.doumee.dao.system.model.SystemDictData;
import com.doumee.service.business.RefundService;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.*;
/**
 * 微信小程序-公共方法
@@ -36,6 +46,9 @@
    @Autowired
    private TransactionsMapper transactionsMapper;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Transactional(rollbackFor = {BusinessException.class,Exception.class})
@@ -111,4 +124,56 @@
        throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"退款发生异常请联系管理员");
    }
    /**
     * 生成小程序码
     * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
     * @return
     */
    public void generateWXMiniCode(Locks locks){
        SystemDictData systemDictData = systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.ACCESS_TOKEN);
        if(Objects.isNull(systemDictData)){
            return;
        }
        //生成图片上传OSS
        Map<String,Object> body = new HashMap<>();
        // 场景码,与前端约定,最终是需要前端解析
        body.put("scene", locks.getSiteId() + "/" +locks.getCode() );
        // 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
        body.put("env_version", "release");
        // 透明,根据你的场景自行设置body参数
        body.put("is_hyaline", false);
//        body.put("page","pages/index/index");
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/json");
        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(mediaType, JSONObject.toJSONString(body));
        Request request = new Request.Builder().url("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+systemDictData.getCode()).method("POST", requestBody).build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                InputStream inputStream = new ByteArrayInputStream(response.body().bytes());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = -1;
                while ((len = inputStream.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }
                locks.setInfo("data:mediatype;base64," + Base64.getEncoder().encodeToString(baos.toByteArray()));
//                FileOutputStream out = new FileOutputStream("d:\\test.png");
//                byte[] buffer = new byte[8192];
//                int bytesRead = 0;
//                while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
//                    out.write(buffer, 0, bytesRead);
//                }
//                out.flush();
//                inputStream.close();
//                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}