小程序app.js使用以下代码:
App({
globalData: {
selectGoods: [], //购物车商品
topListInfo: [],
cateGoods: [],
cateGoods2: [],
httpInfo: "https://www.test.com/image/"
},
appData: {
customerInfo: null
},
onLaunch: function () {
//生成首页带参数二维码
wx.request({
url: "http://localhost/test/index.php?route=weixinprogram/qrcode/index",
success: function (res) {
console.log(res.data);
}
})
}
});
php后端代码:
<?php
class ControllerQrcode{
//测试生成小程序码
public function index(){
//url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxdf2a75a443729375&secret=3a2f38a617f4a531235809846cfa1b3b',
//url: 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' + res.data.access_token,
//拿到wxid和uid 查找经销商表内是否有该用户 没有则拒绝生成二维码 有则查看是否已生成二维码 有生成则发送数据 没有则生成
$appid = 'wxxxxxxxxxxxxxx75&secret';
$secret = '3xxxxxx17fxxxx9xxxxxx3b';
$wxid = "first";
$url_access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$json_access_token = $this -> sendCmd($url_access_token,array());
$arr_access_token = json_decode($json_access_token,true);
$access_token = $arr_access_token['access_token'];
if(!empty($access_token)) {
$url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$access_token;
$data = '{"path": "pages/index/index?customer_id=15&test_id=1", "width": 430}';
$result = $this -> sendCmd($url,$data);
$name = $wxid.time();
file_put_contents('./image/qrcode/code-'.$name.'.jpg',$result); //保存二维码
//存储二维码路径
$arr = array('code'=>1,'msg'=>'/Uploads/qrcode/code-'.$name.'.jpg');
echo json_encode($arr);
} else {
$arr = array('code'=>0,'msg'=>'ACCESS TOKEN为空!');
echo json_encode($arr);
}
}
//获取用户经销商信息 及生成推广二维码
public function qrcode()
{
//拿到wxid和uid 查找经销商表内是否有该用户 没有则拒绝生成二维码 有则查看是否已生成二维码 有生成则发送数据 没有则生成
$where = array('wxid'=>$_REQUEST['wxid'],'uid'=>$_REQUEST['uid']);
$dealer = M('r') -> where($where) -> find();
if($dealer){
if($dealer['qrcode'] == ''){
$appid = '';
$secret = '';
$wxid = $_REQUEST['wxid'];
$url_access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$json_access_token = $this -> sendCmd($url_access_token,array());
$arr_access_token = json_decode($json_access_token,true);
$access_token = $arr_access_token['access_token'];
if(!empty($access_token)) {
$url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='.$access_token;
$data = '{"path": "pages/my/my?superwxid='.$dealer['superior_level_wxid'].'&topwxid='.$dealer['top_level_wxid'].'", "width": 430}';
$result = $this -> sendCmd($url,$data);
$name = $wxid.time();
file_put_contents('./Uploads/qrcode/code-'.$name.'.jpg',$result);
//存储二维码路径
$arr = array('code'=>1,'msg'=>'/Uploads/qrcode/code-'.$name.'.jpg');
$this -> ajaxReturn($arr);
} else {
$arr = array('code'=>0,'msg'=>'ACCESS TOKEN为空!');
$this -> ajaxReturn($arr);
}
}else{
echo '获取二维码';
}
}else{
$arr = array('code'=>0,'msg'=>'');
$this -> ajaxReturn($arr);
}
}
/**
* 发起请求
* @param string $url 请求地址
* @param string $data 请求数据包
* @return string 请求返回数据
*/
function sendCmd($url,$data)
{
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $tmpInfo; // 返回数据
}
}
?>
网友评论