php生成微信菊花码 小程序码

<?php
// +----------------------------------------------------------------------
// | ITUX.CN [ ITUX.CN软件定制开发 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014~2023 https://itux.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 此软件并不是自由软件,未经许可不能去掉ITUX相关版权
// +----------------------------------------------------------------------
// | Author: 13103895694 <itholiday@126.com>
// +----------------------------------------------------------------------

namespace addons\gaga\library\cqrs\libs;

use think\exception\ValidateException;

class Wxaqrcode
{
    protected $wxapp_settings;
    protected $default_wxapp_settings;
    private $appId;
    private $appSecret;
    private $access_token;
    private $error = '';

    public function __construct($app_id,$app_secret)
    {
        if (empty($app_id) || empty($app_secret)) throw new \think\Exception('小程序参数错误');
        $this->appId = $app_id;
        $this->appSecret = $app_secret;
        $access_token = \think\Cache::get('wxa:access_token:' . $this->appId);
        if (!$access_token) {
            $target_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appId . "&secret=" . $this->appSecret;
            $res = $this->http_get($target_url);
            $res = json_decode($res, true);
            if (isset($res['errcode']) && $res['errcode'] != 0) {
                $content = '';
                switch (!$res || $res['errcode']) {
                    case 40001:
                        $content = "AppSecret错误或者AppSecret不属于这个小程序,请开发者确认AppSecret的正确性";
                        break;
                    case 40164:
                        $content = "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置";
                        break;
                }
                throw new ValidateException($content);
            }
            $access_token = $res['access_token'];
            $expire = $res['expires_in'] ? intval($res['expires_in']) : 7200;
            \think\Cache::set('wxa:access_token:' . $this->appId, $res['access_token'], $expire);
        }
        $this->access_token = $access_token;
    }

    //菊花码,适用于大量生成二维码,注意scene最大32个可见字符
    public function getWxQrcode($page = 'pages/index/index', $scene = '', $width = 430, $filepath = null, $qrtype = 1, $device_no = '', $loop = '')
    {
        $width = $width ? $width : 430;
        //40130 page要发布
        if (!file_exists($filepath)) {
            mkdir($filepath, 0777, true);
        }
        //存二维码
        if ($qrtype == 1) {
            $file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $this->access_token;
            $param = json_encode(array(
                "scene" => $scene,
                "page" => $page,
                "width" => $width
            ));
            $res = $this->http_post($url, $param);

            if (is_array($res2 = json_decode($res, true))) {
                $this->error = $res2['errmsg'];
                return false;
            }
            $res = file_put_contents($file, $res);
            if ($res === false) {
                $this->error = '小程序码文件写入失败';
                return false;
            }
        } else {
            $file = rtrim($filepath, DS) . DS . uniqid() . '.png';
            if (!class_exists(\addons\qrcode\library\Service::class)) {
                throw new ValidateException('请先安装插件《二维码生成》', []);
            }
            $qrcodeService = \addons\qrcode\library\Service::qrcode([
                'text' => 'https://' . $_SERVER['HTTP_HOST'] . '/UserScan/Charge?station=' . $device_no . '&loop=' . $loop,
                'errorlevel' => 'medium',
                'size' => $width,
                'format' => 'png',
                'padding' => 3,
            ]);
            $filePath = $file;
            $qrcodeService->writeFile($filePath);
        }


        if ($page == 'pages/station_led/station_led') {
            //合成
            $qCodeWidth = $width;
            $qCodeHight = $width;
            //创建白色背景
            $bigImg = imagecreatetruecolor(450, 500);
            $white = imagecolorallocate($bigImg, 255, 255, 255);
            imagefill($bigImg, 0, 0, $white);
            $qCodeImg = imagecreatefromstring(file_get_contents($file));
            //背景和二维码合成
            imagecopyresampled($bigImg, $qCodeImg, 10, 0, 0, 0, 430, 430, $qCodeWidth, $qCodeHight);
            $fonttype = './assets/addons/gaga/fonts/wqy-microhei.ttc';
            $fontcolor = imagecolorallocate($bigImg, 0x00, 0x00, 0x00);
            $content = $this->default_wxapp_settings['app_name'] . ':' . $scene;
            $fontSize = 25;
            $fontBox = imagettfbbox($fontSize, 0, $fonttype, $content);//获取文字所需的尺寸大小
            $width = imagesx($bigImg);
            imagettftext($bigImg, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $qCodeHight + 55, $fontcolor, $fonttype, $content);
            @unlink($file);
            $file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
            imagejpeg($bigImg, $file);
        } else if ($page == 'pages/station_del/station_del') {
            //合成
            $qCodeWidth = $width;
            $qCodeHight = $width;
            //创建白色背景
            $bigImg = imagecreatetruecolor(450, 500);
            $white = imagecolorallocate($bigImg, 255, 255, 255);
            imagefill($bigImg, 0, 0, $white);
            $qCodeImg = imagecreatefromstring(file_get_contents($file));
            //背景和二维码合成
            imagecopyresampled($bigImg, $qCodeImg, 10, 0, 0, 0, 430, 430, $qCodeWidth, $qCodeHight);
            $fonttype = './assets/addons/gaga/fonts/wqy-microhei.ttc';
            $fontcolor = imagecolorallocate($bigImg, 0x00, 0x00, 0x00);
            $tmp = substr($scene, 6);
            list($tmp_no, $tmp_loop) = explode('loop', $tmp);
            $content = $tmp_no . '-' . $tmp_loop;
            $fontSize = 25;
            $fontBox = imagettfbbox($fontSize, 0, $fonttype, $content);//获取文字所需的尺寸大小
            $width = imagesx($bigImg);
            imagettftext($bigImg, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $qCodeHight + 55, $fontcolor, $fonttype, $content);
            @unlink($file);
            $file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
            imagejpeg($bigImg, $file);
        }
        return $file;
    }

    public function getError()
    {
        return $this->error;
    }

    /**
     * 获取小程序二维码
     * @param $page 带参数的小程序跳转链接
     * @param $width 二维码宽度
     */
    public function getQrcode($page = 'pages/index/index', $width = '150', $filepath = null)
    {
        $width = $width ? $width : 150;
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" . $this->access_token;
        $param = json_encode(array("path" => $page, "width" => $width));
        $res = $this->http_post($url, $param);
        if (is_array($res2 = json_decode($res, true))) {
            $this->error = $res2['errmsg'];
            return false;
        }
        if ($filepath) {
            if (!file_exists($filepath)) {
                mkdir($filepath, 0777, true);
            }
            $file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';

            $res = file_put_contents($file, $res);
            if ($res === false) {
                return false;
            }
            return $file;
        } else {
            header('content-type:image/jpeg');
            die($res);
        }
    }

    /**
     * GET 请求
     * @param string $url
     */
    private function http_get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉阳软件yuyangdev_cn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值