All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.share.source.AliShare Maven / Gradle / Ivy

package com.spring.boxes.share.source;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;

import org.springframework.util.CollectionUtils;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayOpenAppQrcodeCreateModel;
import com.alipay.api.request.AlipayOpenAppQrcodeCreateRequest;
import com.alipay.api.response.AlipayOpenAppQrcodeCreateResponse;
import com.spring.boxes.share.ShareWith;
import com.spring.boxes.share.beans.ShareData;
import com.spring.boxes.share.beans.ShareKey;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class AliShare implements ShareWith {

    private ShareKey shareKey;

    @Override
    public BufferedImage getQrCode(ShareData shareData) throws Exception {
        AlipayClient alipayClient = toAlipayClient(shareKey);
        AlipayOpenAppQrcodeCreateRequest request = new AlipayOpenAppQrcodeCreateRequest();
        AlipayOpenAppQrcodeCreateModel model = new AlipayOpenAppQrcodeCreateModel();
        model.setUrlParam(shareData.getPage());
        if (!CollectionUtils.isEmpty(shareData.getParams())) {
            StringBuilder params = new StringBuilder();
            shareData.getParams().forEach((k,v) ->{ params.append(",").append(v); });
            model.setQueryParam(params.toString().substring(1));
        }
        model.setDescribe("小程序二维码");
        request.setBizModel(model);
        AlipayOpenAppQrcodeCreateResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            return ImageIO.read(readFile(response.getQrCodeUrl()));
        }
        //请求失败
        log.error("生成支付宝二维码失败:" + response.getMsg() + "," + response.getSubMsg());
        throw new IllegalStateException("小程序维护中,请稍后再试");
    }

    /**
     * 获取图片流
     *
     * @param fileUrl http://url:port/store/lesson/qrcode/2018/07/12909064255321.jpg
     * @return InputStream
     */
    private InputStream readFile(String fileUrl) throws Exception{
        URL url = new URL(fileUrl);
        HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
        urlCon.setConnectTimeout(6000);
        urlCon.setReadTimeout(6000);
        int code = urlCon.getResponseCode();
        if (code != HttpURLConnection.HTTP_OK) {
            throw new Exception("文件读取失败");
        }
        return urlCon.getInputStream();
    }

    private AlipayClient toAlipayClient(ShareKey shareKey) {
        String appId = shareKey.getAppId();
        String privateKey = shareKey.getSecretKey();
        String publicKey = shareKey.getPublicKey();
        return new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "utf-8", publicKey, "RSA2");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy