(一)編寫 trait 類
(1)注意事項,小程式必須要有一個釋出版本才可以!!!
(2)程式碼親測有效
(3)程式碼是在tp6環境下,生成的路徑可根據當前Tp版本自行修改
<?php
namespace app\common\traits;
trait QrcodeMin
{
//獲取配置
protected static function _C($type){
$arr=[
'APPID'=>'wxba595', //你的APPID
'APP_SECRET'=>'7bfd443' //你的app_secret
];
return $arr[$type];
}
/**
* @param null $param
* @param bool $domain
* @return string
* @author: Hhy <jackhhy520@qq.com>
* @describe:生成小程式太陽碼
*/
public static function min_qrcode($param=null,$domain=false)
{
if (empty($param)){
$param = "site_id=4";
}else{
if (is_array($param)){
$param = http_build_query($param);
}
}
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . self::getAccessToken();
//生成二維碼圖片
$da['page'] = 'pages/index/index'; //小程式路徑地址,不寫預設跳首頁
$da['width'] = 430; //二維碼大小
$da['scene'] = $param; //頁面傳參
$post_data = json_encode($da);
//這裡會直接生成base64圖片.直接寫成檔案就可以 列印會顯示亂碼
$result = self::api_notice_increment($url, $post_data);
$n = date("Ym");
$dir = app()->getRootPath() . '/public/' . 'qrcode/min/'.$n;
//判斷目錄是否存在
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
//生成唯一檔名
$file =uniqid().'.png';
$filename=app()->getRootPath() . '/public/qrcode/min/'.$n."/".$file;
//寫入檔案
file_put_contents($filename,$result);
$f='/qrcode/min/'.$n."/".$file;
if ($domain){
return request()->domain().$f;
}else{
return $f;
}
}
/**
* @param $file
* @author: Hhy <jackhhy520@qq.com>
* @describe:刪除二維碼檔案
*/
public static function delQrcode($file)
{
//這裡傳入的$file 是我這邊存入資料庫的圖片.呼叫unlink函式刪除伺服器上的圖片檔案
$path = app()->getRootPath() .'public'.$file;
if (file_exists($path)) {
@unlink ($path);
};
}
//新增日誌
public static function add_log($data,$path=""){
if (empty($path)) $path="min_qrcode";
$file = date("Y_m_d", time());
$jia=date("Ym");
$file_path = app()->getRootPath()."Logs/".$path."/".$jia;
if (!is_dir($file_path)) {
mkdir($file_path, 0777, true);
}
@file_put_contents(
$file_path.'/'.$file.'.log',
date('Y-m-d H:i:s', time()).' '.json_encode($data)."\r\n",
FILE_APPEND
);
}
/**
* @return mixed
* 獲取accessToken
*/
public static function getAccessToken()
{
$tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::_C("APPID")."&secret=".self::_C("APP_SECRET");
$getArr=array();
$tokenArr=json_decode(self::send_post($tokenUrl,$getArr,"GET"));
$access_token=self::checkWXToken($tokenArr->access_token);
return $access_token;
}
/**
* @param $access_token
* @return mixed
* @author: Hhy <jackhhy520@qq.com>
* @describe: 驗證access_token是否有效
*/
protected static function checkWXToken($access_token){
//請求微信不限制呼叫次數的介面
$ipurl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$access_token;
$ipresult = self::getSSLPage($ipurl);
$ipdata = json_decode($ipresult,true);
if($ipdata['errcode'] == '40001'){
self::add_log(date('Y-m-d H:i:s').' access_token提前失效,進入二次獲取token'.PHP_EOL,"access_token");
$access_token = self::getAccessToken();
}
return $access_token;
}
/**
* @param $url
* @param $data
* @return bool|string
*/
protected static function api_notice_increment($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
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);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;die;
}
}
/**
* @param $url
* @param $post_data
* @param string $method
* @return false|string
* 傳送post請求
*/
protected static 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;
}
/**
* @param $url
* @return bool|string
*
*/
protected static function getSSLPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION, 30);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結