273 lines
7.3 KiB
PHP
273 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace app;
|
|
|
|
use think\App;
|
|
use think\facade\View;
|
|
use think\exception\HttpException;
|
|
use think\exception\HttpResponseException;
|
|
use think\exception\ValidateException;
|
|
use think\Response;
|
|
use think\Validate;
|
|
use liliuwei\think\Jump;
|
|
use think\facade\Route;
|
|
use think\facade\Config;
|
|
use think\facade\Env;
|
|
use app\tracking\VisitorTracker;
|
|
|
|
/**
|
|
* 控制器基础类
|
|
*/
|
|
abstract class Controller
|
|
{
|
|
|
|
use Jump;
|
|
/**
|
|
* 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()
|
|
{
|
|
// 控制器初始化
|
|
$this->initialize();
|
|
}
|
|
|
|
// 初始化
|
|
protected function initialize()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 加载模板输出
|
|
* @access protected
|
|
* @param string $template 模板文件名
|
|
* @param array $vars 模板输出变量
|
|
* @param array $config 模板参数
|
|
* @return mixed
|
|
*/
|
|
protected function fetch($template = '', $vars = [], $config = [])
|
|
{
|
|
if (!empty($config)) {
|
|
$config_view = Config::get('view');
|
|
$config_view[ 'tpl_replace_string' ] = array_merge($config_view[ 'tpl_replace_string' ], $config);
|
|
Config::set($config_view, 'view');
|
|
}
|
|
//固定一个版本号
|
|
$this->assign('version', 128);
|
|
|
|
// ------------ 埋点:新增用户访问来源统计数据 ------------
|
|
try {
|
|
$visitorTracker = new VisitorTracker();
|
|
$siteId = property_exists($this, 'site_id') ? $this->site_id : 0;
|
|
$visitorTracker->shop_visit(['site_id' => $siteId]);
|
|
if (property_exists($this, 'store_id')) {
|
|
$storeId = $this->store_id;
|
|
if (!empty($storeId)) {
|
|
$visitorTracker->store_visit(['site_id' => $siteId, 'store_id' => $storeId]);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// 埋点异常不影响正常业务
|
|
log_write('埋点异常:' . $e->getMessage(), 'error');
|
|
}
|
|
|
|
return View::fetch($template, $vars);
|
|
}
|
|
|
|
/**
|
|
* 渲染内容输出
|
|
* @access protected
|
|
* @param string $content 模板内容
|
|
* @param array $vars 模板输出变量
|
|
* @param array $config 模板参数
|
|
* @return mixed
|
|
*/
|
|
protected function display($content = '', $vars = [], $config = [])
|
|
{
|
|
return View::display($content, $vars, $config);
|
|
}
|
|
|
|
/**
|
|
* 模板变量赋值
|
|
* @access protected
|
|
* @param mixed $name 要显示的模板变量
|
|
* @param mixed $value 变量的值
|
|
* @return $this
|
|
*/
|
|
protected function assign($name, $value = '')
|
|
{
|
|
View::assign($name, $value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 操作成功跳转的快捷方法
|
|
* @access protected
|
|
* @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 = [])
|
|
{
|
|
if (is_null($url) && isset($_SERVER[ "HTTP_REFERER" ])) {
|
|
$url = $_SERVER[ "HTTP_REFERER" ];
|
|
} elseif ($url) {
|
|
$url = ( strpos($url, '://') || 0 === strpos($url, '/') ) ? $url : $this->app->route->buildUrl($url);
|
|
}
|
|
|
|
$result = [
|
|
'code' => 1,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
'url' => $url,
|
|
'wait' => $wait,
|
|
];
|
|
|
|
$type = 'html';
|
|
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
|
|
if ('html' == strtolower($type)) {
|
|
$type = 'view';
|
|
$response = Response::create(config('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
|
|
} else {
|
|
$response = Response::create($result, $type)->header($header);
|
|
}
|
|
|
|
throw new HttpResponseException($response);
|
|
}
|
|
|
|
/**
|
|
* 操作错误跳转的快捷方法
|
|
* @access protected
|
|
* @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 = [])
|
|
{
|
|
if (is_null($url)) {
|
|
$url = 'javascript:history.back(-1);';
|
|
} elseif ($url) {
|
|
$url = ( strpos($url, '://') || 0 === strpos($url, '/') ) ? $url : $this->app->route->buildUrl($url);
|
|
}
|
|
|
|
$result = [
|
|
'code' => 0,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
'url' => $url,
|
|
'wait' => $wait,
|
|
];
|
|
|
|
$type = 'html';
|
|
|
|
if ('html' == strtolower($type)) {
|
|
$type = 'view';
|
|
$response = Response::create(config('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
|
|
} else {
|
|
$response = Response::create($result, $type)->header($header);
|
|
}
|
|
|
|
throw new HttpResponseException($response);
|
|
}
|
|
|
|
/**
|
|
* 返回封装后的API数据到客户端
|
|
* @access protected
|
|
* @param mixed $data 要返回的数据
|
|
* @param integer $code 返回的code
|
|
* @param mixed $msg 提示信息
|
|
* @param string $type 返回数据格式
|
|
* @param array $header 发送的Header信息
|
|
* @return void
|
|
*/
|
|
protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
|
|
{
|
|
$result = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'time' => time(),
|
|
'data' => $data,
|
|
];
|
|
|
|
$type = 'html';
|
|
$response = Response::create($result, $type)->header($header);
|
|
|
|
throw new HttpResponseException($response);
|
|
}
|
|
|
|
/**
|
|
* URL重定向
|
|
* @access protected
|
|
* @param string $url 跳转的URL表达式
|
|
* @param integer $code http code
|
|
* @param array $with 隐式传参
|
|
* @return void
|
|
*/
|
|
protected function redirect($url, $code = 302, $with = [])
|
|
{
|
|
$response = Response::create($url, 'redirect');
|
|
|
|
$response->code($code)->with($with);
|
|
|
|
throw new HttpResponseException($response);
|
|
}
|
|
|
|
/**
|
|
* @param array $data 验证数据
|
|
* @param 验证类 $validate
|
|
* @param 验证场景 $scene
|
|
*/
|
|
public function validate(array $data, $validate, $scene = '')
|
|
{
|
|
try {
|
|
$class = new $validate;
|
|
if (!empty($scene)) {
|
|
$res = $class->scene($scene)->check($data);
|
|
} else {
|
|
$res = $class->check($data);
|
|
}
|
|
if (!$res) {
|
|
return error(-1, $class->getError());
|
|
} else
|
|
return success(1);
|
|
|
|
} catch (\Exception $e) {
|
|
return error(-1, $e->getMessage());
|
|
}
|
|
|
|
}
|
|
}
|