2025-04-30 11:30:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
|
|
|
|
|
|
use support\Request;
|
|
|
|
|
use support\Response;
|
|
|
|
|
use plugin\admin\app\model\Location;
|
|
|
|
|
use plugin\admin\app\controller\Crud;
|
|
|
|
|
use support\exception\BusinessException;
|
|
|
|
|
use util\Util;
|
2025-05-06 17:16:37 +08:00
|
|
|
use support\Log;
|
2025-04-30 11:30:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 仓库/港口
|
|
|
|
|
*/
|
|
|
|
|
class LocationController extends Crud
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Location
|
|
|
|
|
*/
|
|
|
|
|
protected $model = null;
|
|
|
|
|
|
2025-05-19 09:14:51 +08:00
|
|
|
/**
|
|
|
|
|
* 不需要鉴权的方法
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
|
|
|
|
protected $noNeedAuth = ['getAll'];
|
|
|
|
|
|
2025-04-30 11:30:08 +08:00
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->model = new Location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 浏览
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
|
|
|
|
public function index(): Response
|
|
|
|
|
{
|
2025-05-06 17:16:37 +08:00
|
|
|
// 获取当前语言
|
|
|
|
|
$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/index', [
|
|
|
|
|
'lang' => $lang,
|
|
|
|
|
'translations' => $translations
|
|
|
|
|
]);
|
2025-04-30 11:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* 查询后置处理,添加位置信息
|
|
|
|
|
* @param mixed $items
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
protected function afterQuery($items)
|
|
|
|
|
{
|
|
|
|
|
$lang = session('lang', 'zh-cn');
|
|
|
|
|
switch ($lang) {
|
|
|
|
|
case 'zh-cn':
|
|
|
|
|
$location = new \app\model\Location();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'en':
|
|
|
|
|
$location = new \app\model\LocationEn();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'ru':
|
|
|
|
|
$location = new \app\model\LocationRu();
|
|
|
|
|
break;
|
|
|
|
|
case 'ar':
|
|
|
|
|
$location = new \app\model\LocationAr();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($items as &$item) {
|
|
|
|
|
$item = $location::find($item['id']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:30:08 +08:00
|
|
|
/**
|
|
|
|
|
* 插入
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @return Response
|
|
|
|
|
* @throws BusinessException
|
|
|
|
|
*/
|
|
|
|
|
public function insert(Request $request): Response
|
|
|
|
|
{
|
2025-05-06 17:16:37 +08:00
|
|
|
// 获取当前语言
|
|
|
|
|
$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);
|
2025-05-09 16:04:16 +08:00
|
|
|
|
|
|
|
|
// 如果是 POST 请求,处理数据保存和翻译
|
|
|
|
|
if ($request->method() === 'POST') {
|
2025-05-13 15:29:56 +08:00
|
|
|
$this->save($request);
|
|
|
|
|
// // 获取表单数据
|
|
|
|
|
// $data = $request->all();
|
|
|
|
|
// Log::info('location',$data);
|
|
|
|
|
//
|
|
|
|
|
// // 保存主表数据
|
|
|
|
|
// $location = new Location();
|
|
|
|
|
// $location->fill($data);
|
|
|
|
|
// $location->save();
|
|
|
|
|
//
|
|
|
|
|
// // 处理多语言翻译
|
|
|
|
|
// $languages = ['en', 'ar', 'ru'];
|
|
|
|
|
// $translationModels = [
|
|
|
|
|
// 'en' => \app\model\LocationEn::class,
|
|
|
|
|
// 'ar' => \app\model\LocationAr::class,
|
|
|
|
|
// 'ru' => \app\model\LocationRu::class
|
|
|
|
|
// ];
|
|
|
|
|
//
|
|
|
|
|
// // 创建翻译服务实例
|
|
|
|
|
// $translatorService = new \app\service\TranslatorService();
|
|
|
|
|
//
|
|
|
|
|
// foreach ($languages as $lang) {
|
|
|
|
|
// $translationModel = $translationModels[$lang];
|
|
|
|
|
// $translation = new $translationModel();
|
|
|
|
|
//
|
|
|
|
|
// // 复制主表数据
|
|
|
|
|
// $translation->fill($data);
|
|
|
|
|
// $translation->id = $location->id;
|
|
|
|
|
//
|
|
|
|
|
// // 使用翻译服务处理需要翻译的字段
|
|
|
|
|
// $translatedData = $translatorService->location($data, $lang);
|
|
|
|
|
//
|
|
|
|
|
// // 更新翻译后的字段
|
|
|
|
|
// $translation->name = $translatedData['name'] ?? $data['name'];
|
|
|
|
|
//// $translation->description = $translatedData['description'] ?? $data['description'];
|
|
|
|
|
//
|
|
|
|
|
// Log::info('location:'.$lang.':translatedData',$translatedData);
|
|
|
|
|
// $translation->save();
|
|
|
|
|
// }
|
2025-05-09 16:04:16 +08:00
|
|
|
}
|
2025-05-06 17:16:37 +08:00
|
|
|
|
|
|
|
|
return view('location/insert', [
|
|
|
|
|
'lang' => $lang,
|
|
|
|
|
'translations' => $translations
|
|
|
|
|
]);
|
2025-04-30 11:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* 保存
|
|
|
|
|
*/
|
|
|
|
|
public function save(Request $request)
|
|
|
|
|
{
|
|
|
|
|
// 获取表单数据
|
|
|
|
|
$data = $request->all();
|
|
|
|
|
|
|
|
|
|
$lang = session('lang', 'zh-cn');
|
|
|
|
|
|
|
|
|
|
$locationModel = $this->getModel($lang);
|
|
|
|
|
|
|
|
|
|
// 保存主表数据
|
|
|
|
|
if(!empty($data['id'])){
|
|
|
|
|
$location = $locationModel::find($data['id']);
|
|
|
|
|
}else{
|
|
|
|
|
$location = $locationModel;
|
|
|
|
|
}
|
|
|
|
|
$location->fill($data);
|
|
|
|
|
|
|
|
|
|
$location->save();
|
|
|
|
|
|
|
|
|
|
// 处理多语言翻译
|
|
|
|
|
$languages = ['zh-cn' => 'cn', 'en' => 'en', 'ar' => 'ar', 'ru' => 'ru'];
|
|
|
|
|
$translationModels = [
|
|
|
|
|
'cn' => \app\model\Location::class,
|
|
|
|
|
'en' => \app\model\LocationEn::class,
|
|
|
|
|
'ar' => \app\model\LocationAr::class,
|
|
|
|
|
'ru' => \app\model\LocationRu::class
|
|
|
|
|
];
|
|
|
|
|
//删除已保存的模型
|
|
|
|
|
unset($translationModels[$lang]);
|
2025-05-28 17:57:30 +08:00
|
|
|
$sourceLang = $languages[$lang];
|
2025-05-28 17:55:16 +08:00
|
|
|
unset($languages[$lang]);
|
2025-05-13 15:29:56 +08:00
|
|
|
|
|
|
|
|
// 创建翻译服务实例
|
|
|
|
|
$translatorService = new \app\service\TranslatorService();
|
|
|
|
|
|
2025-05-28 17:55:16 +08:00
|
|
|
foreach ($languages as $lang) {
|
|
|
|
|
$translatorService->queue($data,$sourceLang,'location',$data['id']?:null,$lang);
|
|
|
|
|
}
|
2025-05-13 15:29:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 11:30:08 +08:00
|
|
|
/**
|
|
|
|
|
* 更新
|
|
|
|
|
* @param Request $request
|
|
|
|
|
* @return Response
|
|
|
|
|
* @throws BusinessException
|
|
|
|
|
*/
|
|
|
|
|
public function update(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
if ($request->method() === 'POST') {
|
2025-05-13 15:29:56 +08:00
|
|
|
// return parent::update($request);
|
|
|
|
|
$this->save($request);
|
2025-04-30 11:30:08 +08:00
|
|
|
}
|
2025-05-06 17:16:37 +08:00
|
|
|
// 获取当前语言
|
|
|
|
|
$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
|
|
|
|
|
]);
|
2025-04-30 11:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-19 09:14:51 +08:00
|
|
|
// public function updateLogistics(Request $request): Response
|
|
|
|
|
// {
|
|
|
|
|
// return $this->update($request);
|
|
|
|
|
// }
|
|
|
|
|
|
2025-04-30 11:30:08 +08:00
|
|
|
/**
|
|
|
|
|
* 获取下拉选项接口
|
|
|
|
|
*/
|
|
|
|
|
public function getAll()
|
|
|
|
|
{
|
2025-05-13 15:29:30 +08:00
|
|
|
$lang = session('lang', 'zh-cn');
|
2025-05-13 15:29:56 +08:00
|
|
|
$hz = $this->getModel($lang);
|
|
|
|
|
$locations = $hz::get();
|
|
|
|
|
return Util::success($locations);
|
|
|
|
|
}
|
2025-05-13 15:29:30 +08:00
|
|
|
|
2025-05-13 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* @param $lang
|
|
|
|
|
* @return \app\model\Location|\app\model\LocationAr|\app\model\LocationEn|\app\model\LocationRu
|
|
|
|
|
*/
|
|
|
|
|
protected function getModel($lang){
|
|
|
|
|
|
|
|
|
|
switch ($lang) {
|
2025-05-13 15:29:30 +08:00
|
|
|
case 'en':
|
|
|
|
|
$hz = new \app\model\LocationEn();
|
|
|
|
|
break;
|
|
|
|
|
case 'ru':
|
|
|
|
|
$hz = new \app\model\LocationRu();
|
|
|
|
|
break;
|
|
|
|
|
case 'ar':
|
|
|
|
|
$hz = new \app\model\LocationAr();
|
|
|
|
|
break;
|
2025-05-13 15:29:56 +08:00
|
|
|
default:
|
|
|
|
|
$hz = new \app\model\Location();
|
|
|
|
|
break;
|
2025-05-13 15:29:30 +08:00
|
|
|
}
|
2025-05-13 15:29:56 +08:00
|
|
|
return $hz;
|
2025-04-30 11:30:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|