生成带参数小程序二维码,以及扫码后小程序接收参数

    //生成小程序二维码

public function qrcode($uid=0, $pickupCode='')
{
$access_token = $this->getAccessToken();
$scene = "{$uid}-{$pickupCode}";//携带的参数
$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
$data = array(
'scene' => $scene,
'page' => 'pages/home/index',//扫码后进入的小程序页面
'width' => 400,//宽度
'auto_color' => false,
'line_color' => array('r'=>0,'g'=>0,'b'=>0),
'is_hyaline' => false
);
$result = curl_go($url, json_encode($data),'POST');//post访问接口
$img_path = './uploads/wxcode/'.time().$uid.'.jpg';//自定义二维码路径名称
file_put_contents($img_path, $result);//写入二维码路径

$img_url = 'https://'.$_SERVER['HTTP_HOST'].'/'.$img_path;//二维码的全路径

return $img_url;
}

/**
* @return 获取access_token
*/
public function getAccessToken()
{
$config = Config::get('config');

$appid = $config[0]['value'];
$secret = $config[1]['value'];

$api = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$res = curl_go($api);
$res = json_decode($res, true);
if ($res['access_token']) {
return $res['access_token'];
}

return false;
}
小程序端接收参数

 onLoad(options) {
console.log("打印参数", decodeURIComponent(options.scene))
if (decodeURIComponent(options.scene).split('-')) {//生成二维码时使用啥符号分割的参数
let scene = decodeURIComponent(options.scene).split('-');
//获取参数
let user_id = scene[0];
let buyFoodCode = scene[1];

}

posted @   幽暗天琴  阅读(299)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示