feat: 新增语言列表,语言切换接口

This commit is contained in:
2025-01-18 17:21:34 +08:00
parent b221633a47
commit b11968bcf7
3 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
use app\admin\model\v1\LanguageModel;
use think\facade\Cookie;
use think\facade\Log;
class Language
{
// 语言列表
public function list()
{
$language = LanguageModel::field([
'id',
'country_id',
'name' => 'language_name',
'code' => 'language_code',
'icon' => 'language_icon',
])
->with('country')
->order(['sort' => 'desc', 'id' => 'asc'])
->hidden(['country_id', 'country'])
->select()
->bindAttr('country', ['country_name' => 'name']);
return success('操作成功', $language);
}
// 语言切换
public function cutover()
{
$id = request()->param('id');
$language = LanguageModel::id($id)->find();
if (is_null($language)) {
return error('语言不存在');
}
try {
Cookie::set('lang', $language->code);
} catch (\Throwable $th) {
Log::error($th->getMessage());
return error('操作失败');
}
return success('操作成功');
}
}

View File

@@ -15,4 +15,10 @@ class LanguageModel extends LanguageBaseModel
{
return $this->belongsTo(CountryModel::class, 'country_id', 'id');
}
// 根据id查询
public function scopeId($query, $value)
{
$query->where('id', '=', $value);
}
}

View File

@@ -26,6 +26,15 @@ Route::group('v1', function () {
Route::post('login', 'Login/index');
});
// 语言模块
Route::group('language', function () {
// 语言列表
Route::get('list', 'Language/list');
// 语言切换
Route::get('cutover/:id', 'Language/cutover');
});
// 图片管理
Route::group('images', function () {
// 图片上传