1:这个是service的代码,直接上
<?php
/*
* @Author: wang kaiyuan @163.com
* @Date: 2023-03-21 17:32:43
* @LastEditors: wang kaiyuan @163.com
* @LastEditTime: 2023-03-21 19:03:06
* @FilePath: \server\app\api\service\SpreadcodeService.php
* @Description:
*/
namespace app\api\service;
class SpreadcodeService
{
// 获取accesstoken
public function getAccesstoken(){
$appid = 'wxf72b49e392c7b959'; /*小程序appid*/
$srcret = 'd9074076ada132a74703ccc5a402b2d7'; /*小程序秘钥*/
$tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$srcret;
$getArr=array();
$tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
$access_token=$tokenArr->access_token;
return $access_token;
}
public function send_post($url, $post_data,$method='POST') {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $method, //or GET
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
public function api_notice_increment($url, $data){
$ch = curl_init();
$header=array('Accept-Language:zh-CN','x-appkey:114816004000028','x-apsignature:933931F9124593865313864503D477035C0F6A0C551804320036A2A1C5DF38297C9A4D30BB1714EC53214BD92112FB31B4A6FAB466EEF245710CC83D840D410A7592D262B09D0A5D0FE3A2295A81F32D4C75EBD65FA846004A42248B096EDE2FEE84EDEBEBEC321C237D99483AB51235FCB900AD501C07A9CAD2F415C36DED82','x-apversion:1.0','Content-Type:application/x-www-form-urlencoded','Accept-Charset: utf-8','Accept:application/json','X-APFormat:json');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
// var_dump($tmpInfo);
// exit;
if (curl_errno($ch)) {
return false;
}else{
// var_dump($tmpInfo);
return $tmpInfo;
}
}
/*上面生成的是数量限制10万的二维码,下面重写数量不限制的码*/
/*getWXACodeUnlimit*/
/*码一,圆形的小程序二维码,数量限制一分钟五千条*/
/*45009 调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。
41030 所传page页面不存在,或者小程序没有发布*/
public function mpcode($page,$cardid){
//参数
// $postdata['scene']="nidaodaodao";
$postdata['scene']=$cardid;
// 宽度
$postdata['width']=430;
// 页面
$postdata['page']=$page;
// $postdata['page']="pages/postcard/postcard";
// 线条颜色
$postdata['auto_color']=false;
//auto_color 为 false 时生效
$postdata['line_color']=['r'=>'0','g'=>'0','b'=>'0'];
// 是否有底色为true时是透明的
$postdata['is_hyaline']=true;
$post_data = json_encode($postdata);
$access_token=$this->getAccesstoken();
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
$result=$this->api_notice_increment($url,$post_data);
// $data='image/png;base64,'.base64_encode($result);
return $result;
// echo '<img src="data:'.$data.'">';
}
/*码二,正方形的二维码,数量限制调用十万条*/
public function qrcodes(){
$path="pages/postcard/postcard";
// 宽度
$postdata['width']=430;
// 页面
$postdata['path']=$path;
$post_data = json_encode($postdata);
$access_token=$this->getAccesstoken();
$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
$result=$this->api_notice_increment($url,$post_data);
$data='image/png;base64,'.base64_encode($result);
echo '<img src="data:'.$data.'">';
}
}
2:这里是控制器的代码
// 案例详情
public function cases_share()
{
$page='pages/index/address';//自定义,前端传过来
$cardid="10";//自定义
$service = new SpreadcodeService();
$result = $service->mpcode($page,$cardid);
$share_scene = time();
$mkdir = 'upload/wxCode/';
$filePath = $mkdir.'/wxCode_' .$share_scene. '.jpg';
if(!file_exists($filePath)){
//判断是否生成二维码错误
$resdata = json_decode($result);
if(is_array($resdata) && isset($resdata['errcode'])){
file_put_contents('1.txt', $resdata, FILE_APPEND);
}
//创建目录
if(!is_dir($mkdir)){
mkdir($mkdir, 0777);
}
//将二级制图片存入服务器,file_put_contents() 方法可能有欠缺
file_put_contents($filePath,$result);
}
halt($filePath);
// $base64_image=base64_imgup_wx($result,time());
$this->success('成功!', $base64_image);
}
3:最后看下效果