270 lines
10 KiB
PHP
270 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* @Created by PhpStorm.
|
|
* @Author:Soar
|
|
* @Time:2024/1/8 13:40
|
|
*/
|
|
|
|
namespace app\api\controller;
|
|
|
|
use addons\third\library\Service;
|
|
use addons\third\model\Third;
|
|
use app\common\controller\Api;
|
|
use app\common\model\User;
|
|
use think\Cookie;
|
|
use think\Hook;
|
|
use think\Validate;
|
|
|
|
class Tiktok extends Api
|
|
{
|
|
protected $noNeedLogin = ['login', 'test', 'bind', 'check'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
private $appid = "tt363d4080a90e4c4c01";
|
|
private $secret = "1e0d18a93c17442a518436fd2b06147fc90cbfe3";
|
|
private $url = "https://open-sandbox.douyin.com/api/apps/v2/jscode2session";
|
|
|
|
public function login()
|
|
{
|
|
$auth = $this->auth;
|
|
|
|
//监听注册登录注销的事件
|
|
Hook::add('user_login_successed', function ($user) use ($auth) {
|
|
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
|
|
Cookie::set('uid', $user->id, $expire);
|
|
Cookie::set('token', $auth->getToken(), $expire);
|
|
});
|
|
Hook::add('user_register_successed', function ($user) use ($auth) {
|
|
Cookie::set('uid', $user->id);
|
|
Cookie::set('token', $auth->getToken());
|
|
});
|
|
Hook::add('user_logout_successed', function ($user) use ($auth) {
|
|
Cookie::delete('uid');
|
|
Cookie::delete('token');
|
|
});
|
|
|
|
$platform = $this->request->param('platform');
|
|
$third_id = $this->request->param('third_id');
|
|
|
|
$userinfo = Third::get($third_id);
|
|
|
|
if (!empty($userinfo)) {
|
|
$userinfo = $userinfo->toArray();
|
|
} else {
|
|
$this->error("未找到您的绑定信息,请先绑定用户!");
|
|
}
|
|
// 授权成功后的回调
|
|
$loginret = Service::connect($platform, $userinfo);
|
|
if ($loginret) {
|
|
$this->result("登陆成功", ["token" => $auth->getToken(), "user_id" => $auth->getUser()->id]);
|
|
} else {
|
|
$this->error("登录失败,请重试");
|
|
}
|
|
}
|
|
|
|
public function check()
|
|
{
|
|
$code = $this->request->param("code");
|
|
$anonymous_code = $this->request->param("anonymous_code", "");
|
|
if (empty($code)) {
|
|
$this->error("param error");
|
|
}
|
|
// 根据 code 获取抖音 openid
|
|
$data['appid'] = $this->appid;
|
|
$data['secret'] = $this->secret;
|
|
$data['code'] = $code;
|
|
$data['anonymous_code'] = $anonymous_code;
|
|
$result = $this->curlPost($this->url, $data, 5, "", "json");
|
|
$result = json_decode($result, true);
|
|
|
|
if ($result['err_no'] == 0) {
|
|
// 如果返回 openid 则去查询是否存在数据库绑定
|
|
$third = new Third();
|
|
$douyin_third = $third->where("openid", $result['data']['openid'])
|
|
->where("unionid", $result['data']['unionid'])
|
|
->where("platform", "douyin")
|
|
->where("apptype", "dy")
|
|
->find();
|
|
|
|
if (empty($douyin_third)) {
|
|
$this->result("请先绑定用户", [
|
|
'openid' => $result['data']['openid'],
|
|
'unionid' => $result['data']['unionid'],
|
|
], 2);
|
|
} else {
|
|
// 如果不为空 则直接发起登录
|
|
$auth = $this->auth;
|
|
|
|
//监听注册登录注销的事件
|
|
Hook::add('user_login_successed', function ($user) use ($auth) {
|
|
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
|
|
Cookie::set('uid', $user->id, $expire);
|
|
Cookie::set('token', $auth->getToken(), $expire);
|
|
});
|
|
Hook::add('user_register_successed', function ($user) use ($auth) {
|
|
Cookie::set('uid', $user->id);
|
|
Cookie::set('token', $auth->getToken());
|
|
});
|
|
Hook::add('user_logout_successed', function ($user) use ($auth) {
|
|
Cookie::delete('uid');
|
|
Cookie::delete('token');
|
|
});
|
|
$douyin_third = $douyin_third->toArray();
|
|
// 授权成功后的回调
|
|
$loginret = Service::connect("douyin", $douyin_third);
|
|
if ($loginret) {
|
|
$this->result("登陆成功", ["token" => $auth->getToken(), "user_id" => $auth->getUser()->id], 1);
|
|
} else {
|
|
$this->error("登录失败,请重试", [], 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
switch ($result['err_no']) {
|
|
case "-1":
|
|
$this->error("抖音系统错误!", [], 0);
|
|
break;
|
|
case "40014":
|
|
$this->error("param error", [], 0);
|
|
break;
|
|
case "40015":
|
|
$this->error("APPID error", [], 0);
|
|
break;
|
|
case "40017":
|
|
$this->error("secret error", [], 0);
|
|
break;
|
|
case "40018":
|
|
$this->error("code error", [], 0);
|
|
break;
|
|
case "40019":
|
|
$this->error("acode error", [], 0);
|
|
break;
|
|
default:
|
|
$this->error("error", [], 0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function bind()
|
|
{
|
|
|
|
$account = $this->request->param("username");
|
|
$password = $this->request->param("password");
|
|
$openid = $this->request->param("openid");
|
|
$unionid = $this->request->param("unionid");
|
|
$nickanme = $this->request->param("nickname", "");
|
|
$avatarUrl = $this->request->param("avatarUrl", "");
|
|
|
|
// 取出加盐因子 md5(md5($password) . $salt)
|
|
$user = new \app\common\model\User();
|
|
$field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
|
|
$user = User::get([$field => $account]);
|
|
if (!$user) {
|
|
$this->error('账号或者密码错误');
|
|
}
|
|
|
|
if ($user->status != 'normal') {
|
|
$this->error('用户已被锁定');
|
|
}
|
|
|
|
if (
|
|
md5(md5($password) . $user->salt) == $user->password ||
|
|
password_verify($password, $user->password)
|
|
) {
|
|
// 查询之前有没有绑定抖音
|
|
$third = \addons\third\model\Third::where('user_id', $user->id)
|
|
->where("platform", "douyin")
|
|
->where("apptype", "dy")
|
|
->find();
|
|
|
|
if ($third) {
|
|
$this->error("已绑定账号,请勿重复绑定");
|
|
}
|
|
|
|
// 添加绑定抖音
|
|
$param['user_id'] = $user->id;
|
|
$param['platform'] = "douyin";
|
|
$param['apptype'] = "dy";
|
|
$param['unionid'] = $unionid;
|
|
$param['openid'] = $openid;
|
|
$param['createtime'] = time();
|
|
$param['openname'] = $nickanme;
|
|
$param['wechat_avatar'] = $avatarUrl;
|
|
|
|
|
|
$third = \addons\third\model\Third::create($param);
|
|
if ($third) {
|
|
$auth = $this->auth;
|
|
|
|
//监听注册登录注销的事件
|
|
Hook::add('user_login_successed', function ($user) use ($auth) {
|
|
$expire = input('post.keeplogin') ? 30 * 86400 : 0;
|
|
Cookie::set('uid', $user->id, $expire);
|
|
Cookie::set('token', $auth->getToken(), $expire);
|
|
});
|
|
Hook::add('user_register_successed', function ($user) use ($auth) {
|
|
Cookie::set('uid', $user->id);
|
|
Cookie::set('token', $auth->getToken());
|
|
});
|
|
Hook::add('user_logout_successed', function ($user) use ($auth) {
|
|
Cookie::delete('uid');
|
|
Cookie::delete('token');
|
|
});
|
|
|
|
// 直接拉起登录
|
|
$third = \addons\third\model\Third::where('user_id', $user->id)
|
|
->where("platform", "douyin")
|
|
->where("apptype", "dy")
|
|
->find();
|
|
$third = $third->toArray();
|
|
// 授权成功后的回调
|
|
$loginret = Service::connect("douyin", $third);
|
|
if ($loginret) {
|
|
$this->result("登陆成功", ["token" => $auth->getToken(), "user_id" => $auth->getUser()->id], 1);
|
|
} else {
|
|
$this->error("登录失败,请重试");
|
|
}
|
|
} else {
|
|
$this->error("账号绑定失败,请重试");
|
|
}
|
|
}
|
|
|
|
$this->error('账号或者密码错误');
|
|
}
|
|
|
|
function curlPost($url, $post_data = array(), $timeout = 5, $header = "", $data_type = "") {
|
|
$header = empty($header) ? '' : $header;
|
|
|
|
//支持json数据数据提交
|
|
if($data_type == 'json'){
|
|
$post_string = json_encode($post_data);
|
|
}elseif($data_type == 'array') {
|
|
$post_string = $post_data;
|
|
}elseif(is_array($post_data)){
|
|
$post_string = http_build_query($post_data, '', '&');
|
|
}
|
|
|
|
$ch = curl_init(); // 启动一个CURL会话
|
|
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查 // https请求 不验证证书和hosts
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
|
|
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
|
|
//curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
|
|
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); // Post提交的数据包
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // 设置超时限制防止死循环
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
//curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
// curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
|
|
$result = curl_exec($ch);
|
|
|
|
// 打印请求的header信息
|
|
//$a = curl_getinfo($ch);
|
|
//var_dump($a);
|
|
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
} |