generated from php/site_api
143 lines
4.1 KiB
PHP
143 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\I18n\I18nLang;
|
|
|
|
use app\BaseController;
|
|
use app\common\model\I18n\I18nLang\I18nLang as ModelI18nLang;
|
|
use app\Request;
|
|
use think\Validate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Filesystem;
|
|
use app\common\arw\adjfut\src\Excel;
|
|
use app\common\arw\adjfut\src\UploadFile;
|
|
use app\common\exception\Tool;
|
|
use think\facade\Db;
|
|
use think\facade\Env;
|
|
|
|
|
|
class I18nLang extends BaseController
|
|
{
|
|
/**
|
|
* 获取多语言设置列表
|
|
*/
|
|
public function getI18nLangList(Request $request, $isExport = false): array
|
|
{
|
|
$params = $request->param();
|
|
$con = [];
|
|
|
|
$con = Tool::getOptionalQuery(['a.i18n_lang_type_guid', '='], ['a.i18n_lang_key', 'LIKE'], ['a.i18n_lang_name', 'LIKE'],);
|
|
|
|
$query = ModelI18nLang::where($con)
|
|
->alias('a')
|
|
->leftjoin('i18n_lang_type b', 'a.i18n_lang_type_guid = b.i18n_lang_type_guid')
|
|
->field([
|
|
'a.i18n_lang_id',
|
|
'a.i18n_lang_guid',
|
|
'a.i18n_lang_type_guid',
|
|
'b.i18n_lang_type_name',
|
|
'a.i18n_lang_key',
|
|
'a.i18n_lang_name'
|
|
])
|
|
->order('i18n_lang_create_time', 'asc');
|
|
|
|
return $isExport ? $query->select()->toArray() : msg("获取多语言设置列表成功!", $query);
|
|
}
|
|
|
|
/**
|
|
* 添加多语言设置
|
|
*/
|
|
public function addI18nLang(Request $request): array
|
|
{
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'i18n_lang_type_guid|语言类型guid' => 'require',
|
|
'i18n_lang_key|语言key' => 'require',
|
|
'i18n_lang_name|语言name' => 'require'
|
|
]);
|
|
$model = ModelI18nLang::create($params, [
|
|
'i18n_lang_type_guid',
|
|
'i18n_lang_key',
|
|
'i18n_lang_name',
|
|
'i18n_lang_guid',
|
|
'i18n_lang_create_user_guid',
|
|
'i18n_lang_update_user_guid'
|
|
]);
|
|
return msg('添加成功!');
|
|
}
|
|
|
|
/**
|
|
* 编辑多语言设置
|
|
*/
|
|
public function editI18nLang(Request $request): array
|
|
{
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'i18n_lang_type_guid|语言类型guid' => 'require',
|
|
'i18n_lang_key|语言key' => 'require',
|
|
'i18n_lang_name|语言name' => 'require'
|
|
]);
|
|
$model = ModelI18nLang::where('i18n_lang_guid', $params['i18n_lang_guid'])->find();
|
|
if (!$model) throwErrorMsg("该多语言设置不存在", 1);
|
|
$model->allowField([
|
|
'i18n_lang_type_guid',
|
|
'i18n_lang_key',
|
|
'i18n_lang_name',
|
|
'i18n_lang_update_user_guid'
|
|
])->save($params);
|
|
return msg('编辑成功!');
|
|
}
|
|
|
|
/**
|
|
* 删除多语言设置
|
|
*/
|
|
public function deleteI18nLang(Request $request): array
|
|
{
|
|
$params = $request->param();
|
|
$this->validate($params, [
|
|
'i18n_lang_guid' => 'require',
|
|
]);
|
|
$i18n_lang = ModelI18nLang::where([
|
|
'i18n_lang_guid' => explode(',', $params['i18n_lang_guid'])
|
|
])->select();
|
|
$i18n_lang->delete();
|
|
return msg('删除成功!');
|
|
}
|
|
|
|
/**
|
|
* 导出Excel
|
|
*/
|
|
public function exportExcel(Request $request): void
|
|
{
|
|
ModelI18nLang::exportExcel(self::getI18nLangList($request, true));
|
|
}
|
|
|
|
/**
|
|
* 下载导入模板
|
|
*/
|
|
public function downloadTemplate(Request $request): void
|
|
{
|
|
$params = $request->param();
|
|
$data = [
|
|
array_values(ModelI18nLang::EXCELFIELD),
|
|
['zh', 'home', '首页',],
|
|
['en', 'home', 'Home',]
|
|
];
|
|
$excel = (new Excel())->exporTsheet($data);
|
|
$excel->save('多语言设置导入模板.xlsx');
|
|
}
|
|
|
|
/**
|
|
* 导入excel
|
|
*/
|
|
public function importExcel(Request $request): array
|
|
{
|
|
$file = new UploadFile('uploads', 'fileExt:xlsx');
|
|
$file->putFile('i18n_lang');
|
|
$msg = ModelI18nLang::importExcel($file);
|
|
return [
|
|
'code' => 0,
|
|
'msg' => $msg
|
|
];
|
|
}
|
|
}
|