...
|
...
|
@@ -19,6 +19,9 @@ use think\Config; |
|
|
class LoginController extends HomeBaseController
|
|
|
{
|
|
|
private $limit = 8;//收藏,搜索分页
|
|
|
private $appkey = 737607150;//微博appkey
|
|
|
private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
|
|
|
private $redirect_uri = 'http://www.xingqiu.cn/portal/login/wb_login';//回调地址
|
|
|
//登录页面
|
|
|
public function login(){
|
|
|
return $this->fetch();
|
...
|
...
|
@@ -94,6 +97,7 @@ class LoginController extends HomeBaseController |
|
|
$info['mobile'] = $param['mobile'];
|
|
|
$info['user_pass'] = cmf_password($param['user_pass']);
|
|
|
$info['user_type'] = 2;
|
|
|
$info['source'] = '本站';
|
|
|
$info['create_time'] = time();
|
|
|
$res = $userModel->allowField(true)->save($info);
|
|
|
if($res){
|
...
|
...
|
@@ -355,6 +359,83 @@ class LoginController extends HomeBaseController |
|
|
var_dump($res);exit;
|
|
|
}
|
|
|
|
|
|
//第三方微博登录
|
|
|
public function wb_login(){
|
|
|
$code = $this->request->get('code');
|
|
|
$url = 'https://api.weibo.com/oauth2/access_token';
|
|
|
//要传的数据
|
|
|
$data = [
|
|
|
'client_id' => $this->appkey,
|
|
|
'client_secret'=>$this->appsecret,
|
|
|
'grant_type'=>'authorization_code',
|
|
|
'code'=>$code,
|
|
|
'redirect_uri'=>$this->redirect_uri //回调地址
|
|
|
];
|
|
|
$res = $this->http_post($url,$data);
|
|
|
$json_arr = json_decode($res,true);
|
|
|
//获取access_token
|
|
|
if(isset($json_arr['error_code'])&&!empty($json_arr['error_code'])){
|
|
|
//用户取消登录
|
|
|
$this->redirect('/portal/login/thirdLogin');
|
|
|
}
|
|
|
$token = $json_arr['access_token'];
|
|
|
//存token到session
|
|
|
session('token', $token);
|
|
|
$uid = $json_arr['uid'];
|
|
|
//发送get请求,获取登陆用户的信息
|
|
|
$info = $this->http_get('https://api.weibo.com/2/users/show.json?access_token='.$token.'&uid='.$uid);
|
|
|
$info = json_decode($info,true);
|
|
|
|
|
|
//查询该微博用户是否存在
|
|
|
$where = ['wb_id'=>$info['id'],'source'=>'微博'];
|
|
|
$user = $this->findThird($where);
|
|
|
//获取微博id,昵称,头像
|
|
|
$userModel = new UserModel();
|
|
|
if($user){
|
|
|
$users['user_nickname'] = $info['screen_name'];
|
|
|
$users['avatar'] = $info['profile_image_url'];
|
|
|
$userModel->where(['wb_id'=>$info['id'],'source'=>'微博'])->update($users);
|
|
|
}else{
|
|
|
$users['wb_id'] = $info['id'];
|
|
|
$users['user_nickname'] = $info['screen_name'];
|
|
|
$users['avatar'] = $info['profile_image_url'];
|
|
|
$users['source'] = '微博';
|
|
|
$users['user_type'] = 2;
|
|
|
$users['create_time'] = time();
|
|
|
$userModel->create($users);
|
|
|
}
|
|
|
$userInfo = $this->findThird($where);
|
|
|
cmf_update_current_user($userInfo);
|
|
|
$this->redirect('/');
|
|
|
}
|
|
|
|
|
|
//微博分享
|
|
|
public function wb_share(){
|
|
|
$token = session('token');
|
|
|
$url = 'https://api.weibo.com/2/statuses/share.json';
|
|
|
$data = [
|
|
|
'access_token' => $token,
|
|
|
'status' => URLencode('http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=52')
|
|
|
];
|
|
|
$res = $this->http_post($url,$data);
|
|
|
$json_arr = json_decode($res,true);
|
|
|
var_dump($json_arr);exit;
|
|
|
}
|
|
|
|
|
|
//取消授权
|
|
|
public function wb_cancel(){
|
|
|
echo '取消了';
|
|
|
// $this->redirect('/');
|
|
|
}
|
|
|
|
|
|
//查询第三方用户是否存在
|
|
|
public function findThird($where){
|
|
|
$info = Db::name('user')
|
|
|
->where($where)
|
|
|
->find();
|
|
|
return $info;
|
|
|
}
|
|
|
|
|
|
//curl get请求
|
|
|
public function http_get($url){
|
|
|
$curl = curl_init();//启动一个CURL会话
|
...
|
...
|
@@ -370,16 +451,16 @@ class LoginController extends HomeBaseController |
|
|
}
|
|
|
|
|
|
//curl post请求
|
|
|
public function http_post($url,$data,$headers){
|
|
|
public function http_post($url,$data){
|
|
|
$curl = curl_init();//启动一个CURL会话
|
|
|
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
|
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
|
|
|
curl_setopt($curl, CURLOPT_HEADER, true); // 开启header
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
|
|
|
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
|
$result = curl_exec($curl); //执行操作
|
|
|
curl_close($curl);
|
...
|
...
|
|