首先需要注意一点 scene参数微信官方限制的为32位 请注意
public function actionShare()
{
$scene = \Yii::$app->request->post('scene');//小程序页面地址
$page = \Yii::$app->request->post('page');//页面参数
if (empty($scene) or empty($page)){
return $this->lackParameter();
}
$tokenIsExist = Token::find()->asArray()->one();
$this->delFile(\Yii::getAlias("../web/allContent/code/"));
if (!empty($tokenIsExist)){
$accessToken = $tokenIsExist['access_token'];
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";
$width = 200;
$post_data = '{"scene":"'.$scene.'","page":"'.$page.'","width":"'.$width.'"}';
$dataArr = $this->https_request($url,$post_data);
$jsonArr = json_decode($dataArr,true);
if ($jsonArr['errcode'] == '42001') {
$publicInfo = $this->wxInfo();
$appId = $publicInfo['app_id'];
$appSecret = $publicInfo['app_secret'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
$resultArr = json_decode(file_get_contents($url),true);
$tokenUpdate = Token::updateAll(['access_token'=>$resultArr['access_token'],'expires_in'=>$resultArr['expires_in'],'add_time'=>date('Y-m-d H:i:s'),'drop_time'=>date('Y-m-d H:i:s',time()+7200)]);
$tokenIsExist = Token::find()->asArray()->one();
$accessToken = $tokenIsExist['access_token'];
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$accessToken";
$width = 200;
$post_data = '{"scene":"'.$scene.'","page":"'.$page.'","width":"'.$width.'"}';
$dataArr = $this->https_request($url,$post_data);
$code = 'data:image/png;base64,'.base64_encode($dataArr);
$saveAsImage = $this->base64_image_content($code);
return $this->status(['code'=>$saveAsImage],'生成二维码失败');
}else{
$code = 'data:image/png;base64,'.base64_encode($dataArr);
$saveAsImage = $this->base64_image_content($code);
return $this->status(['code'=>$saveAsImage],'生成二维码失败');
}
}
}
//删除目录下所有文件
public function delFile($dirName)
{
if ($handle = opendir("$dirName")) {
while (($item = readdir($handle)) != false) {
if ($item != "." && $item != "..") {
if (is_dir("$dirName/$item")) {
$this->delFile("$dirName/$item");
} else unlink("$dirName/$item");
}
}
closedir($handle);
}
}
//base64另存到本地
public function base64_image_content($base64_image_content)
{
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = \Yii::getAlias("@frontend/web/allContent/code/");
if(!file_exists($new_file))
{
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}";
$path = time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return "./allContent/code/".$path;
}else{
echo '新文件保存失败';
}
}
}
function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
小程序生成二维码
最新推荐文章于 2024-06-13 10:28:48 发布