TP5(小程序二维码生成)代码示例:
//小程序二维码生成(已测试)
public function getQRcode($getData){
$this->projectName='zhihuiceping';//项目文件名称
$getData=json_decode($getData,true);
$userId=isset($getData['userId'])?$getData['userId']:'';
$productId=isset($getData['productId'])?$getData['productId']:'';
$type=$getData['type'];
$orderId=isset($getData['orderId'])?$getData['orderId']:'';
$adminId=isset($getData['adminId'])?$getData['adminId']:'';
switch ($type) {
case 'adminIdPoster':
//商品详情
$name=$this->projectName.'adminIdPoster'.$adminId;
$data = array(
'path'=>'pages/home/index?adminId='.$adminId,
'auto_color' => false,
'width' => 400,
// 'is_hyaline'=>true
);
break;
case 'product':
//商品详情
$name=$this->projectName.'product'.$userId.$productId;
$data = array(
'path'=>'pages/products/detail/index?id='.$productId.'&fid='.$userId,
'auto_color' => false,
'width' => 400,
'is_hyaline'=>true
);
break;
case 'distribution':
//分销商
$name=$this->projectName.'distribution'.$userId;
$data = array(
'path'=>'pages/member/distribution/register/index?fid='.$userId,
'auto_color' => false,
'width' => 400,
'is_hyaline'=>true
);
break;
case 'groupOrder':
//邀请拼团
$name=$this->projectName.'groupOrder'.$orderId;
$data = array(
'path'=>'pages/groupBuy/joinGroupBuy/index?id='.$orderId,
'auto_color' => false,
'width' => 400,
'is_hyaline'=>true
);
break;
default:
# code...
break;
}
//获取微信access_token;
$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$this->getAccessToken();
$qrcode=Cache::get($name);
if(!empty($qrcode)){
//不为空
return $qrcode;
}
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept: application/json",
"Cache-Control: no-cache",
"Pragma: no-cache"
);
$jsonStr = json_encode($data);
$img = http($url, $jsonStr, 1, $headers);
$dir=DOC_ROOT."/qrcode";
if (!file_exists($dir)){
mkdir ($dir,0777,true);
//echo '创建文件夹成功';
}
$savePath ="qrcode/";
$saveName = $userId.time(). '.png';
//存入二维码
$res = file_put_contents(DOC_ROOT.'/'.$savePath.$saveName,$img);
if ($res === false) {
return 0;
}else{
$qrcodeUrl=IMG_PATH.$savePath.$saveName;
Cache::set($name,$qrcodeUrl);
return $qrcodeUrl;
}
}
//access_token获取频次2000/天,一小时内有效
public function getAccessToken(){
$config=Db::table('ims_wx_config')->field('appid,appsecret,access_time,access_token')->where('id',1)->find();
if($config['access_time']< time()){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config['appid']."&secret=".$config['appsecret'];
$tmp=CURLSend($url,'get');
$obj=json_decode($tmp);
$data['access_time']=(time()+3600);//一小时
$data['access_token']=$obj->access_token;
Db::name('wx_config')->where('id=1')->update($data);
return $obj->access_token;
}else {
return $config['access_token'];
}
}
/**
* http请求
* @param string $url 请求地址
* @param boolean|string|array $params 请求数据
* @param integer $ispost 0/1,是否post
* @param array $header
* @param $verify 是否验证ssl
* @return string|boolean 出错时返回false
*/
function http($url, $params = false, $ispost = 0, $header = array(), $verify = false) {
$httpInfo = array();
$ch = curl_init();
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//忽略ssl证书
if($verify === true){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
} else {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if (is_array($params)) {
$params = http_build_query($params);
}
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log/http.log', "cURL Error: " . curl_errno($ch) . ',' . curl_error($ch).PHP_EOL.PHP_EOL, FILE_APPEND);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log/http.log', var_export($httpInfo, true).PHP_EOL.PHP_EOL, FILE_APPEND);
return false;
}
curl_close($ch);
return $response;
}
/*
*模拟浏览器发送
*/
function CURLSend($url,$method = 'get', $data = ''){
$ch = curl_init();//初始化
$headers = array('Accept-Charset: utf-8');
//设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, $url);//指定请求的URL
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));//提交方式
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不验证SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不验证SSL
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头字段的数组
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)');//头的字符串
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);//自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交数值
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否输出到屏幕上,true不直接输出
$temp = curl_exec($ch);//执行并获取结果
curl_close($ch);
return $temp;//return 返回值
}