From 204ec576711e6a791b713b096c28dd5982ec04d4 Mon Sep 17 00:00:00 2001 From: "HESU\\HESU" <1696136552@qq.com> Date: Sat, 22 Mar 2025 10:26:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E4=B9=A6=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/FeishuController.php | 99 +++++++++++++++++++++-------- app/model/FeishuUser.php | 53 +++++++++++++++ app/process/Task.php | 41 ++++++++++++ composer.json | 3 +- composer.lock | 58 ++++++++++++++++- config/feishu.php | 4 +- config/process.php | 5 +- config/route.php | 1 + 8 files changed, 231 insertions(+), 33 deletions(-) create mode 100644 app/model/FeishuUser.php create mode 100644 app/process/Task.php diff --git a/app/controller/FeishuController.php b/app/controller/FeishuController.php index 3465cb0..483fab3 100644 --- a/app/controller/FeishuController.php +++ b/app/controller/FeishuController.php @@ -2,24 +2,38 @@ namespace app\controller; +use app\model\FeishuUser; +use app\model\User; +use app\util\Util; +use support\Log; use support\Request; use support\exception\BusinessException; class FeishuController { + protected $config = 'feishu.mbaf'; + /** * 不需要登录的方法 */ protected $noNeedLogin = ['auth', 'send', 'show']; + /** + * 获取飞书code + * @return \support\Response + */ + public function getAuth(Request $request) + { + return $this->auth($request); + } public function auth(Request $request) { $param = $request->all(); // 获取凭证 - $certificate = config('feishu.mbaf'); + $certificate = config($this->config); // 飞书服务 $class = new \app\service\FeishuService($certificate['appId'], $certificate['appSecret'], $certificate['state']); @@ -35,6 +49,21 @@ class FeishuController throw new BusinessException('授权失败,请重试'); } + Log::info('feishu user_info', ['res' => $rst]); + $state = explode(",", $param['state']); + //$userId = $request->user['id']; + $userId = $state[2]; + $user = FeishuUser::where('id', $userId)->first(); + + //保存用户open_id + $user->user_id = $userId; + $user->sign = $state[0]; + $user->tenant_key = $rst['data']['tenant_key']; + $user->open_id = $rst['data']['open_id']; + $user->feishu_user_id = $rst['data']['user_id']; + $user->union_id = $rst['data']['union_id']; + $user->save(); + // var_dump($rst); // array(3) { // ["code"]=> @@ -70,49 +99,63 @@ class FeishuController return json($rst); } else { + //需要登录并且未提供token + if (!Util::isEmpty($request->user['id'])) { + $certificate['state']=$certificate['state'].'_'. $request->user['id']; + } // 授权地址 $rst = $class->authorize($certificate); + + Log::info('feishu getCode', ['rst' => $rst]); // 拉起授权 return redirect($rst); } } - - public function send() + + /** + * @param $receiveId openid + * @param $title 推送标题 + * @param $content 推送内容 + * @param $href 详情链接 + * @return \support\Response + */ + public function send($receiveId,$title,$content,$href) { + Log::info('feishu send', ['receiveId' => $receiveId, 'title' => $title, 'content' => $content, 'href' => $href]); // 获取凭证 - $certificate = config('feishu.mbaf'); + $certificate = config($this->config); // 飞书服务 $class = new \app\service\FeishuService($certificate['appId'], $certificate['appSecret'], $certificate['state']); // $receiveId = 'ou_37bb7cfc4311c858ea32461700fa1a43'; - $receiveId = 'ou_a3b97543acbf1b28d978356bb08f85dd'; +// $receiveId = 'ou_a3b97543acbf1b28d978356bb08f85dd'; - // 富文本 类型 - // $content = "{ - // \"zh_cn\":{ - // \"title\":\"演示-消息提醒\", - // \"content\":[ - // [{ - // \"tag\":\"text\", - // \"text\":\"示例内容 - demo\", - // \"style\":[\"bold\"] - // }], - // [{ - // \"tag\":\"hr\" - // }], - // [{ - // \"tag\":\"a\", - // \"href\":\"http://219.157.255.213:25321/feishu/show\", - // \"text\":\"查看详情\" - // }] - // ] - // } - // }"; +// 富文本 类型 + $content = "{ + \"zh_cn\":{ + \"title\":\"$title\", + \"content\":[ + [{ + \"tag\":\"text\", + \"text\":\"$content\", + \"style\":[\"bold\"] + }], + [{ + \"tag\":\"hr\" + }], + [{ + \"tag\":\"a\", + \"href\":\"$href\", + \"text\":\"查看详情\" + }] + ] + } + }"; // 卡片 类型 - $content = "{ +/* $content = "{ \"config\":{ \"wide_screen_mode\":true }, @@ -140,7 +183,7 @@ class FeishuController \"tag\":\"plain_text\" } } - }"; + }";*/ $msgType = 'interactive'; $receiveIdtype = 'open_id'; diff --git a/app/model/FeishuUser.php b/app/model/FeishuUser.php new file mode 100644 index 0000000..b6d6714 --- /dev/null +++ b/app/model/FeishuUser.php @@ -0,0 +1,53 @@ + 'datetime:Y-m-d H:i:s', // 指定序列化格式 +// 'created_at' => 'datetime:Y-m-d H:i:s', // 指定序列化格式 + 'updated_at' => 'timestamp', // 指定序列化格式 + 'created_at' => 'timestamp', // 指定序列化格式 + ]; + + public static function getUser($userId) + { + $user = FeishuUser::first()->select(['id','name','sex','role','avatar','email','password']) + ->where('user_id',$userId) + ->toArray(); + return $user; + } +} \ No newline at end of file diff --git a/app/process/Task.php b/app/process/Task.php new file mode 100644 index 0000000..1f8aa04 --- /dev/null +++ b/app/process/Task.php @@ -0,0 +1,41 @@ +where(['follow_next', '>', time()]) + ->where(['warn', '<', 1]) + ->get(); + foreach ($customer as $item) { + $feishuUser = FeishuUser::where(['user_id' => $item->user_id])->first(); + if ($feishuUser) { + // 获取凭证 + $certificate = config($this->config); + // 使用parse_url解析URL + $parsedUrl = parse_url($certificate['rUrl']); + // 获取域名和端口 + $host = $parsedUrl['host']; + $port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : ''; + $url = $host .$port. '/pages/customer/detile?id='.$customer->id; + FeishuController::class->send($feishuUser->open_id, '跟进提醒', '您有新的跟进提醒', $url); + } + } + }); + + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index 160f304..0d98800 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "iidestiny/flysystem-oss": "^4", "vlucas/phpdotenv": "^5.6", "guzzlehttp/guzzle": "^7.9", - "webman/cache": "^2.1" + "webman/cache": "^2.1", + "workerman/crontab": "^1.0" }, "suggest": { "ext-event": "For better performance. " diff --git a/composer.lock b/composer.lock index c8772e9..24e52de 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1a64bcf537bed214294c8043291f03f3", + "content-hash": "a1bdf8bacc564776f13b4f4634c1b4c9", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -4002,6 +4002,62 @@ }, "time": "2025-02-17T03:34:21+00:00" }, + { + "name": "workerman/crontab", + "version": "v1.0.7", + "source": { + "type": "git", + "url": "https://github.com/walkor/crontab.git", + "reference": "74f51ca8204e8eb628e57bc0e640561d570da2cb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/walkor/crontab/zipball/74f51ca8204e8eb628e57bc0e640561d570da2cb", + "reference": "74f51ca8204e8eb628e57bc0e640561d570da2cb", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=7.0", + "workerman/workerman": ">=4.0.20" + }, + "type": "library", + "autoload": { + "psr-4": { + "Workerman\\Crontab\\": "./src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "walkor", + "email": "walkor@workerman.net", + "homepage": "http://www.workerman.net", + "role": "Developer" + } + ], + "description": "A crontab written in PHP based on workerman", + "homepage": "http://www.workerman.net", + "keywords": [ + "crontab" + ], + "support": { + "email": "walkor@workerman.net", + "forum": "http://wenda.workerman.net/", + "issues": "https://github.com/walkor/workerman/issues", + "source": "https://github.com/walkor/crontab", + "wiki": "http://doc.workerman.net/" + }, + "time": "2025-01-15T07:20:50+00:00" + }, { "name": "workerman/webman-framework", "version": "v2.1.2", diff --git a/config/feishu.php b/config/feishu.php index 4b30c07..77d319f 100644 --- a/config/feishu.php +++ b/config/feishu.php @@ -4,7 +4,7 @@ return [ 'xinyouhe' => [ 'appId' => "cli_a73e1e6406fa501c", 'appSecret' => "9cMfxujjGG9JPmNfNcOwUdCoIXqaM1wY", - 'rUrl' => "http://219.157.255.213:25321/feishu/auth", + 'rUrl' => "http://219.157.255.213:9007/feishu/auth", 'scope' => "contact:user.base:readonly contact:contact.base:readonly contact:user.employee_id:readonly im:message:send_as_bot", 'state' => "xinyouhe", 'baseUri' => "https://open.feishu.cn/open-apis/" @@ -12,7 +12,7 @@ return [ 'mbaf' => [ 'appId' => "cli_a75839fb4ff85028", 'appSecret' => "VsqLvwsRzpmisBVbrTIuKdNCvRCO447d", - 'rUrl' => "http://219.157.255.213:25321/feishu/auth", + 'rUrl' => "http://219.157.255.213:9007/feishu/auth", 'scope' => "contact:user.base:readonly contact:contact.base:readonly contact:user.employee_id:readonly im:message:send_as_bot", 'state' => "mbaf", 'baseUri' => "https://open.larksuite.com/open-apis/" diff --git a/config/process.php b/config/process.php index a6cd3f7..691ca88 100644 --- a/config/process.php +++ b/config/process.php @@ -58,5 +58,8 @@ return [ 'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/', ] ] - ] + ], + 'task' => [ + 'handler' => process\Task::class + ], ]; diff --git a/config/route.php b/config/route.php index 7ad9935..a29090d 100644 --- a/config/route.php +++ b/config/route.php @@ -29,5 +29,6 @@ Route::any('/upload', [app\controller\UploadController::class, 'uploadImage'])-> // 飞书 Route::post('/feishu/auth', [app\controller\FeishuController::class, 'auth']); +Route::post('/feishu/getAuth', [app\controller\FeishuController::class, 'getAuth']); Route::post('/feishu/show', [app\controller\FeishuController::class, 'show']); Route::post('/feishu/send', [app\controller\FeishuController::class, 'send']); \ No newline at end of file