作者 景龙
1 个管道 的构建 通过 耗费 2 秒

写第三方微博登录

... ... @@ -11,8 +11,8 @@
return [
// 应用调试模式
'app_debug' => false,
'app_debug' => true,
// 应用Trace
'app_trace' => false,
'app_trace' => true,
];
\ No newline at end of file
... ...
... ... @@ -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);
... ...
... ... @@ -57,25 +57,28 @@
<script src="__TMPL__/public/assets/js/public.js"></script>
<script>
$(function() {
$('.save_info').click(function() {
var nickname = $.trim($('#nickname').val());
$.ajax({
type: 'POST',
url: "/portal/login/updateNickname",
data: {
'nickname': nickname,
},
dataType: 'json',
success: function(data) {
console.log(data);
if (data.code == 1) {
mask(data.msg);
window.location.href = '/';
} else {
mask(data.msg);
var source = $('#source').val();
if(source == '本站'){
$('.save_info').click(function() {
var nickname = $.trim($('#nickname').val());
$.ajax({
type: 'POST',
url: "/portal/login/updateNickname",
data: {
'nickname': nickname,
},
dataType: 'json',
success: function(data) {
console.log(data);
if (data.code == 1) {
mask(data.msg);
window.location.href = '/';
} else {
mask(data.msg);
}
}
}
});
});
});
}
});
</script>
\ No newline at end of file
... ...
... ... @@ -68,6 +68,10 @@
<script src="__TMPL__/public/assets/js/uploadfile.js"></script>
<script>
$(function() {
var source = $('#source').val();
if(source != '本站'){
$("#personal-photo").attr('disabled','disabled');
}
$("#personal-photo").on("change", function() {
var img_size = $("input[type=file]").get(0).files[0].size;
if (img_size > 1024000) {
... ...
... ... @@ -36,9 +36,13 @@
<div class="WeChat">
<a href=""><img src="__TMPL__/public/assets/starImg/aicon_89.png" alt=""></a>
</div>
<div class="Sina">
<a href=""><img src="__TMPL__/public/assets/starImg/aicon_90.png" alt=""></a>
</div>
<form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form">
<div class="Sina" id="wb_login">
<img src="__TMPL__/public/assets/starImg/aicon_90.png" alt="">
</div>
<input type="hidden" name="client_id" value="737607150">
<input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
</form>
</div>
</div>
</div>
... ... @@ -49,5 +53,6 @@
Copyright © 2004-2019 独角星球 版权所有&nbsp;&nbsp;&nbsp;京ICP备17035112号-2
</div>
</body>
<script src="__TMPL__/public/assets/js/jquery-3.2.1.min.js"></script>
<script src="__TMPL__/public/assets/js/public.js"></script>
</html>
\ No newline at end of file
... ...
... ... @@ -109,6 +109,12 @@ $(function() {
$('.home_nav_act').children('a').addClass('home_nav_act');
}
var maxwidth = 4;//设置最多显示的字数
var user_nickname = $('#user_nickname').text();
if(user_nickname.length > maxwidth){
var user = user_nickname.substring(0,maxwidth);
$('#user_nickname').text(user+'...');
}
//改变分页图标
$('.page-item').eq(0).children().text('<');
$('.page-item:last').children().text('>');
... ... @@ -190,6 +196,11 @@ $(function() {
}
window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
});
//微博登录
$('#wb_login').click(function(){
$('#wb_form').submit();
});
});
//点赞收藏
... ...
... ... @@ -45,13 +45,14 @@
</form>
<notempty name="user">
<section class="home_login login_person">
<input type="hidden" value="{$user.source}" id="source">
<notempty name="user.avatar">
<img src="{:cmf_get_image_url($user.avatar)}" alt="">
<else/>
<img src="__TMPL__/public/assets/starImg/aicon_82.png" alt="">
</notempty>
<notempty name="user.user_nickname">
<span>{$user.user_nickname}</span>
<span id="user_nickname">{$user.user_nickname}</span>
<else/>
<span></span>
</notempty>
... ...
... ... @@ -59,26 +59,29 @@
<script src="__TMPL__/public/assets/js/public.js"></script>
<script>
$(function(){
$('.save_info').click(function(){
var nickname = $.trim($('#nickname').val());
$.ajax({
type: 'POST',
url: "/portal/login/updateNickname",
data: {
'nickname': nickname,
},
dataType: 'json',
success: function (data) {
console.log(data);
if (data.code == 1) {
mask(data.msg);
window.location.href = '/';
} else {
mask(data.msg);
var source = $('#source').val();
if(source == '本站'){
$('.save_info').click(function(){
var nickname = $.trim($('#nickname').val());
$.ajax({
type: 'POST',
url: "/portal/login/updateNickname",
data: {
'nickname': nickname,
},
dataType: 'json',
success: function (data) {
console.log(data);
if (data.code == 1) {
mask(data.msg);
window.location.href = '/';
} else {
mask(data.msg);
}
}
}
});
});
});
}
});
</script>
... ...
... ... @@ -81,6 +81,10 @@
<script src="__TMPL__/public/assets/js/uploadfile.js"></script>
<script>
$(function(){
var source = $('#source').val();
if(source != '本站'){
$("#personal-photo").attr('disabled','disabled');
}
$("#personal-photo").on("change",function(){
var img_size = $("input[type=file]").get(0).files[0].size;
if (img_size > 1024000){
... ...
... ... @@ -51,14 +51,20 @@
微信登录
</div>
</div>
<div class="index_tenth_second_one">
<div class="index_tenth_second_img">
<img src="__TMPL__/public/assets/images/cicon_72@2x.png" alt="" />
</div>
<div class="index_tenth_second_title">
微博登录
<form action="https://api.weibo.com/oauth2/authorize" method="POST" id="wb_form">
<div class="index_tenth_second_one">
<div class="index_tenth_second_img" id="wb_login">
<img src="__TMPL__/public/assets/images/cicon_72@2x.png" alt="" />
</div>
<div class="index_tenth_second_title">
微博登录
</div>
<input type="hidden" name="client_id" value="737607150">
<input type="hidden" name="redirect_uri" value="http://www.xingqiu.cn/portal/login/wb_login">
</div>
</div>
</form>
</div>
</div>
</div>
... ...
... ... @@ -117,6 +117,14 @@ $(function() {
} else {
$('.show_main_more').show();
}
var maxwidth = 4;//设置最多显示的字数
var user_nickname = $('#user_nickname').text();
if(user_nickname.length > maxwidth){
var user = user_nickname.substring(0,maxwidth);
$('#user_nickname').text(user+'...');
}
//改变分页图标
$('.page-item').eq(0).children().text('<');
$('.page-item:last').children().text('>');
... ... @@ -224,6 +232,11 @@ $(function() {
window.open('http://v.t.sina.com.cn/share/share.php?' + temp.join('&'));
});
//微博登录
$('#wb_login').click(function(){
$('#wb_form').submit();
});
//分享到微信
// $('.weixin').click(function(){
// var target_url = "http://qr.liantu.com/api.php?text="+share_url;
... ...
... ... @@ -23,6 +23,7 @@
<notempty name="user">
<!--登陆后-->
<div class="nav_downBtn login_person">
<input type="hidden" value="{$user.source}" id="source">
<notempty name="user.avatar">
<div class="nav_downImg">
<img src="{:cmf_get_image_url($user.avatar)}" alt="" />
... ... @@ -34,7 +35,7 @@
</notempty>
<notempty name="user.user_nickname">
<p class="nav_downName">{$user.user_nickname}</p>
<p class="nav_downName" id="user_nickname">{$user.user_nickname}</p>
<else/>
<p class="nav_downName"></p>
</notempty>
... ... @@ -95,5 +96,6 @@
<!--<img class="nav_downImg2" src="__TMPL__/public/assets/images/down.png" alt="" />-->
</div>
</div>
</div>
</div>
\ No newline at end of file
... ...