package com.ruoyi.weixin.service;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.weixin.config.WxMaConfiguration;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author wc
 */
@Slf4j
@Service
public class WxMinService {

    /**
     * 微信app id
     */
    @Value("${wx.miniapp.configs[0].appid}")
    private String appid;

    @Value("${wx.miniapp.configs[0].version}")
    private String version;


    /**
     * 获取小程序二维码
     * @param path
     * @param scene
     * @return
     */
    public byte[] getWxMinCode(String path, String  scene) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        WxMaQrcodeService qrcodeService = wxService.getQrcodeService();
        try {
            byte[] bytes = qrcodeService.createWxaCodeUnlimitBytes(scene, path, false, version, 430, true,
                new WxMaCodeLineColor("0", "0", "0"), true);
            return bytes;
        } catch (WxErrorException e) {
            log.error("获取小程序二维码失败", e);
            throw new ServiceException("获取小程序二维码失败");
        }
    }

}
