TaoLer/app/BaseController.php

482 lines
14 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
2022-08-02 20:48:54 +08:00
use think\Response;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\facade\Request;
use think\facade\Lang;
use think\facade\View;
2022-08-02 21:13:36 +08:00
use think\facade\Route;
2022-08-16 15:19:21 +08:00
use taoser\SetArr;
use app\common\lib\Uploads;
2020-01-01 13:17:19 +08:00
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
2020-01-17 14:57:05 +08:00
[$validate, $scene] = explode('.', $validate);
2020-01-01 13:17:19 +08:00
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
2021-07-16 17:42:07 +08:00
/**
* 操作错误跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_error_tmpl')]);
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return Response
*/
protected function result($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: 'json';
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* 操作成功跳转
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = []): Response
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = (request()->isJson() || request()->isAjax()) ? 'json' : 'html';
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
if ('html' == strtolower($type)) {
$type = 'jump';
}
$response = Response::create($result, $type)->header($header)->options(['jump_template' => app('config')->get('app.dispatch_success_tmpl')]);
throw new HttpResponseException($response);
}
2022-08-02 20:48:54 +08:00
//显示网站设置
protected function getSystem()
{
//1.系统配置信息
return Db::name('system')->cache('system',3600)->find(1);
}
//域名协议转换 把数据库中的带HTTP或不带协议的域名转换为当前协议的域名前缀
protected function getHttpUrl($url)
{
//域名转换为无http协议
$www = stripos($url,'://') ? substr(stristr($url,'://'),3) : $url;
$htpw = Request::scheme().'://'. $www;
return $htpw;
}
//得到当前系统安装前台域名
protected function getIndexUrl()
{
$sys = $this->getSystem();
$domain = $this->getHttpUrl($sys['domain']);
$syscy = $sys['clevel'] ? Lang::get('Authorized') : Lang::get('Free version');
$runTime = $this->getRunTime();
View::assign(['domain'=>$domain,'insurl'=>$sys['domain'],'syscy'=>$syscy,'clevel'=>$sys['clevel'],'runTime'=>$runTime]);
return $domain;
}
protected function getRunTime()
{
//运行时间
$now = time();
$sys = $this->getSystem();
$count = $now-$sys['create_time'];
$days = floor($count/86400);
$hos = floor(($count%86400)/3600);
$mins = floor(($count%3600)/60);
$years = floor($days/365);
if($years >= 1){
$days = floor($days%365);
}
$runTime = $years ? "{$years}{$days}{$hos}{$mins}" : "{$days}{$hos}{$mins}";
return $runTime;
}
/**
* 获取文章链接地址
*
* @param integer $aid
* @return string
*/
2022-08-02 21:13:36 +08:00
protected function getRouteUrl(int $aid,string $ename = '') : string
2022-08-02 20:48:54 +08:00
{
$indexUrl = $this->getIndexUrl();
2022-08-02 21:13:36 +08:00
if(config('taoler.url_rewrite.article_as') == '<ename>/'){
// 分类可变路由
2022-08-16 15:19:21 +08:00
$artUrl = (string) url('article_detail', ['id' => (int) $aid, 'ename'=> $ename]);
//$artUrl = (string) Route::buildUrl('article_detail', ['id' => $aid, 'ename'=> $ename]);
2022-08-02 21:13:36 +08:00
} else {
$artUrl = (string) url('article_detail', ['id' => $aid]);
}
2022-08-16 15:19:21 +08:00
//dump($artUrl);
2022-08-02 20:48:54 +08:00
// 判断是否开启绑定
//$domain_bind = array_key_exists('domain_bind',config('app'));
// 判断index应用是否绑定域名
$bind_index = array_search('index',config('app.domain_bind'));
// 判断admin应用是否绑定域名
$bind_admin = array_search('admin',config('app.domain_bind'));
// 判断index应用是否域名映射
$map_index = array_search('index',config('app.app_map'));
// 判断admin应用是否域名映射
$map_admin = array_search('admin',config('app.app_map'));
$index = $map_index ? $map_index : 'index'; // index应用名
$admin = $map_admin ? $map_admin : 'admin'; // admin应用名
if($bind_index) {
// index绑定域名
$url = $indexUrl . str_replace($admin.'/','',$artUrl);
} else { // index未绑定域名
// admin绑定域名
if($bind_admin) {
$url = $indexUrl .'/' . $index . $artUrl;
} else {
$url = $indexUrl . str_replace($admin,$index,$artUrl);
}
}
return $url;
}
2022-08-16 15:19:21 +08:00
/**
* 关键词
*
* @return void
*/
public function setTags($data)
{
$tags = [];
if($data['flag'] == 'on') {
// 百度分词自动生成关键词
if(!empty(config('taoler.baidu.client_id')) == true) {
$url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?charset=UTF-8&access_token='.config('taoler.baidu.access_token');
//headers数组内的格式
$headers = array();
$headers[] = "Content-Type:application/json";
$body = array(
"text" => $data['tags']
);
$postBody = json_encode($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。(这个加不加没啥影响)
$datas = curl_exec($curl);
if($datas == false) {
echo '接口无法链接';
} else {
$res = stripos($datas,'error_code');
// 接收返回的数据
$dataItem = json_decode($datas);
if($res == false) {
// 数据正常
$items = $dataItem->items;
foreach($items as $item) {
if($item->pos == 'n' && !in_array($item->item,$tags)){
$tags[] = $item->item;
}
}
} else {
// 接口正常但获取数据失败可能参数错误重新获取token
$url = 'https://aip.baidubce.com/oauth/2.0/token';
$post_data['grant_type'] = config('taoler.baidu.grant_type');;
$post_data['client_id'] = config('taoler.baidu.client_id');
$post_data['client_secret'] = config('taoler.baidu.client_secret');
$o = "";
foreach ( $post_data as $k => $v )
{
$o.= "$k=" . urlencode( $v ). "&" ;
}
$post_data = substr($o,0,-1);
$res = $this->request_post($url, $post_data);
// 写入token
SetArr::name('taoler')->edit([
'baidu'=> [
'access_token' => json_decode($res)->access_token,
]
]);
echo 'api接口数据错误 - ';
echo $dataItem->error_msg;
}
}
}
} else {
// 手动添加关键词
// 中文一个或者多个空格转换为英文空格
$str = preg_replace('/\s+/',' ',$data['tags']);
$att = explode(' ', $str);
foreach($att as $v){
if ($v !='') {
$tags[] = $v;
}
}
}
return json(['code'=>0,'data'=>$tags]);
}
// api_post接口
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
}
/**
* 标题调用百度关键词词条
*
* @return void
*/
public function getBdiduSearchWordList($words)
{
if(empty($words)) return json(['code'=>-1,'msg'=>'null']);
$url = 'https://www.baidu.com/sugrec?prod=pc&from=pc_web&wd='.$words;
//$result = Api::urlGet($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$datas = curl_exec($curl);
curl_close($curl);
$data = json_decode($datas,true);
if(isset($data['g'])) {
return json(['code'=>0,'msg'=>'success','data'=>$data['g']]);
} else {
return json(['code'=>-1,'msg'=>'null']);
}
}
/**
* baidu push api
*
* @param string $link
* @return void
*/
protected function baiduPushUrl(string $link)
{
// baidu 接口
$api = config('taoler.baidu.push_api');
if(!empty($api)) {
$url[] = $link;
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $url),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
}
/**
* 上传接口
*
* @return void
*/
public function uploadFiles($type)
{
$type = Request::param('type');
$uploads = new Uploads();
switch ($type){
case 'image':
$upRes = $uploads->put('file','article_pic',1024,'image');
break;
case 'zip':
$upRes = $uploads->put('file','article_zip',1024,'application|image');
break;
case 'video':
$upRes = $uploads->put('file','article_video',102400,'video|audio');
break;
case 'audio':
$upRes = $uploads->put('file','article_audio',102400,'audio');
break;
default:
$upRes = $uploads->put('file','article_file',1024,'image');
break;
}
return $upRes;
}
2022-08-02 20:48:54 +08:00
2020-01-01 13:17:19 +08:00
}