多语言
This commit is contained in:
@@ -4,6 +4,7 @@ namespace plugin\admin\app\controller;
|
||||
|
||||
use plugin\admin\app\common\Util;
|
||||
use plugin\admin\app\model\Option;
|
||||
use plugin\admin\app\model\Rule;
|
||||
use plugin\admin\app\model\User;
|
||||
use support\exception\BusinessException;
|
||||
use support\Request;
|
||||
@@ -48,7 +49,26 @@ class IndexController
|
||||
$logo = $config['logo']['image'] ?? '/app/admin/admin/images/logo.png';
|
||||
return raw_view('account/login',['logo'=>$logo,'title'=>$title]);
|
||||
}
|
||||
return raw_view('index/index');
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
|
||||
// 加载语言文件
|
||||
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
||||
|
||||
// 获取菜单并应用翻译
|
||||
$rules = Rule::whereIn('type', [0, 1])->orderBy('weight')->get();
|
||||
$menus = [];
|
||||
foreach ($rules as $rule) {
|
||||
$menu = $rule->toArray();
|
||||
$menu['title'] = $rule->getTranslatedTitle($lang);
|
||||
$menus[] = $menu;
|
||||
}
|
||||
|
||||
return view('index/index', [
|
||||
'lang' => $lang,
|
||||
'translations' => $common_translations,
|
||||
'menus' => $menus
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\admin\app\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use support\Log;
|
||||
|
||||
class LanguageController
|
||||
{
|
||||
/**
|
||||
* 不需要登录的方法
|
||||
* @var string[]
|
||||
*/
|
||||
protected $noNeedLogin = ['switch'];
|
||||
|
||||
/**
|
||||
* 不需要鉴权的方法
|
||||
* @var string[]
|
||||
*/
|
||||
protected $noNeedAuth = ['switch'];
|
||||
|
||||
/**
|
||||
* 切换语言
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function switch(Request $request): Response
|
||||
{
|
||||
try {
|
||||
$lang = $request->post('lang');
|
||||
Log::info('Language switch request received', [
|
||||
'lang' => $lang,
|
||||
'method' => $request->method(),
|
||||
'headers' => $request->header(),
|
||||
'input' => $request->all()
|
||||
]);
|
||||
|
||||
if (!$lang) {
|
||||
Log::warning('No language specified in request');
|
||||
return json(['code' => 1, 'msg' => '未指定语言']);
|
||||
}
|
||||
|
||||
if (!in_array($lang, ['zh-cn', 'en', 'ru', 'ar'])) {
|
||||
Log::warning('Unsupported language', ['lang' => $lang]);
|
||||
return json(['code' => 1, 'msg' => '不支持的语言']);
|
||||
}
|
||||
|
||||
// 设置语言到session
|
||||
session(['lang' => $lang]);
|
||||
Log::info('Language set to session', ['lang' => $lang]);
|
||||
|
||||
// 获取当前页面的URL
|
||||
$referer = $request->header('referer');
|
||||
if (!$referer) {
|
||||
$referer = '/';
|
||||
}
|
||||
Log::info('Redirect URL', ['referer' => $referer]);
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '切换成功',
|
||||
'data' => [
|
||||
'redirect' => $referer
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Language switch error', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '切换语言失败: ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use plugin\admin\app\model\Location;
|
||||
use plugin\admin\app\controller\Crud;
|
||||
use support\exception\BusinessException;
|
||||
use util\Util;
|
||||
use support\Log;
|
||||
|
||||
/**
|
||||
* 仓库/港口
|
||||
@@ -35,7 +36,22 @@ class LocationController extends Crud
|
||||
*/
|
||||
public function index(): Response
|
||||
{
|
||||
return view('location/index');
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
Log::info('Current language', ['lang' => $lang]);
|
||||
|
||||
// 加载语言文件
|
||||
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
||||
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
||||
|
||||
// 合并翻译
|
||||
$translations = array_merge($location_translations, $common_translations);
|
||||
Log::info('Loaded translations', ['keys' => array_keys($translations)]);
|
||||
|
||||
return view('location/index', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +65,20 @@ class LocationController extends Crud
|
||||
if ($request->method() === 'POST') {
|
||||
return parent::insert($request);
|
||||
}
|
||||
return view('location/insert');
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
|
||||
// 加载语言文件
|
||||
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
||||
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
||||
|
||||
// 合并翻译
|
||||
$translations = array_merge($location_translations, $common_translations);
|
||||
|
||||
return view('location/insert', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +92,20 @@ class LocationController extends Crud
|
||||
if ($request->method() === 'POST') {
|
||||
return parent::update($request);
|
||||
}
|
||||
return view('location/update');
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
|
||||
// 加载语言文件
|
||||
$location_translations = include __DIR__ . "/../lang/{$lang}/location.php";
|
||||
$common_translations = include __DIR__ . "/../lang/{$lang}/common.php";
|
||||
|
||||
// 合并翻译
|
||||
$translations = array_merge($location_translations, $common_translations);
|
||||
|
||||
return view('location/update', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,18 +67,26 @@ class RuleController extends Crud
|
||||
*/
|
||||
function get(Request $request): Response
|
||||
{
|
||||
// 添加调试日志
|
||||
error_log("RuleController::get called");
|
||||
|
||||
$rules = $this->getRules(admin('roles'));
|
||||
$types = $request->get('type', '0,1');
|
||||
$types = is_string($types) ? explode(',', $types) : [0, 1];
|
||||
$items = Rule::orderBy('weight', 'desc')->get()->toArray();
|
||||
|
||||
$items = Rule::orderBy('weight', 'desc')->get();
|
||||
|
||||
// 获取当前语言
|
||||
$lang = session('lang', 'zh-cn');
|
||||
|
||||
$formatted_items = [];
|
||||
foreach ($items as $item) {
|
||||
$item['pid'] = (int)$item['pid'];
|
||||
$item['name'] = $item['title'];
|
||||
$item['value'] = $item['id'];
|
||||
$item['icon'] = $item['icon'] ? "layui-icon {$item['icon']}" : '';
|
||||
$formatted_items[] = $item;
|
||||
$item_array = $item->toArray();
|
||||
$item_array['pid'] = (int)$item_array['pid'];
|
||||
$item_array['name'] = $item->getTranslatedTitle($lang);
|
||||
$item_array['title'] = $item->getTranslatedTitle($lang);
|
||||
$item_array['value'] = $item_array['id'];
|
||||
$item_array['icon'] = $item_array['icon'] ? "layui-icon {$item_array['icon']}" : '';
|
||||
$formatted_items[] = $item_array;
|
||||
}
|
||||
|
||||
$tree = new Tree($formatted_items);
|
||||
@@ -89,6 +97,10 @@ class RuleController extends Crud
|
||||
}
|
||||
$this->removeNotContain($tree_items, 'type', $types);
|
||||
$menus = $this->empty_filter(Tree::arrayValues($tree_items));
|
||||
|
||||
// 添加调试日志
|
||||
error_log("RuleController::get returning menus: " . json_encode($menus));
|
||||
|
||||
return $this->json(0, 'ok', $menus);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace plugin\admin\app\controller;
|
||||
|
||||
use plugin\admin\app\model\Rule;
|
||||
use plugin\admin\app\model\RuleTranslation;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
class RuleTranslationController
|
||||
{
|
||||
/**
|
||||
* 获取规则的翻译
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function get(Request $request): Response
|
||||
{
|
||||
$ruleId = $request->get('rule_id');
|
||||
$lang = $request->get('lang');
|
||||
|
||||
if (!$ruleId || !$lang) {
|
||||
return json(['code' => 1, 'msg' => '参数错误']);
|
||||
}
|
||||
|
||||
$translation = RuleTranslation::where('rule_id', $ruleId)
|
||||
->where('lang', $lang)
|
||||
->first();
|
||||
|
||||
return json(['code' => 0, 'data' => $translation]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存规则的翻译
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
public function save(Request $request): Response
|
||||
{
|
||||
$ruleId = $request->post('rule_id');
|
||||
$lang = $request->post('lang');
|
||||
$title = $request->post('title');
|
||||
|
||||
if (!$ruleId || !$lang || !$title) {
|
||||
return json(['code' => 1, 'msg' => '参数错误']);
|
||||
}
|
||||
|
||||
$rule = Rule::find($ruleId);
|
||||
if (!$rule) {
|
||||
return json(['code' => 1, 'msg' => '规则不存在']);
|
||||
}
|
||||
|
||||
$result = $rule->setTranslatedTitle($lang, $title);
|
||||
|
||||
return json(['code' => 0, 'msg' => '保存成功']);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,12 @@ class UploadController extends Crud
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
/**
|
||||
* 不需要鉴权的方法
|
||||
* @var string[]
|
||||
*/
|
||||
protected $noNeedAuth = ['uploadImage'];
|
||||
|
||||
/**
|
||||
* 只返回当前管理员数据
|
||||
* @var string
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace plugin\admin\app\controller;
|
||||
|
||||
@@ -50,7 +51,15 @@ class VehicleController extends Crud
|
||||
*/
|
||||
public function index(): Response
|
||||
{
|
||||
return view('vehicle/index');
|
||||
$lang = session('lang', 'zh-cn');
|
||||
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
|
||||
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
|
||||
$translations = array_merge($translations, $common);
|
||||
|
||||
return view('vehicle/index', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,10 +70,15 @@ class VehicleController extends Crud
|
||||
*/
|
||||
public function insert(Request $request): Response
|
||||
{
|
||||
if ($request->method() === 'POST') {
|
||||
return parent::insert($request);
|
||||
}
|
||||
return view('vehicle/insert');
|
||||
$lang = session('lang', 'zh-cn');
|
||||
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
|
||||
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
|
||||
$translations = array_merge($translations, $common);
|
||||
|
||||
return view('vehicle/insert', [
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,14 +89,25 @@ class VehicleController extends Crud
|
||||
*/
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
if ($request->method() === 'POST') {
|
||||
return parent::update($request);
|
||||
$id = $request->input('id');
|
||||
$lang = session('lang', 'zh-cn');
|
||||
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
|
||||
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
|
||||
$translations = array_merge($translations, $common);
|
||||
|
||||
$data = Vehicle::where('id', $id)->first();
|
||||
if (!$data) {
|
||||
return json(['code' => 1, 'msg' => $translations['data_not_found'] ?? '数据不存在']);
|
||||
}
|
||||
return view('vehicle/update');
|
||||
|
||||
return view('vehicle/update', [
|
||||
'id' => $id,
|
||||
'data' => $data,
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 详情
|
||||
* @param Request $request
|
||||
@@ -91,28 +116,24 @@ class VehicleController extends Crud
|
||||
*/
|
||||
public function detail(Request $request): Response
|
||||
{
|
||||
return view('vehicle/detail');
|
||||
$id = $request->input('id');
|
||||
$lang = session('lang', 'zh-cn');
|
||||
$translations = (array)require __DIR__ . '/../lang/' . $lang . '/vehicle.php';
|
||||
$common = (array)require __DIR__ . '/../lang/' . $lang . '/common.php';
|
||||
$translations = array_merge($translations, $common);
|
||||
|
||||
$data = Vehicle::where('id', $id)->first();
|
||||
if (!$data) {
|
||||
return json(['code' => 1, 'msg' => $translations['data_not_found'] ?? '数据不存在']);
|
||||
}
|
||||
|
||||
return view('vehicle/detail', [
|
||||
'data' => $data,
|
||||
'lang' => $lang,
|
||||
'translations' => $translations
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
* @throws BusinessException
|
||||
*/
|
||||
/* public function list(Request $request)
|
||||
{
|
||||
[$where, $format, $limit, $field, $order] = $this->selectInput($request);
|
||||
return json($where);
|
||||
$query = $this->doSelect($where, $field, $order);
|
||||
$data = $this->doFormat($query, $format, $limit);
|
||||
// foreach ($data['data'] as &$value) {
|
||||
// $value['locations'] = Location::find($value['locations_id'])->name;
|
||||
// }
|
||||
// $data = json_decode(json_encode($data), true);
|
||||
return $data;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 查询后置处理,添加位置信息
|
||||
* @param mixed $items
|
||||
@@ -129,13 +150,6 @@ class VehicleController extends Crud
|
||||
$fbo_location = Location::find($item->fbo_locations_id);
|
||||
$item->fbo_location_name = $fbo_location ? $fbo_location->name : '';
|
||||
|
||||
// if(!str_starts_with($item['img_url'], 'http')){
|
||||
// $ossservice = new OssService();
|
||||
// $img_url = $ossservice->generatePrivateUrl($item['img_url'], 3600);
|
||||
// $item['img_url'] = $img_url;
|
||||
// }
|
||||
|
||||
|
||||
if(!str_starts_with($item['img_url'], 'http')){
|
||||
try {
|
||||
$ossservice = new OssService();
|
||||
|
||||
Reference in New Issue
Block a user