feat:控制器多层级语言包自动加载

This commit is contained in:
妙码生花 2022-03-09 18:12:15 +08:00
parent e911878b5b
commit 7669e40eb6
12 changed files with 72 additions and 54 deletions

View File

@ -58,10 +58,10 @@ abstract class BaseController
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/

View File

@ -9,7 +9,7 @@ class Dashboard extends Backend
{
public function dashboard()
{
$this->success('ok', [
$this->success('', [
'adminName' => $this->auth->nickname,
'remark' => get_route_remark()
]);

View File

@ -17,7 +17,7 @@ class Index extends Backend
{
$adminInfo = $this->auth->getInfo();
unset($adminInfo['token']);
$this->success('ok', [
$this->success('', [
'adminInfo' => $adminInfo,
'menus' => $this->auth->getMenus()
]);
@ -84,7 +84,7 @@ class Index extends Backend
}
}
$this->success('ok', [
$this->success('', [
'captcha' => $captchaSwitch
]);
}

View File

@ -20,9 +20,9 @@ class Menu extends Backend
protected $keyword = false;
public function _initialize()
public function initialize()
{
parent::_initialize();
parent::initialize();
$this->model = new MenuRule();
}
@ -32,7 +32,7 @@ class Menu extends Backend
$this->select();
}
$this->success('ok', [
$this->success('', [
'menu' => $this->getMenus()
]);
}
@ -41,7 +41,7 @@ class Menu extends Backend
{
$row = $this->model->find($id);
if (!$row) {
$this->error(__('No Results were found'));
$this->error(__('Record not found'));
}
if ($this->request->isPost()) {
@ -66,7 +66,7 @@ class Menu extends Backend
if ($isTree) {
$data = Tree::assembleTree(Tree::getTreeArray($data, 'title'));
}
$this->success('select', [
$this->success('', [
'options' => $data
]);
}

View File

@ -6,6 +6,7 @@ return [
'CaptchaId' => 'Captcha Id',
'Please enter the correct verification code' => 'Please enter the correct verification code!',
'Parameter %s can not be empty' => 'Parameter %s can not be empty',
'Record not found' => 'Record not found',
'No rows were added' => 'No rows were added',
'No rows were deleted' => 'No rows were deleted',
'No rows updated' => 'No rows updated',

View File

@ -6,6 +6,7 @@ return [
'CaptchaId' => '验证码ID',
'Please enter the correct verification code' => '请输入正确的验证码!',
'Parameter %s can not be empty' => '参数%s不能为空',
'Record not found' => '记录未找到',
'No rows were added' => '未添加任何行',
'No rows were deleted' => '未删除任何行',
'No rows updated' => '未更新任何行',

View File

@ -0,0 +1,4 @@
<?php
return [
];

View File

@ -32,7 +32,7 @@ trait Backend
{
$row = $this->model->find($id);
if (!$row) {
$this->error(__('No Results were found'));
$this->error(__('Record not found'));
}
if ($this->request->isPost()) {
@ -65,7 +65,7 @@ trait Backend
}
$this->success('edit', [
$this->success('', [
'row' => $row
]);
}

View File

@ -19,28 +19,40 @@ class Api extends BaseController
*/
protected $responseType = 'json';
/**
* 控制器目录路径
*/
protected $controllerPath;
public function __construct(App $app)
{
parent::__construct($app);
$this->_initialize();
}
/**
* 控制器初始化方法
*/
protected function _initialize()
protected function initialize()
{
parent::initialize();
$this->request->filter('trim,strip_tags,htmlspecialchars');
// 加载控制器语言包
$langset = $this->app->lang->getLangSet();
$this->controllerPath = str_replace('.', DIRECTORY_SEPARATOR, $this->request->controller(true));
$this->app->lang->load([
app_path() . 'lang' . DIRECTORY_SEPARATOR . $langset . DIRECTORY_SEPARATOR . $this->controllerPath . '.php'
]);
}
/**
* 操作成功
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function success($msg = '', $data = null, $code = 1, $type = null, $header = [], $options = [])
{
@ -49,12 +61,12 @@ class Api extends BaseController
/**
* 操作失败
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [], $options = [])
{
@ -63,12 +75,12 @@ class Api extends BaseController
/**
* 返回 API 数据
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
* @param string $msg 提示消息
* @param null $data 返回数据
* @param int $code 错误码
* @param null $type 输出类型
* @param array $header 发送的 header 信息
* @param array $options Response 输出参数
*/
public function result($msg, $data = null, $code = 0, $type = null, $header = [], $options = [])
{

View File

@ -2,7 +2,6 @@
namespace app\common\controller;
use app\common\controller\Api;
use app\admin\library\Auth;
use think\facade\Cookie;
@ -20,6 +19,9 @@ class Backend extends Api
protected $model = null;
/**
* 快速搜索字段
*/
protected $quickSearchField = 'id';
/**
@ -28,15 +30,11 @@ class Backend extends Api
*/
use \app\admin\library\traits\Backend;
public function _initialize()
public function initialize()
{
$modulename = app('http')->getName();
$controllername = $this->request->controller(true);
$actionname = $this->request->action(true);
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
parent::initialize();
$this->auth = Auth::instance();
$routePath = $this->controllerPath . '/' . $this->request->action(true);
$token = $this->request->server('HTTP_BATOKEN', $this->request->request('batoken', Cookie::get('batoken') ?: false));
if (!$this->auth->actionInArr($this->noNeedLogin)) {
$this->auth->init($token);
@ -46,7 +44,7 @@ class Backend extends Api
], 302);
}
if (!$this->auth->actionInArr($this->noNeedPermission)) {
if (!$this->auth->check($path)) {
if (!$this->auth->check($routePath)) {
$this->error(__('You have no permission'), [
'routeName' => 'admin'
], 302);
@ -57,10 +55,6 @@ class Backend extends Api
$this->auth->init($token);
}
}
// 语言检测
// 加载语言包?
// 初始化需要用到的配置
}
public function select()

View File

@ -156,7 +156,7 @@ class Index extends Api
];
}
$this->success('ok', [
$this->success('', [
'php_version' => [
'describe' => $phpVersion,
'state' => $phpVersionCompare ? self::$ok : self::$fail,
@ -272,7 +272,7 @@ class Index extends Api
];
}
$this->success('ok', [
$this->success('', [
'npm_version' => [
'describe' => $npmVersion ? $npmVersion : __('Acquisition failed'),
'state' => $npmVersionCompare ? self::$ok : self::$warn,
@ -338,7 +338,7 @@ class Index extends Api
if ($conn['code'] == 0) {
$this->error($conn['msg']);
} else {
$this->success('ok', [
$this->success('', [
'databases' => $conn['databases']
]);
}
@ -352,7 +352,7 @@ class Index extends Api
$envOk = $this->commandExecutionCheck();
if ($this->request->isGet()) {
$this->success('ok', ['envOk' => $envOk]);
$this->success('', ['envOk' => $envOk]);
}
$param = $this->request->only(['hostname', 'username', 'password', 'hostport', 'database', 'prefix', 'adminname', 'adminpassword', 'sitename']);
@ -389,7 +389,7 @@ class Index extends Api
$this->error(__('File has no write permission:%s', ['public/' . self::$lockFileName]), [], 102);
}
$this->success('ok', [
$this->success('', [
'execution' => $envOk
]);
}
@ -411,7 +411,7 @@ class Index extends Api
if (rename($indexHtmlPath, $toIndexHtmlPath) && rename($assetsPath, $toAssetsPath)) {
deldir($distPath);
$this->success('ok');
$this->success('');
} else {
$this->error(__('Failed to move the front-end file, please move it manually!'), [], 104);
}
@ -438,7 +438,7 @@ class Index extends Api
public function manualInstall()
{
// 取得 web 目录完整路径
$this->success('ok', [
$this->success('', [
'webPath' => root_path() . 'web'
]);
}

View File

@ -32,7 +32,7 @@
</div>
</template>
<div class="ba-operate-form" :class="'ba-' + form.operate + '-form'" :style="'width: calc(100% - ' + form.labelWidth / 2 + 'px)'">
<el-form v-model="form.items" label-position="right" :label-width="form.labelWidth + 'px'">
<el-form @keyup.enter="onSubmit" v-model="form.items" label-position="right" :label-width="form.labelWidth + 'px'">
<el-form-item label="上级菜单规则">
<remoteSelect
:params="{ isTree: true }"
@ -90,6 +90,8 @@
</el-form-item>
<el-form-item label="规则备注">
<el-input
@keyup.enter.stop=""
@keyup.ctrl.enter="onSubmit"
v-model="form.items.remark"
type="textarea"
:autosize="{ minRows: 2, maxRows: 5 }"
@ -243,8 +245,12 @@ const onSubmit = () => {
if (form.items.pid == form.items.pidebak) {
delete form.items.pid
}
if (!form.items.extend) {
delete form.items.extend
}
postEdit(form.items)
.then((res) => {
onAction('refresh', {})
form.submitLoading = false
form.operateIds.shift()
if (form.operateIds.length > 0) {