[游戏管理]36字花字典-来自游戏配置扩展
This commit is contained in:
130
app/admin/controller/config/ZiHuaDictionary.php
Normal file
130
app/admin/controller/config/ZiHuaDictionary.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\config;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\game\ZiHuaDictionary as ZiHuaDictionaryLib;
|
||||
use InvalidArgumentException;
|
||||
use support\think\Db;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 36 字花字典独立编辑(仅 game_config.zi_hua_36_dictionary)
|
||||
*/
|
||||
class ZiHuaDictionary extends Backend
|
||||
{
|
||||
protected bool $modelValidate = false;
|
||||
protected array $noNeedPermission = ['index', 'save'];
|
||||
|
||||
private function hasNodePermission(WebmanRequest $request, string $action): bool
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return false;
|
||||
}
|
||||
$controllerPath = get_controller_path($request);
|
||||
if (!$controllerPath) {
|
||||
return false;
|
||||
}
|
||||
$paths = [];
|
||||
$paths[] = $controllerPath . '/' . $action;
|
||||
$parts = explode('/', $controllerPath);
|
||||
foreach ($parts as &$part) {
|
||||
if (str_contains($part, '_')) {
|
||||
$part = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $part))));
|
||||
}
|
||||
}
|
||||
$paths[] = implode('/', $parts) . '/' . $action;
|
||||
foreach (array_values(array_unique($paths)) as $path) {
|
||||
if ($this->auth->check($path)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function initController(WebmanRequest $request): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET:读取 36 条;POST 不支持
|
||||
*/
|
||||
public function index(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if (!$this->hasNodePermission($request, 'index')) {
|
||||
return $this->error(__('You have no permission'), [], 401);
|
||||
}
|
||||
if ($request->method() !== 'GET') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$row = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
|
||||
$items = ZiHuaDictionaryLib::parseFromConfigValue($row['config_value'] ?? null);
|
||||
return $this->success('', [
|
||||
'items' => $items,
|
||||
'categories' => ZiHuaDictionaryLib::CATEGORIES,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 JSON 数组(仅 value_type=json)
|
||||
*/
|
||||
public function save(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if (!$this->hasNodePermission($request, 'save')) {
|
||||
return $this->error(__('You have no permission'), [], 401);
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$payload = $request->post();
|
||||
if (!is_array($payload)) {
|
||||
return $this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
$items = $payload['items'] ?? null;
|
||||
if (!is_array($items)) {
|
||||
return $this->error('items 必须为数组');
|
||||
}
|
||||
try {
|
||||
$clean = ZiHuaDictionaryLib::prepareItemsForSave($items);
|
||||
$json = ZiHuaDictionaryLib::encodeForDb($clean);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$now = time();
|
||||
try {
|
||||
$exists = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
|
||||
if ($exists) {
|
||||
Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->update([
|
||||
'config_value' => $json,
|
||||
'value_type' => 'json',
|
||||
'update_time' => $now,
|
||||
]);
|
||||
} else {
|
||||
Db::name('game_config')->insert([
|
||||
'config_key' => ZiHuaDictionaryLib::CONFIG_KEY,
|
||||
'config_value' => $json,
|
||||
'value_type' => 'json',
|
||||
'remark' => '36字花字典 JSON 数组(独立表单维护)',
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success(__('Saved successfully'));
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,19 @@ use Webman\Http\Request as WebmanRequest;
|
||||
class ZiHuaDictionary extends Backend
|
||||
{
|
||||
protected bool $modelValidate = false;
|
||||
protected array $noNeedPermission = ['index', 'save'];
|
||||
|
||||
private function hasNodePermission(WebmanRequest $request, string $action): bool
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return false;
|
||||
}
|
||||
$controllerPath = get_controller_path($request);
|
||||
if (!$controllerPath) {
|
||||
return false;
|
||||
}
|
||||
return $this->auth->check($controllerPath . '/' . $action);
|
||||
}
|
||||
|
||||
protected function initController(WebmanRequest $request): ?Response
|
||||
{
|
||||
@@ -31,6 +44,9 @@ class ZiHuaDictionary extends Backend
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if (!$this->hasNodePermission($request, 'index')) {
|
||||
return $this->error(__('You have no permission'), [], 401);
|
||||
}
|
||||
if ($request->method() !== 'GET') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
@@ -51,6 +67,9 @@ class ZiHuaDictionary extends Backend
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if (!$this->hasNodePermission($request, 'save')) {
|
||||
return $this->error(__('You have no permission'), [], 401);
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
|
||||
3
web/src/lang/backend/en/config/ziHuaDictionary.ts
Normal file
3
web/src/lang/backend/en/config/ziHuaDictionary.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import dict from '../game/ziHuaDictionary'
|
||||
export default dict
|
||||
|
||||
3
web/src/lang/backend/zh-cn/config/ziHuaDictionary.ts
Normal file
3
web/src/lang/backend/zh-cn/config/ziHuaDictionary.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import dict from '../game/ziHuaDictionary'
|
||||
export default dict
|
||||
|
||||
@@ -37,6 +37,12 @@ router.beforeEach((to, from, next) => {
|
||||
|
||||
// 按需动态加载页面的语言包-start
|
||||
let loadPath: string[] = []
|
||||
const toCamelPath = (path: string): string => {
|
||||
return path
|
||||
.split('/')
|
||||
.map((seg) => seg.replace(/_([a-z])/g, (_m, c: string) => c.toUpperCase()))
|
||||
.join('/')
|
||||
}
|
||||
const config = useConfig()
|
||||
if (to.path in langAutoLoadMap) {
|
||||
loadPath.push(...langAutoLoadMap[to.path as keyof typeof langAutoLoadMap])
|
||||
@@ -47,7 +53,10 @@ router.beforeEach((to, from, next) => {
|
||||
|
||||
// 去除 path 中的 /admin
|
||||
const adminPath = to.path.slice(to.path.indexOf(adminBaseRoutePath) + adminBaseRoutePath.length)
|
||||
if (adminPath) loadPath.push(prefix + adminPath + '.ts')
|
||||
if (adminPath) {
|
||||
loadPath.push(prefix + adminPath + '.ts')
|
||||
loadPath.push(prefix + toCamelPath(adminPath) + '.ts')
|
||||
}
|
||||
} else {
|
||||
prefix = './frontend/' + config.lang.defaultLang
|
||||
loadPath.push(prefix + to.path + '.ts')
|
||||
@@ -55,7 +64,9 @@ router.beforeEach((to, from, next) => {
|
||||
|
||||
// 根据路由 name 加载的语言包
|
||||
if (to.name) {
|
||||
loadPath.push(prefix + '/' + to.name.toString() + '.ts')
|
||||
const routeName = to.name.toString()
|
||||
loadPath.push(prefix + '/' + routeName + '.ts')
|
||||
loadPath.push(prefix + '/' + toCamelPath(routeName) + '.ts')
|
||||
}
|
||||
|
||||
if (!window.loadLangHandle.publicMessageLoaded) window.loadLangHandle.publicMessageLoaded = []
|
||||
|
||||
104
web/src/views/backend/config/ziHuaDictionary/index.vue
Normal file
104
web/src/views/backend/config/ziHuaDictionary/index.vue
Normal file
@@ -0,0 +1,104 @@
|
||||
<template>
|
||||
<div class="default-main ba-table-box zi-hua-dict-page">
|
||||
<el-alert type="info" :closable="false" show-icon>
|
||||
{{ t('config.ziHuaDictionary.desc') }}
|
||||
</el-alert>
|
||||
|
||||
<div class="toolbar">
|
||||
<el-button type="primary" :loading="saving" :disabled="loading" @click="onSave">
|
||||
{{ t('config.ziHuaDictionary.btn_save') }}
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<el-table v-loading="loading" border stripe :data="items" row-key="no" max-height="640">
|
||||
<el-table-column prop="no" :label="t('config.ziHuaDictionary.no')" width="72" align="center" />
|
||||
<el-table-column :label="t('config.ziHuaDictionary.name')" min-width="120">
|
||||
<template #default="{ row }">
|
||||
<el-input v-model="row.name" maxlength="32" show-word-limit />
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="t('config.ziHuaDictionary.category')" min-width="160">
|
||||
<template #default="{ row }">
|
||||
<el-select v-model="row.category" style="width: 100%">
|
||||
<el-option v-for="c in categories" :key="c" :label="categoryLabel(c)" :value="c" />
|
||||
</el-select>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
import { auth } from '/@/utils/common'
|
||||
|
||||
defineOptions({
|
||||
name: 'config/ziHuaDictionary',
|
||||
})
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
type Item = { no: number; name: string; category: string }
|
||||
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const items = ref<Item[]>([])
|
||||
const categories = ref<string[]>(['zodiac', 'beast', 'fowl', 'vermin', 'divine'])
|
||||
|
||||
function categoryLabel(cat: string): string {
|
||||
return t('config.ziHuaDictionary.category_label.' + cat)
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await createAxios({
|
||||
url: '/admin/config.ZiHuaDictionary/index',
|
||||
method: 'get',
|
||||
})
|
||||
if (res.code === 1 && res.data) {
|
||||
const list = res.data.items as Item[]
|
||||
items.value = Array.isArray(list) ? list : []
|
||||
if (Array.isArray(res.data.categories) && res.data.categories.length) {
|
||||
categories.value = res.data.categories as string[]
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function onSave() {
|
||||
if (!auth('save')) {
|
||||
return
|
||||
}
|
||||
saving.value = true
|
||||
try {
|
||||
await createAxios({
|
||||
url: '/admin/config.ZiHuaDictionary/save',
|
||||
method: 'post',
|
||||
data: { items: items.value },
|
||||
showSuccessMessage: true,
|
||||
})
|
||||
await load()
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void load()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.zi-hua-dict-page {
|
||||
.toolbar {
|
||||
margin: 12px 0;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user