飞书推送

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';