2019-08-29 13:08:37 +08:00

112 lines
3.0 KiB
PHP

<?php
/**
* 工程基类
* @since 2017/02/28 创建
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/
namespace app\wiki\controller;
use app\util\ReturnCode;
use think\facade\Config;
use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Request;
use think\facade\Response;
use think\facade\View as ViewTemplate;
use think\facade\Url;
class Base extends Controller {
protected $appInfo;
public function checkLogin() {
$appInfo = session('app_info');
if ($appInfo) {
$this->appInfo = json_decode($appInfo, true);
} else {
$this->redirect(url('/wiki/login'));
}
}
public function buildSuccess($data, $msg = '操作成功', $code = ReturnCode::SUCCESS) {
$return = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
return json($return);
}
public function buildFailed($code, $msg, $data = []) {
$return = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
return json($return);
}
public function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
if (is_null($url)) {
$url = Request::instance()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
$url = Url::build($url);
}
$type = 'html';
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == strtolower($type)) {
$template = Config::get('template');
$view = Config::get('view_replace_str');
$result = ViewTemplate::instance($template, $view)
->fetch(Config::get('dispatch_error_tmpl'), $result);
}
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
public function success($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
if (is_null($url) && !is_null(Request::instance()->server('HTTP_REFERER'))) {
$url = Request::instance()->server('HTTP_REFERER');
} elseif ('' !== $url && !strpos($url, '://') && 0 !== strpos($url, '/')) {
$url = Url::build($url);
}
$type = 'html';
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ('html' == strtolower($type)) {
$template = Config::get('template');
$view = Config::get('view_replace_str');
$result = ViewTemplate::instance($template, $view)
->fetch(Config::get('dispatch_success_tmpl'), $result);
}
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
}