微信小程序生成二维码

第一步:微信小程序获取access_token,众所周知,微信获取access_token的次数是有限制的,并且每次获取的access_token只有两个小时的有效期,所有我们将获取的access_token存进数据表或者缓存中,这边我将存进数据表中,方法如下:

function getTokens($appid,$appsecret){//$appid 是你小程序的appid;$appsecret 是你小程序的appsecret

$mysql = new Mysql();

$info = $mysql->fetchRow("select access_token_time,access_token from hs_company where appid='{$appid}' and appsecret='{$appsecret}'");

$now=time();

if($now-$info['access_token_time']<7000)//判断存进数据表中的access_token是否过期

{

$token = $info['access_token'];

return $token;

}

else{

$token_access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";

$tokening=curl_get($token_access);

$tokens = json_decode($tokening["output"],true);

$token =$tokens["access_token"];

$arr = array(

'access_token_time'=>time(),

'access_token'=>$token

);

$where = "appid='{$appid}' and appsecret='{$appsecret}'";

$res = $mysql->update($arr,$where,'hs_company');

unset($mysql);

if($res)

{

return $token;

}

}

}

 

//获得二维码

function getcodeImg($data,$appid,$appsecret)

{

 

$token = getTokens($appid,$appsecret);//获取access_token

$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $token;

$codeImg = get_http_array($url,$data);

//$codeImg = json_decode($codeImg,true);

$codeImg=data_uri($codeImg,'image/png');

return $codeImg;

}

 

//post请求,传递参数

function get_http_array($url,$data)

{

$template = $data; //数组形式的模板内容

$json_template = json_encode($template);

$res = httpRequest($url,$json_template); //以post方式 向用户发送数据

return $res;

}

 

function data_uri($contents, $mime)

{

$base64 = base64_encode($contents);

return ('data:' . $mime . ';base64,' . $base64);

}

 

//把请求发送到微信服务器换取二维码

function httpRequest($url, $data='', $method='POST'){

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($curl, CURLOPT_AUTOREFERER, 1);

if($method=='POST')

{

curl_setopt($curl, CURLOPT_POST, 1);

if ($data != '')

{

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

}

 

curl_setopt($curl, CURLOPT_TIMEOUT, 30);

curl_setopt($curl, CURLOPT_HEADER, 0);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

curl_close($curl);

return $result;

}

 

 

 

//传递参数到方法中来获取二维码

function _getcodePic()

{

$params = getParam();//获取参数

$id = empty($params['id']) ? '' : $params['id'];//员工id

$companyId = empty($params['companyId']) ? '' : intval($params['companyId']);//企业id

if(empty($id))

{

jReturn(ErrorCode::MISSING_ARGS,'id参数不全');

}

if(empty($companyId))

{

jReturn(ErrorCode::MISSING_ARGS,'companyId参数不全');

}

$mysql = new Mysql();

$wxInfo = $mysql->fetchRow("select appid,appsecret,status from hs_company where id={$companyId}");

$codeImg = $mysql->fetchResult("select codeImg from hs_staff where id={$id} and companyId={$companyId}");

if(empty($wxInfo)) {

jReturn(ErrorCode::SELECT_FAIL,'公司信息出错');

}

if($wxInfo['status']==0)

{

jReturn(ErrorCode::MISSING_ARGS,'公司已被删除');

}

if(empty($wxInfo['appid']))

{

jReturn(ErrorCode::MISSING_ARGS,'appid参数不全');

}

if(empty($wxInfo['appsecret']))

{

jReturn(ErrorCode::MISSING_ARGS,'appsecret参数不全');

}

if(!empty($codeImg))

{

jReturn(ErrorCode::OK,$codeImg);

}

//从这开始获取二维码需要的参数

$data=array();

$scene = $id.','.$companyId;//二维码带的参数

$data['scene'] = $scene;

$data['page'] = "pages/companyDetails/companyDetails";//二维码的路径

$codeImg = getcodeImg($data,$wxInfo['appid'],$wxInfo['appsecret']);//获取二维码的图片路径

if($codeImg==false)

{

jReturn(ErrorCode::UPDATE_FAIL,"生成二维码参数出错!");

}

if (strstr($codeImg,",")) {

$image = explode(',', $codeImg);

$codeImg = $image[1];

}

$path = "/www/huishang/static/codeImg";

$imageName="qrcode{$id}.png";

if (!is_dir($path)){ //判断目录是否存在 不存在就创建

mkdir($path,0777,true);

}

$imageSrc= $path."/". $imageName; //图片名字

$res = file_put_contents($imageSrc, base64_decode($codeImg));

if($res)

{

$url = "https://hsapi.720i.cc/images/codeImg/".$imageName;

$arr = array(

'codeImg'=>$url

);

$where = "id={$id} and companyId={$companyId}";

$result = $mysql->update($arr,$where,'hs_staff');

unset($mysql);

if($result)

{

jReturn(ErrorCode::OK,$url);

}

else{

jReturn(ErrorCode::UPDATE_FAIL,"获取二维码失败!");

}

}

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值