mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 03:01:12 +00:00
perf: 优化国际化语言
This commit is contained in:
parent
ef2230a331
commit
c46fd080df
@ -1034,7 +1034,7 @@ class ApproveController extends AbstractController
|
|||||||
// 更新审批 未读数量
|
// 更新审批 未读数量
|
||||||
if ($type == 'approve_reviewer' && $toUser['userid']) {
|
if ($type == 'approve_reviewer' && $toUser['userid']) {
|
||||||
$params = [
|
$params = [
|
||||||
'userid' => [$toUser['userid'], User::auth()->userid()],
|
'userid' => [$toUser['userid'], User::userid()],
|
||||||
'msg' => [
|
'msg' => [
|
||||||
'type' => 'approve',
|
'type' => 'approve',
|
||||||
'action' => 'unread',
|
'action' => 'unread',
|
||||||
|
|||||||
@ -7,6 +7,7 @@ use Request;
|
|||||||
use Redirect;
|
use Redirect;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use App\Tasks\PushTask;
|
use App\Tasks\PushTask;
|
||||||
|
use App\Module\Doo;
|
||||||
use App\Models\File;
|
use App\Models\File;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
@ -1556,18 +1557,7 @@ class DialogController extends AbstractController
|
|||||||
//
|
//
|
||||||
$msg_id = intval(Request::input("msg_id"));
|
$msg_id = intval(Request::input("msg_id"));
|
||||||
$language = Base::inputOrHeader('language');
|
$language = Base::inputOrHeader('language');
|
||||||
$targetLanguage = match ($language) {
|
$targetLanguage = Doo::getLanguages($language);
|
||||||
"zh" => "简体中文",
|
|
||||||
"zh-CHT" => "繁体中文",
|
|
||||||
"en" => "英语",
|
|
||||||
"ko" => "韩语",
|
|
||||||
"ja" => "日语",
|
|
||||||
"de" => "德语",
|
|
||||||
"fr" => "法语",
|
|
||||||
"id" => "印度尼西亚语",
|
|
||||||
"ru" => "俄语",
|
|
||||||
default => '',
|
|
||||||
};
|
|
||||||
//
|
//
|
||||||
if (empty($targetLanguage)) {
|
if (empty($targetLanguage)) {
|
||||||
return Base::retError("参数错误");
|
return Base::retError("参数错误");
|
||||||
|
|||||||
@ -331,8 +331,7 @@ class UsersController extends AbstractController
|
|||||||
$data = $user->toArray();
|
$data = $user->toArray();
|
||||||
$data['nickname_original'] = $user->getRawOriginal('nickname');
|
$data['nickname_original'] = $user->getRawOriginal('nickname');
|
||||||
$data['department_name'] = $user->getDepartmentName();
|
$data['department_name'] = $user->getDepartmentName();
|
||||||
// 适用默认部门下第1级负责人才能添加部门OKR
|
$data['department_owner'] = UserDepartment::where('parent_id',0)->where('owner_userid', $user->userid)->exists(); // 适用默认部门下第1级负责人才能添加部门OKR
|
||||||
$data['department_owner'] = UserDepartment::where('parent_id',0)->where('owner_userid', $user->userid())->exists();
|
|
||||||
return Base::retSuccess('success', $data);
|
return Base::retSuccess('success', $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,6 +347,7 @@ class UsersController extends AbstractController
|
|||||||
* @apiParam {String} [tel] 电话
|
* @apiParam {String} [tel] 电话
|
||||||
* @apiParam {String} [nickname] 昵称
|
* @apiParam {String} [nickname] 昵称
|
||||||
* @apiParam {String} [profession] 职位/职称
|
* @apiParam {String} [profession] 职位/职称
|
||||||
|
* @apiParam {String} [lang] 语言(比如:zh/en)
|
||||||
*
|
*
|
||||||
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
||||||
* @apiSuccess {String} msg 返回信息(错误描述)
|
* @apiSuccess {String} msg 返回信息(错误描述)
|
||||||
@ -410,6 +410,15 @@ class UsersController extends AbstractController
|
|||||||
$upLdap['employeeType'] = $profession;
|
$upLdap['employeeType'] = $profession;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 语言
|
||||||
|
if (Arr::exists($data, 'lang')) {
|
||||||
|
$lang = trim(Request::input('lang'));
|
||||||
|
if (!Doo::checkLanguage($lang)) {
|
||||||
|
return Base::retError('语言错误');
|
||||||
|
} else {
|
||||||
|
$user->lang = $lang;
|
||||||
|
}
|
||||||
|
}
|
||||||
//
|
//
|
||||||
$user->save();
|
$user->save();
|
||||||
User::generateToken($user);
|
User::generateToken($user);
|
||||||
|
|||||||
@ -941,7 +941,7 @@ class File extends AbstractModel
|
|||||||
*/
|
*/
|
||||||
public static function filePushMsg($action, $data = null, $userid = null)
|
public static function filePushMsg($action, $data = null, $userid = null)
|
||||||
{
|
{
|
||||||
$userid = User::auth()->userid();
|
$userid = User::userid();
|
||||||
if (empty($userid)) {
|
if (empty($userid)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ use App\Module\Base;
|
|||||||
use App\Module\Doo;
|
use App\Module\Doo;
|
||||||
use Cache;
|
use Cache;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App\Models\User
|
* App\Models\User
|
||||||
@ -446,6 +447,12 @@ class User extends AbstractModel
|
|||||||
if (Carbon::parse($user->line_at)->addSeconds(30)->lt(Carbon::now())) {
|
if (Carbon::parse($user->line_at)->addSeconds(30)->lt(Carbon::now())) {
|
||||||
$upArray['line_at'] = Carbon::now();
|
$upArray['line_at'] = Carbon::now();
|
||||||
}
|
}
|
||||||
|
if (empty($user->lang) || Request::hasHeader('language')) {
|
||||||
|
$lang = Request::header('language');
|
||||||
|
if (Doo::checkLanguage($lang) && $user->lang != $lang) {
|
||||||
|
$upArray['lang'] = $lang;
|
||||||
|
}
|
||||||
|
}
|
||||||
if ($upArray) {
|
if ($upArray) {
|
||||||
$user->updateInstance($upArray);
|
$user->updateInstance($upArray);
|
||||||
$user->save();
|
$user->save();
|
||||||
@ -486,7 +493,7 @@ class User extends AbstractModel
|
|||||||
* @param int $userid 会员ID
|
* @param int $userid 会员ID
|
||||||
* @return self
|
* @return self
|
||||||
*/
|
*/
|
||||||
public static function userid2basic($userid)
|
public static function userid2basic($userid, $addField = [])
|
||||||
{
|
{
|
||||||
global $_A;
|
global $_A;
|
||||||
if (empty($userid)) {
|
if (empty($userid)) {
|
||||||
@ -496,7 +503,7 @@ class User extends AbstractModel
|
|||||||
if (isset($_A["__static_userid2basic_" . $userid])) {
|
if (isset($_A["__static_userid2basic_" . $userid])) {
|
||||||
return $_A["__static_userid2basic_" . $userid];
|
return $_A["__static_userid2basic_" . $userid];
|
||||||
}
|
}
|
||||||
$userInfo = self::whereUserid($userid)->select(User::$basicField)->first();
|
$userInfo = self::whereUserid($userid)->select(array_merge(User::$basicField, $addField))->first();
|
||||||
if ($userInfo) {
|
if ($userInfo) {
|
||||||
$userInfo->online = $userInfo->getOnlineStatus();
|
$userInfo->online = $userInfo->getOnlineStatus();
|
||||||
$userInfo->department_name = $userInfo->getDepartmentName();
|
$userInfo->department_name = $userInfo->getDepartmentName();
|
||||||
|
|||||||
@ -630,8 +630,8 @@ class WebSocketDialogMsg extends AbstractModel
|
|||||||
$text = Base::markdown2html($text);
|
$text = Base::markdown2html($text);
|
||||||
}
|
}
|
||||||
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?alt=\"(\S+)\"[^>]*?>/", "[$1]", $text);
|
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?alt=\"(\S+)\"[^>]*?>/", "[$1]", $text);
|
||||||
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?>/", "[动画表情]", $text);
|
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?>/", "[" . Doo::translate('动画表情') . "]", $text);
|
||||||
$text = preg_replace("/<img\s+class=\"browse\"[^>]*?>/", "[图片]", $text);
|
$text = preg_replace("/<img\s+class=\"browse\"[^>]*?>/", "[" . Doo::translate('图片') . "]", $text);
|
||||||
if (!$preserveHtml) {
|
if (!$preserveHtml) {
|
||||||
$text = str_replace("</p><p>", "</p> <p>", $text);
|
$text = str_replace("</p><p>", "</p> <p>", $text);
|
||||||
$text = strip_tags($text);
|
$text = strip_tags($text);
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use FFI;
|
|||||||
class Doo
|
class Doo
|
||||||
{
|
{
|
||||||
private static $doo;
|
private static $doo;
|
||||||
private static $passphrase = "LYHevk5n";
|
private static $userLanguage = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* char转为字符串
|
* char转为字符串
|
||||||
@ -269,12 +269,58 @@ class Doo
|
|||||||
/**
|
/**
|
||||||
* 翻译
|
* 翻译
|
||||||
* @param $text
|
* @param $text
|
||||||
* @param string $type
|
* @param string $lang
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function translate($text, string $type = ""): string
|
public static function translate($text, string $lang = ""): string
|
||||||
{
|
{
|
||||||
return self::string(self::doo()->translate($text, $type));
|
return self::string(self::doo()->translate($text, $lang ?: self::$userLanguage));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置语言
|
||||||
|
* @param string|integer $lang 语言 或 会员ID
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function setLanguage($lang) {
|
||||||
|
if (Base::isNumber($lang)) {
|
||||||
|
$lang = User::find(intval($lang))?->lang ?: "";
|
||||||
|
}
|
||||||
|
self::$userLanguage = $lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取语言列表 或 语言名称
|
||||||
|
* @param string|false $lang
|
||||||
|
* @return string|string[]
|
||||||
|
*/
|
||||||
|
public static function getLanguages(bool $lang = false)
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
"zh" => "简体中文",
|
||||||
|
"zh-CHT" => "繁体中文",
|
||||||
|
"en" => "英语",
|
||||||
|
"ko" => "韩语",
|
||||||
|
"ja" => "日语",
|
||||||
|
"de" => "德语",
|
||||||
|
"fr" => "法语",
|
||||||
|
"id" => "印度尼西亚语",
|
||||||
|
"ru" => "俄语",
|
||||||
|
];
|
||||||
|
if ($lang !== false) {
|
||||||
|
return $array[$lang] ?? "";
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查语言是否存在
|
||||||
|
* @param $lang
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function checkLanguage($lang): bool
|
||||||
|
{
|
||||||
|
return array_key_exists($lang, self::getLanguages());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -3,7 +3,6 @@ namespace App\Tasks;
|
|||||||
|
|
||||||
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
||||||
|
|
||||||
use App\Models\AbstractModel;
|
|
||||||
use App\Models\ProjectTask;
|
use App\Models\ProjectTask;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
|||||||
@ -7,6 +7,7 @@ use App\Models\User;
|
|||||||
use App\Models\WebSocketDialogMsg;
|
use App\Models\WebSocketDialogMsg;
|
||||||
use App\Models\WebSocketDialogMsgRead;
|
use App\Models\WebSocketDialogMsgRead;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
|
use App\Module\Doo;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Guanguans\Notify\Factory;
|
use Guanguans\Notify\Factory;
|
||||||
use Guanguans\Notify\Messages\EmailMessage;
|
use Guanguans\Notify\Messages\EmailMessage;
|
||||||
@ -113,7 +114,8 @@ class EmailNoticeTask extends AbstractTask
|
|||||||
$dialogName = null;
|
$dialogName = null;
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
$item->cancelAppend();
|
$item->cancelAppend();
|
||||||
$item->userInfo = User::userid2basic($item->userid);
|
$item->userInfo = User::userid2basic($item->userid, ['lang']);
|
||||||
|
Doo::setLanguage($item->userInfo->lang);
|
||||||
$item->preview = WebSocketDialogMsg::previewMsg($item, true);
|
$item->preview = WebSocketDialogMsg::previewMsg($item, true);
|
||||||
$item->preview = str_replace('<p>', '<p style="margin:0;padding:0">', $item->preview);
|
$item->preview = str_replace('<p>', '<p style="margin:0;padding:0">', $item->preview);
|
||||||
if (empty($dialogId)) {
|
if (empty($dialogId)) {
|
||||||
|
|||||||
@ -9,6 +9,7 @@ use App\Models\WebSocketDialog;
|
|||||||
use App\Models\WebSocketDialogMsg;
|
use App\Models\WebSocketDialogMsg;
|
||||||
use App\Models\WebSocketDialogMsgRead;
|
use App\Models\WebSocketDialogMsgRead;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
|
use App\Module\Doo;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Hhxsv5\LaravelS\Swoole\Task\Task;
|
use Hhxsv5\LaravelS\Swoole\Task\Task;
|
||||||
use Request;
|
use Request;
|
||||||
@ -191,13 +192,24 @@ class WebSocketDialogMsgTask extends AbstractTask
|
|||||||
if ($dialog->type == 'group') {
|
if ($dialog->type == 'group') {
|
||||||
$umengTitle = "{$dialog->getGroupName()} ($umengTitle)";
|
$umengTitle = "{$dialog->getGroupName()} ($umengTitle)";
|
||||||
}
|
}
|
||||||
$this->endArray[] = new PushUmengMsg($umengUserid, [
|
$langs = User::select(['userid', 'lang'])
|
||||||
'title' => $umengTitle,
|
->whereIn('userid', $umengUserid)
|
||||||
'body' => WebSocketDialogMsg::previewMsg($msg),
|
->get()
|
||||||
'description' => "MID:{$msg->id}",
|
->groupBy('lang')
|
||||||
'seconds' => 3600,
|
->map(function($group) {
|
||||||
'badge' => 1,
|
return $group->pluck('userid');
|
||||||
]);
|
});
|
||||||
|
foreach ($langs as $lang => $uids) {
|
||||||
|
Doo::setLanguage($lang);
|
||||||
|
$umengMsg = [
|
||||||
|
'title' => $umengTitle,
|
||||||
|
'body' => WebSocketDialogMsg::previewMsg($msg),
|
||||||
|
'description' => "MID:{$msg->id}",
|
||||||
|
'seconds' => 3600,
|
||||||
|
'badge' => 1,
|
||||||
|
];
|
||||||
|
$this->endArray[] = new PushUmengMsg($uids->toArray(), $umengMsg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
35
database/migrations/2024_11_05_151611_add_users_lang.php
Normal file
35
database/migrations/2024_11_05_151611_add_users_lang.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddUsersLang extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
if (!Schema::hasColumn('users', 'lang')) {
|
||||||
|
$table->string('lang', 20)->nullable()->default('')->after('bot')->comment('语言首选项');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropColumn("lang");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user