PC端微信登录之公众号二维码登录

  • front

  • backend

php用file_get_contents(“php://input”)或者$HTTP_RAW_POST_DATA可以接收xml数据

public function get_ACCESS_TOKEN() //获取token 
{ 
$appid= ‘wx8feca7bba41accc4’;
 $secret='beca81f0da48dc92f9610dc21648fec6';
    $data = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret")) ;

    return $data->access_token;
}
public function buildQrcode()    //生成带参数二维码
{  
    $str = $_POST['str'];
    $access_token=$this->get_ACCESS_TOKEN();
  // scene_id 参数
    $data = '{  
        "expire_seconds": 604800,  
        "action_name": "QR_SCENE",  
        "action_info": {  
            "scene": {  
                "scene_id":'.$str.'
            }  
        }  
    }';  
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    $obj = json_decode(httpRequest($url, 'POST', $data));
    $ticket=$obj->ticket;
    return json_encode(['src'=>"?ticket=$ticket",'scene_str'=>$str]);
}
public function kf_login()    //扫码客服
{
    //$GLOBALS["HTTP_RAW_POST_DATA"];  //这个用不了了;换成下面那个
    $postStr = file_get_contents("php://input");
    $postObj = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
    $openid = $postObj['FromUserName'];
    $EventKey = $postObj['EventKey'];
    $user=M('sms_log')->field('id')->where(['openid'=>$openid])->find();

    if(!$user['id']){
        $id = M('sms_log')->add(['openid'=>$openid]);
        $Login=new Login();
        $pass=$Login->get_sign('kf'.$id);
        M('sms_log')->where(['id'=>$id])->save(['pass'=>$pass,'code'=>$EventKey]);
    }else{
        M('sms_log')->where(['id'=>$user['id']])->save(['code'=>$EventKey]);
    }
}

public function pass(){  //登录
    $str = $_POST['str'];
    $user=M('sms_log')->field('id,pass')->where(['code'=>$str])->find();
    M('sms_log')->where(['code'=>$str])->save(['code'=>'']);  //每次登录后清空参数,避免出现重复
    return json_encode(['user_name'=>'kf'.$user['id'],'pass'=>$user['pass']]);
}


你可能感兴趣的:(PC端微信登录之公众号二维码登录)