微信生成公众号带参数二维码(一)

来源:互联网 发布:儿童电脑控制软件 编辑:程序博客网 时间:2024/05/16 09:09



/**
     * 获取微信token 值  这个是普通token值
     */
    public function getaccess_token() {
        $access_tokenstr = S('wechat_token');
        if ($access_tokenstr) {
            return $access_tokenstr;
        }
        $wechat = C('wechat');  //微信配置
        $sAppId = $wechat['AppId'];  
        $APPSECRET = $wechat['AppSecret'];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $sAppId . '&secret=' . $APPSECRET;
        $r = curlrequest($url);
        $arr = json_decode($r, true);
        $expirestime = isset($arr['expires_in']) ? intval($arr['expires_in']) : 7000;
        if (isset($arr['access_token'])) {
            S('wechat_token', $arr['access_token'], $expirestime);
        }
        return $arr['access_token'];
    }

    /**
     * 公众号二维码生成步聚一
     */
   public function get_users_ticket($uid){
       $users_info=D("UsersLogin")->where(array('users_id'=>$uid))->find();
       if(empty($users_info["qr_code"])){
       //获取 token值
        $access_token = $this->getaccess_token();
        $url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
        $r = curlrequest($url,'{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$uid.'"}}}',"post");
          $data = json_decode($r, 1);

 // echo '<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$data['ticket'].'"/>';

// exit;


          //将图片上传到七牛上然后返回 key值
        $url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$data['ticket'];
         $date_path = date('Y-m-d', time());
        $dest = './Uploads/Users/' . $date_path . "/";
        if (!is_dir($dest)) {
            $this->createDir($dest);
        }
        $name_pic = time() . rand(1, 100) . '.jpg';
        $dest.=$name_pic;
        copy($url, $dest);
        $key_val=qiniu_upload_image($dest);
        D("UsersLogin")->where(array('users_id'=>$uid))->save(array('qr_code'=>$key_val));
        return $key_val;
       }else{
           return $users_info["qr_code"];
       }

   }



0 0
原创粉丝点击