mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-14 21:02:49 +00:00
perf: 优化后端翻译
This commit is contained in:
parent
d58246b255
commit
3c67b49d08
@ -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();
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
namespace App\Tasks;
|
||||
|
||||
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
||||
|
||||
use App\Module\Base;
|
||||
use App\Module\Ihttp;
|
||||
|
||||
/**
|
||||
* Ihttp任务
|
||||
* Class IhttpTask
|
||||
* @package App\Tasks
|
||||
*/
|
||||
class IhttpTask extends AbstractTask
|
||||
{
|
||||
protected $url;
|
||||
protected $post;
|
||||
protected $extra;
|
||||
protected $apiWebsocket;
|
||||
protected $apiUserid;
|
||||
protected $endPush = [];
|
||||
|
||||
/**
|
||||
* IhttpTask constructor.
|
||||
* @param $url
|
||||
* @param array $post
|
||||
* @param array $extra
|
||||
*/
|
||||
public function __construct($url, $post = [], $extra = [])
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
19
resources/assets/js/app.js
vendored
19
resources/assets/js/app.js
vendored
@ -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;
|
||||
|
||||
65
resources/assets/js/store/actions.js
vendored
65
resources/assets/js/store/actions.js
vendored
@ -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":
|
||||
|
||||
2
resources/assets/js/store/state.js
vendored
2
resources/assets/js/store/state.js
vendored
@ -88,8 +88,6 @@ export default {
|
||||
cacheKeyboard: {},
|
||||
|
||||
// Ajax
|
||||
ajaxWsReady: false,
|
||||
ajaxWsListener: [],
|
||||
ajaxNetworkException: false,
|
||||
|
||||
// Websocket
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user