php 获取小程序码 (B类接口)

public function qrcode(){
	global $_W;
	global $_GPC;
	$uid = intval($_GPC['uid']);
	$member = pdo_get("ewei_shop_member",array('id'=>$uid));
	if(!empty($member['qrcode'])){
		return app_json(array('status'=>200,'url'=>$_W['siteroot'].$member['qrcode']));
	}


	$data_file  = IA_ROOT.'/attachment/images/ziliao/'.date("Y").'/'.date('m');
		//转码,把utf-8转成gb2312,返回转换后的字符串, 或者在失败时返回 FALSE。
		$filename =iconv("UTF-8","gb2312",$data_file);

		//检查文件或目录是否存在
		if(!file_exists($filename)){
			$newsfile = mkdir($filename,0777,true);
			if($newsfile){
				$filename=$data_file;
			}
		}else{
			$filename=$filename;
		}
   
	$name = $this->random(8);
	$save_url = 'attachment/images/ziliao/'.date("Y").'/'.date('m').'/'.$name.'.png';
	
	//当前用户上级id
	//$agentid = intval($_GPC['agentid']);
	

	
	$url_a  ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
	$access_token = file_get_contents($url_a);
	$ac = json_decode($access_token,1);
	
	$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ac['access_token'];
			
	$data = [
        "page"=>"pages/recruit/recruit",
        "scene"=>$uid,
        ];
    $post_data= json_encode($data,true);
	$qrcode = $this->api_notice_increment($url,$post_data);
	
	$errcode = json_decode($qrcode,true)['errcode'];
	$errmsg = json_decode($qrcode,true)['errmsg'];
	//成功的 code 为0
	if($errcode) { // 存在即为不成功
		//return app_json(array('msg'=>$errcode.":".$errmsg));	
		return app_json(array('msg'=>"生成二维码失败1"));	
	}


	$jpg = $qrcode;//得到post过来的二进制原始数据
	if(empty($jpg))
	{
		echo 'nostream';
		exit();
	}

	$file = fopen($data_file.'/'.$name.'.png',"w");//打开文件准备写入
	fwrite($file,$jpg);//写入
	fclose($file);//关闭

	$filePath = $data_file.'/'.$name.'.png';

	//$res = file_put_contents($save_url,$qrcode);//将微信返回的图片数据流写入文件

	if(!file_exists($filePath)){
		return app_json(array('msg'=>"生成二维码失败"));
	}else{
		pdo_update('ewei_shop_member',array('qrcode'=>$save_url),array('id'=>$uid));
		return app_json(array('status'=>200,'url'=>$_W['siteroot'].$save_url));
	}

}

function api_notice_increment($url,$data)
{
	$curl = curl_init();
	$a = strlen($data);
	$header = array("Content-Type: application/json; charset=utf-8","Content-Length: $a");
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl,CURLOPT_POST,1);
	curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;

}

小程序端 获取 scene

 const scene= decodeURIComponent(options.scene); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值