laravel框架生成带参数的小程序二维码(菊花码)

思路:前端直接走php的API接口获取到生成的小程序二维码地址

微信小程序官方文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html

这里我们用了 EasyWeChat 是一个开源的 微信 非官方 SDK。比较方便开发,不用重复造轮子,首页附上链接https://www.easywechat.com/docs
EasyWeChat生成小程序码文档链接 :https://www.easywechat.com/docs/4.1/mini-program/app_code

小程序二维码有三种获取小程序模式,根据自己的业务去决定,我这里用的是wxacode.getUnlimited第三种(因为要生成多个无限制的二维码),具体官链:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html

image.png

附上代码:

//前面要引入这个SDK
use EasyWeChat\Kernel\Http\StreamResponse;
/**
     * 【小程序】分享达人二维码
     * @param string token Y token 121312
     * @param string page  Y 小程序跳转的页面 'pages/login/login'
     * @return data        Y  状态码  2000(成功)
     * @success int code Y  状态码  2000
     * @success string qrcode_url Y     二维码图片地址
     */
    public function getMiniCodeBuffer(Request $request)
    {
        //获取提交过来的page 页面
        $param = $this->validate($request,[
            'page' => 'string|required',
        ]);

        //验证待校验
        $userInfo = session('userInfo');
       

        $userid = $userInfo['user_id'];
        $appId = $userInfo['app_id'];

        //获取配置信息  这里查询数据库 拿到了小程序的appId和secret,根据自己情况而定
        $res = DB::table('wechats')->where([
            ['wechat_app_id','=',$appId]
        ])->select('wechat_app_id', 'wechat_secret')->get();

        if (empty($res)){
            $this->errorJson(Define_Const::FAIL,'数据库没有当前小程序的密钥');
        }

        $res = $res -> toArray();
        $res = $res[0];
        
        //配置,具体可以看EasyWeChat 文档
        $config = [
            'app_id'=>$res->wechat_app_id,
            'secret'=>$res->wechat_secret,
            'response_type' => 'array',
        ];

        //设置本地保存地址
        $path = public_path('upload/Qrcode/'); // 本地绝对路径,存放生成的二维码
        $filename = 'miniProgram_'.$userid.'.png';

        $app = Factory::miniProgram($config);   //初始化 具体看EasyWeChat 文档,有例子

        //"uid=".$userid 这里就是参数,进去之后前端需要处理这个参数,可以传输用户id之类的根据 业务场景来
        $response = $app->app_code->getUnlimit("uid=".$userid, [
            'page' => $param['page'],    //跳转的页面
//            'width' => 600,
        ]);
        // $response 成功时为 EasyWeChat\Kernel\Http\StreamResponse 实例,失败为数组或你指定的 API 返回类型
        Log::info('小程序$response::'.json_encode($response,JSON_UNESCAPED_UNICODE));

        //保存文件到本地   也可以存储在oss,根据自己业务来
        if ($response instanceof StreamResponse) {
            $file = $response->saveAs($path,$filename);  //保存文件的操作
          
            $result['qrcode_url'] =  $path.$file;
            $this->success($result);
        }
        //失败了就将错误码打印出来,这里如果发生错误,返回的是微信官方的错误码,自己再去查阅是什么原因
        $this->errorJson(Define_Const::FAIL,'生成失败',$response);
      
    }


生成的小程序是永久有效的,理论上其实可以存到缓存里面,不用每次去生成,这里我就不写了,只是提供一个思路

EasyWechat文档的内容,比较简单


image.png

生成小程序二维码时候有个坑!!!就是生成的二维码一定是已经发布的小程序的其中一个页面,不然就会报错!!!

最后附上可以参考的所有文章链接:

【官方教程】获取小程序码
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html

微信小程序生成带参数的二维码
https://blog.csdn.net/weixin_42799222/article/details/82772761#_2

js生成小程序某页面二维码(生成小程序二维码)
https://blog.csdn.net/qq_41473887/article/details/81335977

PHP生成带参数的微信小程序二维码
https://blog.csdn.net/u012628581/article/details/85988662

PHP生成微信小程序二维码,可生成带参数二维码。
https://blog.csdn.net/weixin_39927850/article/details/81038024?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
EasyWeChat v4.1 开发文档
https://www.bookstack.cn/read/EasyWeChat-v4.1/mini-program-app_code.md

微信小程序之生成自定义参数小程序二维码
https://www.jianshu.com/p/3056754987e8

php 获取小程序二维码返回的 Buffer二进制数据 保存图片 全套代码
https://blog.csdn.net/weixin_40024174/article/details/100144801?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allfirst_rank_v2~rank_v25-1-100144801.nonecase

PHP如何把图片base64转为buffer?
http://www.vephp.com/jiaocheng/14785.html

小程序canvas使用网络图片真机不显示解决方案----可直接使用案例测试
https://blog.csdn.net/fangkang7/article/details/82711830/
小程序canvas使用网络图片真机不显示解决方案
https://blog.csdn.net/qq_37546835/article/details/102552531

js将本地路径的图片转化为base64格式
https://www.jianshu.com/p/d4c471759b7b
请问uniapp如何将图片绝对路径转化为BASE64格式?
https://segmentfault.com/q/1010000019120571

你可能感兴趣的:(laravel框架生成带参数的小程序二维码(菊花码))