一、项目背景
初衷
最近项目用到了生成小程序二维码,发现还是有一些需要注意的点,所以就有了这篇文章,希望对遇到同样项目的你有所帮助。
项目实战
场景介绍
针对不同的用户类型,需要生成相应的小程序码,携带所需参数。
通过仔细阅读微信官方文档【参考文档地址】 ,这里使用【获取无限制小程序码】接口。
php代码实现
// 生成小程序码方法封装=
public static function getQrCode($staff_id)
{
= $options = [
'app_id' => env('APPID'),
'secret' => env('SECRET'),
];
// 获取小程序分享=码
$app = Factory::miniProgram($options);
//设置本地保存地址
$date = date('Ym').'/'.date('d');
$qr_path = public_path('/upload/'.'qrcode/'.$date);
$filename = 'miniProgram_'.$staff_id.'.png';
= $param['page'] = 'xxxxxxx';
$response = $app->app_code->getUnlimit("sid=".$staff_id, [
'page' => $param['page'], //跳转的页面
'width' => 600,
]);
// 保存小程序码到文件
$resfilename='';
if ($response instanceof StreamResponse) {
$resfilename = $response->saveAs($qr_path,$filename);
$resfilename = env('APP_URL').'upload/'.'qrcode/'.$date.'/'.$filename;
}
return $resfilename;
}
小程序端获取参数
// 小程序实现
Page({
onLoad(options) {
if (options.scene) {
let scene = decodeURIComponent(options.scene)
this.setData({
scene: scene,
sceneObj: urlSearchParse(scene)
})
}
},
})
小结
重要的点是小程序码的应用场景,根据不同需求使用对应的接口,充分体现了认真阅读文档的重要性,少走弯路。