飞书推送

This commit is contained in:
2025-03-22 10:26:52 +08:00
parent 34cc7a1c8e
commit 204ec57671
8 changed files with 231 additions and 33 deletions
+71 -28
View File
@@ -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';
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace app\model;
use support\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class FeishuUser extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'feishu_user';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = true;
// 指定日期字段存储格式为时间戳
protected $dateFormat = 'U';
//设置黑名单
protected $guarded = ['id','created_at','updated_at'];
protected $casts = [
// 'updated_at' => '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;
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace process;
use app\controller\FeishuController;
use app\model\Customer;
use app\model\FeishuUser;
use app\model\User;
use Webman\App;
use Workerman\Crontab\Crontab;
class Task
{
public function onWorkerStart()
{
// 每半小时执行一次
new Crontab('30/* * * * *', function () {
echo date('Y-m-d H:i:s') . "\n";
$customer = Customer::where(['follow_next', '<=', time() + 3600])
->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);
}
}
});
}
}
+2 -1
View File
@@ -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. "
Generated
+57 -1
View File
@@ -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",
+2 -2
View File
@@ -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/"
+4 -1
View File
@@ -58,5 +58,8 @@ return [
'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
]
]
]
],
'task' => [
'handler' => process\Task::class
],
];
+1
View File
@@ -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']);