251 lines
8.6 KiB
PHP
251 lines
8.6 KiB
PHP
<?php
|
|
|
|
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.xinyouhe';
|
|
|
|
/**
|
|
* 不需要登录的方法
|
|
*/
|
|
protected $noNeedLogin = ['auth', 'send', 'show'];
|
|
|
|
/**
|
|
* 获取应用信息
|
|
* @return \support\Response
|
|
*/
|
|
public function getinfo(Request $request)
|
|
{
|
|
$config = Util::getParam($request, 'config');
|
|
if (Util::isEmpty($config)) {
|
|
$config = $this->config;
|
|
}
|
|
|
|
// 获取凭证
|
|
$certificate = config($config);
|
|
$arr = [
|
|
'app_id' => $certificate['appId'],
|
|
'redirect_uri' => $certificate['rUrl'],
|
|
'scope' => $certificate['scope'],
|
|
'state' => $certificate['state'].'_'. $request->user['id']
|
|
];
|
|
return Util::success($arr,trans('success'));
|
|
}
|
|
|
|
/**
|
|
* 获取飞书code
|
|
* @return \support\Response
|
|
*/
|
|
public function getAuth(Request $request)
|
|
{
|
|
return $this->auth($request);
|
|
}
|
|
|
|
public function auth(Request $request)
|
|
{
|
|
$param = $request->all();
|
|
|
|
// 获取凭证
|
|
$certificate = config($this->config);
|
|
|
|
// 飞书服务
|
|
$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('授权失败,请重试');
|
|
}
|
|
|
|
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"]=>
|
|
// 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 {
|
|
//需要登录并且未提供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);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @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($this->config);
|
|
|
|
// 飞书服务
|
|
$class = new \app\service\FeishuService($certificate['appId'], $certificate['appSecret'], $certificate['state']);
|
|
|
|
// $receiveId = 'ou_37bb7cfc4311c858ea32461700fa1a43';
|
|
// $receiveId = 'ou_a3b97543acbf1b28d978356bb08f85dd';
|
|
|
|
// 富文本 类型
|
|
$content = "{
|
|
\"zh_cn\":{
|
|
\"title\":\"$title\",
|
|
\"content\":[
|
|
[{
|
|
\"tag\":\"text\",
|
|
\"text\":\"$content\",
|
|
\"style\":[\"bold\"]
|
|
}],
|
|
[{
|
|
\"tag\":\"hr\"
|
|
}],
|
|
[{
|
|
\"tag\":\"a\",
|
|
\"href\":\"$href\",
|
|
\"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;
|
|
} |