公众号生成带参数二维码,扫描识别参数

   /**
     * 获取token
     */
    public function htoken(){
        $config = json_decode(\addons\shopro\model\Config::where(['name' => 'wxOfficialAccount'])->value('value'), true);
    	$appid = $config['app_id'];
        $secret = $config['secret'];
		$get_token_url =  'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$get_token_url); 
		curl_setopt($ch,CURLOPT_HEADER,0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
		$res = curl_exec($ch); 
		curl_close($ch); 
		$json_obj = json_decode($res,true); 
		return $json_obj['access_token'];
    }
/**
     * 生成二维码
     */
    public function erweima()
    {
        $type =input('param.type');
        $user_id =123;
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->htoken();
        // $this->success('获取成功',$url);
        if ($type == 1){//永久二维码
            $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": "'.$user_id.'"}}}';
            // {"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "test"}}}
        }else{
            $data = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene_id": "'.$user_id.'"}}}';
        }
        $res = $this->posts($url,'post','json',$data);
        if (empty($res['ticket'])){
            return false;
        }
        $codes='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($res['ticket']);
        $urls=$this->down_load_qrcode($codes);
        return $urls;
    }
/**
     * 下载到服务器
     */
    public function down_load_qrcode($url){
        if (empty($url)) return false;
        $filename = time()."-".rand(1000,9999).".jpg";
        ob_start();
        readfile($url);
        $img = ob_get_contents();
        ob_end_clean();
        $file = 'code'.'/'. $filename;
        $fp2 = fopen($file, "a");
        if (fwrite($fp2, $img) === false) {
            return jsonData(1001,'生成二维码失败');
        }
        fclose($fp2);
        return 'code'.'/'.$filename;
    }
  /**
     * 微信公众号服务端API对接、处理消息回复
     */
    public function index()
    {
        $wechat = new WechatLibrary('wxOfficialAccount');
        $this->app = $wechat->getApp();
        $this->app->server->push(function ($message) {
            //初始化信息
            $this->userOpenId = $message['FromUserName'];
            // return json_encode($message, JSON_UNESCAPED_UNICODE); //调试使用

            switch ($message['MsgType']) {
                case 'event': //收到事件消息
                    switch ($message['Event']) {
                        case 'subscribe': //订阅(关注)事件
                            //获取粉丝信息并保存
                            $subscribe = WechatModel::get(['type' => 'subscribe']);
                            if ($subscribe) {
                                $openid =$message['FromUserName'];
                                $userid = $message['EventKey'];
                                return $userid.'--'.$openid;
                                // qrscene_ 截取掉
                                // return $this->response($subscribe);
                            }
                            break;
                        case 'unsubscribe': //取消订阅(关注)事件
                            //获取粉丝信息并保存
                            break;
                        case 'CLICK':  //自定义菜单事件
                            return $this->response($message, 'CLICK');
                            break;
                        case 'SCAN': //扫码事件
                            $subscribe = WechatModel::get(['type' => 'subscribe']);
                            if ($subscribe) {
                                $openid =$message['FromUserName'];
                                $userid = $message['EventKey'];
                                return $userid.'--'.$openid;
                                // return  $this->response($subscribe);
                            }
                           
                            //关联 人物关系
                            break;
                    }
                    break;
                case 'text': //收到文本消息
                    //检测关键字回复
                    $content = $message['Content'];
                    $auto_reply = WechatModel::where('type', 'auto_reply')->where('find_in_set(:keywords,rules)', ['keywords' => $content])->find();
                    if ($auto_reply) {
                        return $this->response($auto_reply);
                    }
                case 'image': //收到图片消息
                case 'voice': //收到语音消息
                case 'video': //收到视频消息
                case 'location': //收到坐标消息
                case 'link': //收到链接消息
                case 'file': //收到文件消息
                default: // ... 默认回复消息
                    $default_reply = WechatModel::where('type', 'default_reply')->find();
                    if ($default_reply) {
                        return $this->response($default_reply);
                    }
            }
        });
        $response = $this->app->server->serve();
        // 将响应输出
        $response->send();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值