From 3c67b49d08c52abb6cad3db83410a5e382911200 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Fri, 11 Oct 2024 16:49:45 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/InvokeController.php | 22 +------ app/Module/Doo.php | 26 +++++++- app/Tasks/IhttpTask.php | 75 ----------------------- resources/assets/js/app.js | 19 ++++++ resources/assets/js/store/actions.js | 65 ++------------------ resources/assets/js/store/state.js | 2 - 6 files changed, 49 insertions(+), 160 deletions(-) delete mode 100644 app/Tasks/IhttpTask.php diff --git a/app/Http/Controllers/InvokeController.php b/app/Http/Controllers/InvokeController.php index acedd78ca..07e63d130 100644 --- a/app/Http/Controllers/InvokeController.php +++ b/app/Http/Controllers/InvokeController.php @@ -2,10 +2,7 @@ namespace App\Http\Controllers; -use App\Models\User; use App\Module\Base; -use App\Tasks\IhttpTask; -use Hhxsv5\LaravelS\Swoole\Task\Task; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; @@ -32,24 +29,7 @@ class InvokeController extends BaseController $msg = "404 not found (" . str_replace("__", "/", $app) . ")."; return Base::ajaxError($msg); } - // 使用websocket请求 - $apiWebsocket = Request::header('Api-Websocket'); - if ($apiWebsocket) { - $userid = User::userid(); - if ($userid > 0) { - $url = 'http://127.0.0.1:' . env('LARAVELS_LISTEN_PORT') . Request::getRequestUri(); - $task = new IhttpTask($url, Request::post(), [ - 'Content-Type' => Request::header('Content-Type'), - 'language' => Request::header('language'), - 'token' => Request::header('token'), - ]); - $task->setApiWebsocket($apiWebsocket); - $task->setApiUserid($userid); - Task::deliver($task); - return Base::retSuccess('wait'); - } - } - // 正常请求 + // $res = $this->__before($method, $action); if ($res === true || Base::isSuccess($res)) { return $this->$app(); diff --git a/app/Module/Doo.php b/app/Module/Doo.php index 52125f864..73ffdff5a 100644 --- a/app/Module/Doo.php +++ b/app/Module/Doo.php @@ -269,12 +269,32 @@ class Doo /** * 翻译 * @param $text - * @param string $type * @return string */ - public static function translate($text, string $type = ""): string + public static function translate($text): string { - return self::string(self::doo()->translate($text, $type)); + // 存在版本号 且 低于0.38.28时 使用原文 + $version = Base::headerOrInput('version'); + if ($version && !Base::judgeClientVersion('0.38.28', $version)) { + return $text; + } + + if (is_string($text)) { + // 等于success、error、warning、info、(为空)时不处理 + if (in_array($text, ['success', 'error', 'warning', 'info', ''])) { + return $text; + } + // 以__L(开头,)__结尾的不处理 + if (str_starts_with($text, "__L(") && str_ends_with($text, ")__")) { + return $text; + } + $text = "__L(" . $text . ")__"; + } elseif (is_array($text)) { + foreach ($text as $key => $val) { + $text[$key] = self::translate($val); + } + } + return $text; } /** diff --git a/app/Tasks/IhttpTask.php b/app/Tasks/IhttpTask.php deleted file mode 100644 index fcfdace62..000000000 --- a/app/Tasks/IhttpTask.php +++ /dev/null @@ -1,75 +0,0 @@ -url = $url; - $this->post = $post; - $this->extra = $extra; - } - - /** - * @param mixed $apiWebsocket - */ - public function setApiWebsocket($apiWebsocket): void - { - $this->apiWebsocket = $apiWebsocket; - } - - /** - * @param mixed $apiUserid - */ - public function setApiUserid($apiUserid): void - { - $this->apiUserid = $apiUserid; - } - - public function start() - { - $res = Ihttp::ihttp_request($this->url, $this->post, $this->extra); - if ($this->apiWebsocket && $this->apiUserid) { - $data = Base::isSuccess($res) ? Base::json2array($res['data']) : $res; - $this->endPush[] = [ - 'userid' => $this->apiUserid, - 'msg' => [ - 'type' => 'apiWebsocket', - 'apiWebsocket' => $this->apiWebsocket, - 'apiSuccess' => Base::isSuccess($res), - 'data' => $data, - ] - ]; - - } - } - - public function end() - { - PushTask::push($this->endPush); - } -} diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index f5e9d835e..30d0abd74 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -229,6 +229,25 @@ $A.bindScreenshotKey = (data) => { $A.Electron.sendMessage('bindScreenshotKey', {key}); }; +// 翻译 +$A.apiTranslate = (data) => { + if ($A.isJson(data)) { + for (let key in data) { + if (!data.hasOwnProperty(key)) continue; + data[key] = $A.apiTranslate(data[key]); + } + } else if ($A.isArray(data)) { + data.forEach((val, index) => { + data[index] = $A.apiTranslate(val); + }); + } else if (typeof data === 'string' && /__L\((.*?)\)__/.test(data)) { + data = data.replace(/__L\((.*?)\)__/g, (match, p1) => { + return $L(p1) + }) + } + return data +} + Vue.prototype.$A = $A; Vue.prototype.$L = $L; Vue.prototype.$Electron = $A.Electron; diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index 9bcc0d70a..f0461fc8c 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -161,12 +161,13 @@ export default { state.ajaxNetworkException = false if (!$A.isJson(result)) { console.log(result, status, xhr) - reject({ret: -1, data: {}, msg: "Return error"}) + reject({ret: -1, data: {}, msg: $A.L('返回参数错误')}) return } if (params.encrypt === true && result.encrypted) { result = await dispatch("pgpDecryptApi", result.encrypted) } + result = $A.apiTranslate(result) const {ret, data, msg} = result if (ret === -1) { state.userId = 0 @@ -203,7 +204,7 @@ export default { if (ret === 1) { resolve({data, msg}) } else { - reject({ret, data, msg: msg || "Unknown error"}) + reject({ret, data, msg: msg || $A.L('未知错误')}) // if (ret === -4001) { dispatch("forgetProject", data.project_id) @@ -235,66 +236,12 @@ export default { state.ajaxNetworkException = networkException } if (networkException) { - reject({ret: -1001, data: {}, msg: "Network exception"}) + reject({ret: -1001, data: {}, msg: $A.L('网络异常')}) } else { - reject({ret: -1, data: {}, msg: "System error"}) + reject({ret: -1, data: {}, msg: $A.L('请求失败')}) } console.error(xhr, status); } - // WebSocket - if (params.websocket === true) { - const apiWebsocket = $A.randomString(16) - const apiTimeout = setTimeout(() => { - const WListener = state.ajaxWsListener.find((item) => item.apiWebsocket == apiWebsocket) - if (WListener) { - WListener.complete() - WListener.error("timeout") - WListener.after() - } - state.ajaxWsListener = state.ajaxWsListener.filter((item) => item.apiWebsocket != apiWebsocket) - }, params.timeout || 30000) - state.ajaxWsListener.push({ - apiWebsocket: apiWebsocket, - complete: typeof params.complete === "function" ? params.complete : () => { }, - success: typeof params.success === "function" ? params.success : () => { }, - error: typeof params.error === "function" ? params.error : () => { }, - after: typeof params.after === "function" ? params.after : () => { }, - }) - // - params.complete = () => { } - params.success = () => { } - params.error = () => { } - params.after = () => { } - params.header['Api-Websocket'] = apiWebsocket - // - if (state.ajaxWsReady === false) { - state.ajaxWsReady = true - dispatch("websocketMsgListener", { - name: "apiWebsocket", - callback: (msg) => { - switch (msg.type) { - case 'apiWebsocket': - clearTimeout(apiTimeout) - const apiWebsocket = msg.apiWebsocket - const apiSuccess = msg.apiSuccess - const apiResult = msg.data - const WListener = state.ajaxWsListener.find((item) => item.apiWebsocket == apiWebsocket) - if (WListener) { - WListener.complete() - if (apiSuccess) { - WListener.success(apiResult) - } else { - WListener.error(apiResult) - } - WListener.after() - } - state.ajaxWsListener = state.ajaxWsListener.filter((item) => item.apiWebsocket != apiWebsocket) - break - } - } - }) - } - } // $A.ajaxc(params) }) @@ -3594,7 +3541,7 @@ export default { if (result.type === "encrypt" && result.encrypted) { result = await dispatch("pgpDecryptApi", result.encrypted) } - const msgDetail = $A.formatMsgBasic(result); + const msgDetail = $A.apiTranslate($A.formatMsgBasic(result)); const {type, msgId} = msgDetail; switch (type) { case "open": diff --git a/resources/assets/js/store/state.js b/resources/assets/js/store/state.js index c0d2208fd..9ad1f2661 100644 --- a/resources/assets/js/store/state.js +++ b/resources/assets/js/store/state.js @@ -88,8 +88,6 @@ export default { cacheKeyboard: {}, // Ajax - ajaxWsReady: false, - ajaxWsListener: [], ajaxNetworkException: false, // Websocket