// 生成带参数的二维码 $dot_id 二维码里的参数
public function generate($dot_id){
// 获取acckenhere 并且存入数据库
$where[‘id‘]=1;
$accken_token= db("wx_acctoken")->where($where)->find();
$token=$accken_token[‘accken_token‘];
if(($accken_token[‘ad_time‘]+6500)<=time()){
$accken_token=$this->getWxAccessToken();
$data[‘accken_token‘]=$accken_token[‘access_token‘];
$data[‘ad_time‘]=time();
$ud= Db::name(‘wx_acctoken‘)->where($where)->update($data);
$token=$accken_token[‘access_token‘];
}
// $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$token;
$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token";
//$param = json_encode(array("path"=>"pages/index/index?shop_id=".$shop_id."&uid=".$uid."&enter=1","width"=> 100));
$param =json_encode(array("path"=>"pages/start_the/start_the?dot_id=".$dot_id."","width"=> 100));
$result=$this->send_post( $qcode, $param,"POST");
$base64_image = "data:image/jpeg;base64,".base64_encode( $result );
$new_name = $this->base64_save($base64_image,‘./qrcode/‘);
//查二维码
// $code_url = $this->generate($res);
$up =array(
‘token‘=>$new_name
);
Db::name("information")->where("id",$dot_id)->update($up);
return $new_name;
}
// 获取AccessToken
public function getWxAccessToken(){
$appid=‘*****‘;
$appsecret=‘***‘;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$access_token = $this->makeRequest($url);
$access_token = json_decode($access_token[‘result‘],true);
return $access_token;
}
protected function send_post( $url, $post_data ) {
$options = array(
‘http‘ => array(
‘method‘ => ‘POST‘,
‘header‘ => ‘Content-type:application/json‘,
//header 需要设置为 JSON
‘content‘ => $post_data,
‘timeout‘ => 60
//超时时间
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;
}
/*
* 保存base64格式图片到指定文件夹中
* $base64_img 图片数据
* $path 要保存到的图片地址
*/
function base64_save($base64_img,$path){
$img_base = str_replace(‘data:image/jpeg;base64,‘, ‘‘, $base64_img);
$img_name = time().rand(1000,9999).‘.jpg‘; //图片新名称
$img_path = $path.$img_name;
file_put_contents($img_path,base64_decode($img_base));
$url= $this->tailoringImg($img_path);
return $url;
}
// 将微信下面的一行字切掉
function tailoringImg($url)
{
$file_path =$url;
$save_width = 390;
$start_spot_x = 10;
$start_spot_y = 15;
$width = 260;
$height = 255;
if(file_exists($file_path) && is_readable($file_path))
{
//从字符串中的图像流新建一图像
$src = imagecreatefromstring(file_get_contents($file_path));
//保存图片的高
$save_height = round($save_width*$height/$width);
//根据要保存的宽和高创建图片
$new_image = imagecreatetruecolor($save_width, $save_height);
//生成最后的图片
imagecopyresampled($new_image, $src, 0, 0, $start_spot_x, $start_spot_y, $save_width, $save_height, $width, $height);
//直接展示
$rul= "./qrcode/".time().rand(1000,9999).".jpg";
imagejpeg($new_image,$rul);
imagedestroy($new_image);
$url=substr($rul,1);
return $url;
}
else
{
echo ‘文件查看失败‘;//记录日志
}
}
public function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = ‘‘)
{
if (empty($url)) {
return array(‘code‘ => ‘100‘);
}
$_curl = curl_init();
$_header = array(
‘Accept-Language: zh-CN‘,
‘Connection: Keep-Alive‘,
‘Cache-Control: no-cache‘
);
// 方便直接访问要设置host的地址
if (!empty($hostIp)) {
$urlInfo = parse_url($url);
if (empty($urlInfo[‘host‘])) {
$urlInfo[‘host‘] = substr(DOMAIN, 7, -1);
$url = "http://{$hostIp}{$url}";
} else {
$url = str_replace($urlInfo[‘host‘], $hostIp, $url);
}
$_header[] = "Host: {$urlInfo[‘host‘]}";
}
// 只要第二个参数传了值之后,就是POST的
if (!empty($params)) {
curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($_curl, CURLOPT_POST, true);
}
if (substr($url, 0, 8) == ‘https://‘) {
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($_curl, CURLOPT_URL, $url);
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_curl, CURLOPT_USERAGENT, ‘API PHP CURL‘);
curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);
if ($expire > 0) {
curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间
curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
}
// 额外的配置
if (!empty($extend)) {
curl_setopt_array($_curl, $extend);
}
$result[‘result‘] = curl_exec($_curl);
$result[‘code‘] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
$result[‘info‘] = curl_getinfo($_curl);
if ($result[‘result‘] === false) {
$result[‘result‘] = curl_error($_curl);
$result[‘code‘] = -curl_errno($_curl);
}
curl_close($_curl);
return $result;
}