<?php// +----------------------------------------------------------------------// | Created by PHPstorm: [ JRK丶Admin ]// +----------------------------------------------------------------------// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.// +----------------------------------------------------------------------// | SiteUrl: http://www.luckyhhy.cn// +----------------------------------------------------------------------// | Author: LuckyHhy <jackhhy520@qq.com>// +----------------------------------------------------------------------// | Date: 2020/11/9 0009// +----------------------------------------------------------------------// | Description:// +----------------------------------------------------------------------namespace app\common\service;use think\Db;use think\Exception;class CreateAlipayQrcodeService {const APPID = "20210021"; //appidconst PRIVATEKEY = 'MII='; //私钥const PUBLICKEY = 'AQAB'; //公钥/*** @param $queryParam 参数* @param string $page 启动页* @param string $describe 描述* @param int $site_id* @param bool $domain* @return \SimpleXMLElement|string* @throws Exception* @author: Hhy <jackhhy520@qq.com>* @describe:支付宝小程序生成分享二维码*/public static function createQrcode($queryParam, $page = "pages/tabs/home", $describe = "默认站点",$site_id=0,$domain=false) {//根据你sdk存放的地址引入文件include EXTEND_PATH . "alipay/AopClient.php";include EXTEND_PATH . "alipay/request/AlipayOpenAppQrcodeCreateRequest.php";try {$aop = new \AopClient ();$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';$aop->appId = self::APPID;$rsaPrivateKey = self::PRIVATEKEY;$aop->rsaPrivateKey = trim($rsaPrivateKey);$alipayrsaPublicKey = self::PUBLICKEY;$aop->alipayrsaPublicKey = trim($alipayrsaPublicKey);$aop->apiVersion = '1.0';$aop->signType = 'RSA2';$aop->postCharset = 'UTF-8';$aop->format = 'json';$request = new \AlipayOpenAppQrcodeCreateRequest ();$request->setBizContent("{" . "\"url_param\":\"" . $page . "\"," . "\"query_param\":\"" . $queryParam . "\"," . "\"describe\":\"" . $describe . "\"" . " }");// 执行操作 请求对应的接口$result = $aop->execute($request);$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";$resultCode = $result->$responseNode->code;if(!empty($resultCode)&&$resultCode == 10000){if ($domain){//服务端使用,图片下载到本地return self::create($result->$responseNode->qr_code_url,$site_id);}else{//支付宝小程序端使用(直接返回生成的地址就行)return $result->$responseNode->qr_code_url;}}else{throw new Exception("错误代码:".$resultCode);}} catch (Exception $exception) {throw new Exception($exception->getMessage());}}/*** @param $qr_code_url* @param $site_id* @return string* @author: Hhy <jackhhy520@qq.com>* @describe:远程图片保存到本地*/protected static function create($qr_code_url,$site_id){$d=date("Ymd",time());$save_dir=ROOT_PATH."public";$dk="/alipay/".$d."/";$save_dir.=$dk;$filename=$site_id.".jpg";if (!is_dir($save_dir)){@mkdir($save_dir,0777,true);}if (file_exists($save_dir.$filename)){return $dk.$filename;}$url=$qr_code_url.".jpg";$ch = curl_init ();curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );curl_setopt ( $ch, CURLOPT_URL, $url );ob_start ();curl_exec ( $ch );$return_content = ob_get_contents();ob_end_clean ();$fp=@fopen($save_dir.$filename,"a"); //将文件绑定到流fwrite($fp,$return_content); //写入文件fclose($fp);return $dk.$filename;}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号