Skip to content

Commit

Permalink
Merge pull request #154 from maxincai/master
Browse files Browse the repository at this point in the history
[代码优化](master): 优化会话存档同步方式至秒级同步
  • Loading branch information
maxincai authored Aug 31, 2021
2 parents c19ddb8 + 1b1d6aa commit 829f6dc
Show file tree
Hide file tree
Showing 41 changed files with 1,449 additions and 249 deletions.
5 changes: 2 additions & 3 deletions api-server/app/core/common/src/Action/Dashboard/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use League\Flysystem\Filesystem;
use MoChat\App\Utils\File;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
Expand Down Expand Up @@ -46,10 +47,8 @@ public function handle(Filesystem $filesystem): array
$originalFilename = $this->request->post('name', false) ?: $file->getClientFilename();

// 不再支持自定义path
$path = date('Y/md/Hi');
$extension = $file->getExtension();
$filename = strval(microtime(true) * 10000) . uniqid() . '.' . $extension;
$pathFileName = $path . '/' . $filename;
$pathFileName = File::generateFullFilename($extension);

try {
$stream = fopen($file->getRealPath(), 'r+');
Expand Down
8 changes: 8 additions & 0 deletions api-server/app/core/corp/src/Action/Dashboard/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Corp\Action\Dashboard\Traits\RequestTrait;
use MoChat\App\Corp\Contract\CorpContract;
use MoChat\App\Corp\Utils\WeWorkFactory;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
Expand Down Expand Up @@ -48,6 +49,12 @@ class Update extends AbstractAction
*/
private $logger;

/**
* @Inject()
* @var WeWorkFactory
*/
protected $weWorkFactory;

/**
* @RequestMapping(path="/dashboard/corp/update", methods="put")
* @Middlewares({
Expand Down Expand Up @@ -78,6 +85,7 @@ public function handle(): array
try {
## 数据入表
$this->corpService->updateCorpById($corpId, $params);
$this->weWorkFactory->unbindApp((int) $corpId);
} catch (\Throwable $e) {
$this->logger->error(sprintf('%s [%s] %s', '企业微信授信更新失败', date('Y-m-d H:i:s'), $e->getMessage()));
$this->logger->error($e->getTraceAsString());
Expand Down
64 changes: 15 additions & 49 deletions api-server/app/core/corp/src/Logic/AppTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,38 @@
namespace MoChat\App\Corp\Logic;

use EasyWeChat\Work\Application;
use MoChat\App\Corp\Contract\CorpContract;
use MoChat\App\WorkAgent\Contract\WorkAgentContract;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\WeWork\WeWork;
use MoChat\App\Corp\Utils\WeWorkFactory;

trait AppTrait
{
/**
* 根据微信企业ID获取easyWechat实例.
*
* @param int|string $corpId 微信corpid或企业表corpId
* @param string $type ...
* @return Application ...
* @param string $type
*
* @return Application
*/
public function wxApp($corpId, string $type = 'user'): Application
{
if (is_int($corpId)) {
$corMethod = 'getCorpById';
} else {
$corMethod = 'getCorpsByWxCorpId';
}
$corp = make(CorpContract::class)->{$corMethod}($corpId, [
'id', 'employee_secret', 'contact_secret', 'wx_corpid'
]);
if (empty($corp)) {
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('无该企业:[%s]', $corpId));
}

$config['corp_id'] = $corp['wxCorpid'];
switch ($type) {
case 'user':
$config['secret'] = $corp['employeeSecret'];
break;
case 'contact':
$config['secret'] = $corp['contactSecret'];
break;
default:
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('wxApp方法无此类型:[%s]', $type));
$weWorkFactory = make(WeWorkFactory::class);
if ($type === 'user') {
return $weWorkFactory->getUserApp($corpId);
}

return make(WeWork::class)->app($config);
return $weWorkFactory->getContactApp($corpId);
}

/**
* 根据企业微信应用id获取信息.
* 获取三方应用或自建应用实例
*
* @param int|string $agentId
*
* @return Application
*/
public function wxAgentApp($agentId): Application
{
$agentFunc = is_int($agentId) ? 'getWorkAgentById' : 'getWorkAgentByWxAgentId';
$agent = make(WorkAgentContract::class)->{$agentFunc}($agentId, [
'id', 'wx_secret', 'corp_id',
]);
if (empty($agent)) {
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('无该应用:[%s]', $agentId));
}
## 根据corpId 查 wxCorpid
$corp = make(CorpContract::class)->getCorpById($agent['corpId'], [
'id', 'wx_corpid',
]);

$config = [
'corp_id' => $corp['wxCorpid'],
'secret' => $agent['wxSecret'],
];

return make(WeWork::class)->app($config);
$weWorkFactory = make(WeWorkFactory::class);
return $weWorkFactory->getAgentApp($agentId);
}
}
4 changes: 4 additions & 0 deletions api-server/app/core/corp/src/QueueService/WeWorkCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use MoChat\App\WorkDepartment\Event\DeleteDepartmentRawEvent;
use MoChat\App\WorkDepartment\Event\UpdateDepartmentRawEvent;
use MoChat\App\WorkEmployee\Event\DeleteEmployeeRawEvent;
use MoChat\App\WorkMessage\Event\MessageArchiveRawEvent;
use MoChat\App\WorkMessage\Event\MessageNotifyRawEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
use MoChat\App\WorkEmployee\Event\CreateEmployeeRawEvent;
Expand Down Expand Up @@ -83,6 +85,8 @@ protected function getEvents()
'event.change_contact.update_party' => UpdateDepartmentRawEvent::class,
'event.change_contact.delete_party' => DeleteDepartmentRawEvent::class,
'event.change_contact.update_tag' => UpdateContactTagRawEvent::class,
'event.conversation_archive' => MessageArchiveRawEvent::class,
'event.msgaudit_notify' => MessageNotifyRawEvent::class
];
}
}
169 changes: 169 additions & 0 deletions api-server/app/core/corp/src/Utils/WeWorkFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

declare(strict_types=1);
/**
* This file is part of MoChat.
* @link https://mo.chat
* @document https://mochat.wiki
* @contact [email protected]
* @license https://github.com/mochat-cloud/mochat/blob/master/LICENSE
*/
namespace MoChat\App\Corp\Utils;

use MoChat\App\Corp\Contract\CorpContract;
use EasyWeChat\Work\Application;
use MoChat\App\WorkAgent\Contract\WorkAgentContract;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\WeWork\WeWork;

class WeWorkFactory
{
/**
* @var array
*/
protected $userApps = [];

/**
* @var array
*/
protected $contactApps = [];

/**
* @var array
*/
protected $agentApps = [];

/**
* 获取通讯录应用实例
*
* @param string|int $corpId 企业id
*
* @return Application
*/
public function getUserApp($corpId): Application
{
if (isset($this->userApps[$corpId]) && $this->userApps[$corpId] instanceof Application) {
return $this->userApps[$corpId];
}

return $this->userApps[$corpId] = $this->makeApp($corpId, 'user');
}

/**
* 获取客户联系应用实例
*
* @param string|int $corpId 企业id
*
* @return Application
*/
public function getContactApp($corpId): Application
{
if (isset($this->contactApps[$corpId]) && $this->contactApps[$corpId] instanceof Application) {
return $this->contactApps[$corpId];
}

return $this->contactApps[$corpId] = $this->makeApp($corpId, 'contact');
}

/**
* 获取三方应用或自建应用实例
*
* @param string|int $agentId 应用id
*
* @return Application
*/
public function getAgentApp($agentId): Application
{
if (isset($this->agentApps[$agentId]) && $this->agentApps[$agentId] instanceof Application) {
return $this->agentApps[$agentId];
}

return $this->agentApps[$agentId] = $this->makeAgentApp($agentId);
}

/**
* 解绑通讯录和客户联系应用实例(当企业信息发生变化的时候)
*
* @param string|int $corpId
*/
public function unbindApp($corpId)
{
if (isset($this->contactApps[$corpId])) {
unset($this->contactApps[$corpId]);
}

if (isset($this->userApps[$corpId])) {
unset($this->userApps[$corpId]);
}
}

/**
* 解绑三方应用实例(当应用信息发生变化的时候)
*
* @param string|int $agentId
*/
public function unbindAgentApp($agentId)
{
if (isset($this->agentApps[$agentId])) {
unset($this->agentApps[$agentId]);
}
}

protected function makeApp($corpId, string $type)
{
if (is_int($corpId)) {
$corMethod = 'getCorpById';
} else {
$corMethod = 'getCorpsByWxCorpId';
}
$corp = make(CorpContract::class)->{$corMethod}($corpId, [
'id', 'employee_secret', 'contact_secret', 'wx_corpid'
]);
if (empty($corp)) {
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('无该企业:[%s]', $corpId));
}

$config['corp_id'] = $corp['wxCorpid'];
switch ($type) {
case 'user':
$config['secret'] = $corp['employeeSecret'];
break;
case 'contact':
$config['secret'] = $corp['contactSecret'];
break;
default:
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('wxApp方法无此类型:[%s]', $type));
}

return make(WeWork::class)->app($config);
}

/**
* 根据企业微信应用id获取信息.
* @param int|string $agentId
*
* @return Application
*/
protected function makeAgentApp($agentId): Application
{
$agentFunc = is_int($agentId) ? 'getWorkAgentById' : 'getWorkAgentByWxAgentId';
$agent = make(WorkAgentContract::class)->{$agentFunc}($agentId, [
'id', 'wx_secret', 'corp_id',
]);
if (empty($agent)) {
throw new CommonException(ErrorCode::SERVER_ERROR, sprintf('无该应用:[%s]', $agentId));
}
## 根据corpId 查 wxCorpid
$corp = make(CorpContract::class)->getCorpById($agent['corpId'], [
'id', 'wx_corpid',
]);

$config = [
'corp_id' => $corp['wxCorpid'],
'secret' => $agent['wxSecret'],
];

return make(WeWork::class)->app($config);
}
}
39 changes: 39 additions & 0 deletions api-server/app/core/sync-data/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "mochat/app-sync-data",
"description": "MoChat 核心应用-同步数据",
"license": "GPL-3.0",
"keywords": [
"php",
"mochat",
"scrm"
],
"homepage": "https://mo.chat",
"support": {
"docs": "https://mochat.wiki",
"issues": "https://github.com/mochat-cloud/mochat/issues",
"pull-request": "https://github.com/mochat-cloud/mochat/pulls",
"source": "https://github.com/mochat-cloud/mochat"
},
"require": {
"php": ">=7.3"
},
"autoload": {
"psr-4": {
"MoChat\\App\\SyncData\\": "src/"
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "dev-master"
}
},
"bin": [
],
"scripts": {
"cs-fix": "php-cs-fixer fix $1",
"test": "phpunit --colors=always"
}
}
Loading

0 comments on commit 829f6dc

Please sign in to comment.