Files
crm-server/app/controller/FeishuController.php
T
2025-03-21 15:09:49 +08:00

186 lines
6.6 KiB
PHP

<?php
namespace app\controller;
use support\Request;
use support\exception\BusinessException;
class FeishuController
{
/**
* 不需要登录的方法
*/
protected $noNeedLogin = ['auth', 'send', 'show'];
public function auth(Request $request)
{
$param = $request->all();
// 获取凭证
$certificate = config('feishu.mbaf');
// 飞书服务
$class = new \app\service\FeishuService($certificate['appId'], $certificate['appSecret'], $certificate['state']);
if (isset($param['code']) && $param['code']) {
// 获取token
$userAccessToken = $class->userAccessToken($param['code'], $certificate['rUrl']);
// 获取用户信息
$rst = $class->userInfo($userAccessToken['access_token']);
if ($rst['code'] != 0) {
throw new BusinessException('授权失败,请重试');
}
// var_dump($rst);
// array(3) {
// ["code"]=>
// int(0)
// ["data"]=>
// array(11) {
// ["avatar_big"]=>
// string(168) "https://s1-imfile.feishucdn.com/static-resource/v1/v3_00ie_7cc82f38-0a89-43d4-8b2c-d90fc9d9176g~?image_size=640x640&cut_type=&quality=&format=image&sticker_format=.webp"
// ["avatar_middle"]=>
// string(168) "https://s1-imfile.feishucdn.com/static-resource/v1/v3_00ie_7cc82f38-0a89-43d4-8b2c-d90fc9d9176g~?image_size=240x240&cut_type=&quality=&format=image&sticker_format=.webp"
// ["avatar_thumb"]=>
// string(166) "https://s1-imfile.feishucdn.com/static-resource/v1/v3_00ie_7cc82f38-0a89-43d4-8b2c-d90fc9d9176g~?image_size=72x72&cut_type=&quality=&format=image&sticker_format=.webp"
// ["avatar_url"]=>
// string(166) "https://s1-imfile.feishucdn.com/static-resource/v1/v3_00ie_7cc82f38-0a89-43d4-8b2c-d90fc9d9176g~?image_size=72x72&cut_type=&quality=&format=image&sticker_format=.webp"
// ["en_name"]=>
// string(9) "李佳博"
// ["mobile"]=>
// string(14) "+8615738985690"
// ["name"]=>
// string(9) "李佳博"
// ["open_id"]=>
// string(35) "ou_37bb7cfc4311c858ea32461700fa1a43"
// ["tenant_key"]=>
// string(16) "14e7bc0fe5985740"
// ["union_id"]=>
// string(35) "on_a3fdbca48cd9477a140c7f2167a63691"
// ["user_id"]=>
// string(8) "481g947d"
// }
// ["msg"]=>
// string(7) "success"
// }
return json($rst);
} else {
// 授权地址
$rst = $class->authorize($certificate);
// 拉起授权
return redirect($rst);
}
}
public function send()
{
// 获取凭证
$certificate = config('feishu.mbaf');
// 飞书服务
$class = new \app\service\FeishuService($certificate['appId'], $certificate['appSecret'], $certificate['state']);
// $receiveId = 'ou_37bb7cfc4311c858ea32461700fa1a43';
$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 = "{
\"config\":{
\"wide_screen_mode\":true
},
\"elements\":[{
\"tag\":\"div\",
\"text\":{
\"content\":\"**测试内容**,测试内容,测试内容。\\n 测试内容,测试内容,测试内容。\\n\\n 📬 示例1 示例1 示例1 \\n 😍 示例1 示例1 示例1 \\n 📝 示例1 示例1 示例1 \\n 🙌 示例1 示例1 示例1 \",
\"tag\":\"lark_md\"
}},{
\"actions\":[{
\"tag\":\"button\",
\"text\":{
\"content\":\"立即查看\",
\"tag\":\"plain_text\"
},
\"type\":\"primary\",
\"url\":\"http://219.157.255.213:25321/feishu/show\"
}],
\"tag\":\"action\"
}],
\"header\":{
\"template\":\"turquoise\",
\"title\":{
\"content\":\"📚标题\",
\"tag\":\"plain_text\"
}
}
}";
$msgType = 'interactive';
$receiveIdtype = 'open_id';
$result = $class->sendMessages($receiveId, $content, $msgType, $receiveIdtype);
return json($result);
}
public function show()
{
return '<div>查看详情 - 示例页面</div>';
}
}
// 凭证
function certificate($gs = ''){
$data = [];
switch ($gs) {
case "xinyouhe":
$data = [
'appId' => "cli_a73e1e6406fa501c",
'appSecret' => "9cMfxujjGG9JPmNfNcOwUdCoIXqaM1wY",
'rUrl' => "https://server.xyh.hnzhwlkj.club",
'scope' => "contact:user.base:readonly contact:contact.base:readonly contact:user.employee_id:readonly im:message:send_as_bot",
'state' => "xinyouhe"
];
break;
case "mbaf":
$data = [
'appId' => "cli_a75839fb4ff85028",
'appSecret' => "VsqLvwsRzpmisBVbrTIuKdNCvRCO447d",
'rUrl' => "https://server.xyh.hnzhwlkj.club",
'scope' => "contact:user.base:readonly contact:contact.base:readonly contact:user.employee_id:readonly im:message:send_as_bot",
'state' => "mbaf"
];
break;
default:
throw new BusinessException('未授权应用');
break;
}
return $data;
}