284 lines
6.4 KiB
PHP
284 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Route;
|
|
|
|
class BaseController extends Controller
|
|
{
|
|
//
|
|
const ADMIN_BLADE_DIR = 'web.';
|
|
public $title = '';
|
|
public $route = [];
|
|
public $pages = [];
|
|
public $model;
|
|
public $share_view = [];
|
|
public $debug = 1;
|
|
|
|
public function __construct()
|
|
{
|
|
$route_arr = explode('@', Route::currentRouteAction());
|
|
$this->route['controller_name'] = '\\' . $route_arr[0];
|
|
$this->route['action_name'] = $route_arr[1];
|
|
$this->route['controller_base'] = str_replace('Controller', '', str_replace('App\\Http\\Controllers\\Web\\', '', $route_arr[0]));
|
|
$this->route['blade'] = self::ADMIN_BLADE_DIR . strtolower($this->route['controller_base']) . '.' . $this->route['action_name'];
|
|
$this->share_view['controller'] = "\\" . $this->route['controller_name'];
|
|
$this->title = ucwords($this->route['action_name']);
|
|
$this->share_view['title'] = $this->title;
|
|
$this->share_view['action_name'] = $this->route['action_name'];
|
|
$style_path = '/style/web/';
|
|
$style_path = asset($style_path);
|
|
|
|
$this->share_view['style_path'] = $style_path;
|
|
|
|
|
|
$this->shareView();
|
|
|
|
}
|
|
public function msg($data){
|
|
if(request()->ajax())
|
|
{
|
|
return response()->json($data);
|
|
}
|
|
return $this->htmlAlert($data);
|
|
|
|
}
|
|
public function htmlAlert($data){
|
|
echo '<script>alert("'.$data['msg'].'");window.history.back()</script>';
|
|
}
|
|
|
|
public function debugLog($str = '', $arr = [])
|
|
{
|
|
if ($str) {
|
|
if ($this->debug) {
|
|
Log::info($str, $arr);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function setTitle($title, $keyword, $description)
|
|
{
|
|
$data = [
|
|
'title' => $title,
|
|
'keyword' => $keyword,
|
|
'description' => $description,
|
|
];
|
|
return $this->setShareView($data);
|
|
}
|
|
|
|
/**
|
|
* 验证规则
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function checkRule($id = '')
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* 设置表单验证错误信息
|
|
* @return array
|
|
*/
|
|
public function setErrorMsg()
|
|
{
|
|
$messages = [
|
|
|
|
];
|
|
return $messages;
|
|
}
|
|
|
|
/**
|
|
* 全局返回表单错误消息
|
|
* @param $error
|
|
* @return array
|
|
*/
|
|
protected function formError($error)
|
|
{
|
|
$error = $error->all();
|
|
if (count($error) <= 0) return [];
|
|
$error_str = '';
|
|
foreach ($error as $k => $v) {
|
|
|
|
$error_str .= $v . "*\n";
|
|
|
|
}
|
|
return ['error' => 1, 'msg' => $error_str, 'type' => 'validator'];
|
|
|
|
|
|
}
|
|
|
|
public function alertError($error_str, $status = 1)
|
|
{
|
|
return response()->json(['error' => $status, 'msg' => $error_str, 'type' => 'validator']);
|
|
}
|
|
|
|
/**
|
|
* 全局检测表单是否错误,如果又返回错误数组,否则空数组
|
|
* @param $request
|
|
* @param string $id
|
|
* @return array
|
|
*/
|
|
protected function validatorForm($request, $id = '')
|
|
{
|
|
$validator = Validator::make($request->all(), $this->checkRule($id), $this->setErrorMsg());
|
|
if ($validator->fails()) {
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return $validator->errors();
|
|
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
|
|
|
|
/**
|
|
* 设置创建数据键值对
|
|
* @param $obj
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
protected function setDbKeyValue($obj, $data)
|
|
{
|
|
$fileds = $this->getField();
|
|
foreach ($data as $field => $v) {
|
|
if (in_array($field, $fileds)) {
|
|
$v = is_null($v) ? '' : $v;
|
|
$obj->$field = trim($v);
|
|
}
|
|
}
|
|
return $obj;
|
|
}
|
|
|
|
|
|
/**
|
|
* 共享视图资源
|
|
* @return mixed
|
|
*/
|
|
public function shareView()
|
|
{
|
|
$data = $this->share_view;
|
|
return view()->share($data);
|
|
|
|
}
|
|
|
|
public function setShareView($data)
|
|
{
|
|
return view()->share($data);
|
|
|
|
}
|
|
|
|
/**
|
|
* 改变自动获取blade
|
|
* @param $path
|
|
* @param int $cover 是否完全覆盖
|
|
* @param int $is_seft 是否在当前的控制下目录
|
|
* @return string
|
|
*/
|
|
protected function setViewPath($path = '', $filename = '', $cover = 0, $is_seft = 1)
|
|
{
|
|
if ($cover) {
|
|
return $this->route['blade'] = $path;
|
|
}
|
|
if ($is_seft == 0) {
|
|
return $this->route['blade'] = self::ADMIN_BLADE_DIR . $path;
|
|
}
|
|
if ($filename != '') {
|
|
$this->route['action_name'] = $filename;
|
|
}
|
|
if ($path) {
|
|
$this->route['action_name'] = $path . "." . $this->route['action_name'];
|
|
}
|
|
return $this->route['blade'] = self::ADMIN_BLADE_DIR . strtolower($this->route['controller_base']) . '.' . $this->route['action_name'];
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* 本页面操作的模型
|
|
*/
|
|
protected function setModel()
|
|
{
|
|
|
|
}
|
|
|
|
public function checkSelf($show)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function getShow($id)
|
|
{
|
|
$show = $this->setModel()->find($id);
|
|
|
|
|
|
if (!$show) {
|
|
return abort(404);
|
|
}
|
|
view()->share('show', $show);
|
|
|
|
return $this->display($this->shareData($show));
|
|
}
|
|
|
|
//共享创建和编辑数据
|
|
protected function shareData($id = '')
|
|
{
|
|
|
|
return [];
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
return $this->getShow($id);
|
|
}
|
|
|
|
/**
|
|
* 输出视图
|
|
* @param array $data
|
|
* @return mixed
|
|
*/
|
|
public function display($data = [])
|
|
{
|
|
|
|
return view($this->route['blade'], $data);
|
|
}
|
|
|
|
public function jsonDebug($json)
|
|
{
|
|
print_r($json);
|
|
|
|
return '';
|
|
}
|
|
|
|
protected function searchKey($model, $arr, $where = 1)
|
|
{
|
|
return $model->search($arr, $where);
|
|
|
|
}
|
|
|
|
public function jsonMsg($error, $str)
|
|
{
|
|
return response()->json(['error' => $error, 'msg' => $str]);
|
|
}
|
|
|
|
/*********基本控制器资源设置*********/
|
|
public function noSelfMsg()
|
|
{
|
|
if (\request()->ajax() || \request()->wantsJson()) {
|
|
return response()->json(['error' => '1', 'msg' => '非法操作数据']);
|
|
}
|
|
return abort(401, '非法操作数据');
|
|
}
|
|
|
|
public function __call($method, $parameters)
|
|
{
|
|
|
|
return abort('401', $method . '方法不存在');
|
|
}
|
|
|
|
} |