飞书推送

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);
}
}
});
}
}