257 lines
8.4 KiB
PHP
257 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\model\FeishuUser;
|
|
use app\model\User;
|
|
use app\util\Util;
|
|
use config\constants\Constants;
|
|
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;
|
|
// }
|
|
$userId = $request->user['id'];
|
|
$config = User::where('id',$userId)->value('sign');
|
|
if(Util::isEmpty($config)){
|
|
return Util::fail(null,trans('sign_not_null'));
|
|
}
|
|
|
|
// 获取凭证
|
|
$certificate = config('feishu.'.$config);
|
|
$arr = [
|
|
'app_id' => $certificate['appId'],
|
|
'redirect_uri' => $certificate['rUrl'],
|
|
'scope' => $certificate['scope'],
|
|
'state' => $certificate['state'].'_'. $request->user['id'],
|
|
'url' => $request->header('referer')
|
|
];
|
|
return Util::success($arr,trans('success'));
|
|
}
|
|
/**
|
|
* 获取应用信息
|
|
* @return \support\Response
|
|
*/
|
|
public function getSign(Request $request)
|
|
{
|
|
|
|
$res = Constants::SIGN;
|
|
// foreach ($certificate as $k => $v) {
|
|
// $res []=['id'=>$k,'name'=>$k];
|
|
// }
|
|
return Util::success($res,trans('success'));
|
|
}
|
|
|
|
/**
|
|
* 获取飞书code
|
|
* @return \support\Response
|
|
*/
|
|
public function getAuth(Request $request)
|
|
{
|
|
return $this->auth($request);
|
|
}
|
|
|
|
public function auth(Request $request)
|
|
{
|
|
$param = $request->all();
|
|
$userId = $request->user['id'];
|
|
$config = User::where('id',$userId)->value('sign');
|
|
if(Util::isEmpty($config)){
|
|
return Util::fail(null,trans('sign_not_null'));
|
|
}
|
|
|
|
// 获取凭证
|
|
$certificate = config('feishu.'.$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[1];
|
|
// $user = FeishuUser::where('id', $userId)->first();
|
|
// $user = FeishuUser::create();
|
|
|
|
// //保存用户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();
|
|
|
|
$user = new FeishuUser();
|
|
$user->firstOrCreate([
|
|
'user_id' => $userId,
|
|
'sign' => $state[0],
|
|
'tenant_key' => $rst['data']['tenant_key'],
|
|
'open_id' => $rst['data']['open_id'],
|
|
'feishu_user_id' => $rst['data']['user_id'],
|
|
'union_id' => $rst['data']['union_id']
|
|
]);
|
|
|
|
// return json($rst);
|
|
return Util::success($rst['data'], trans('success'));
|
|
} 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 static function send($config='xinyouhe',$receiveId='ou_e33bb3017ada6c4781398a68e55a4696',$title='ceshi',$content='测试',$href='http://219.157.255.213:8085/pages/index/mine')
|
|
{
|
|
// 获取凭证
|
|
$certificate = config('feishu.'.$config);
|
|
Log::info('feishu send', [$certificate,'config'=>$config,'receiveId' => $receiveId, 'title' => $title, 'content' => $content, 'href' => $href]);
|
|
|
|
// 飞书服务
|
|
$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';
|
|
$msgType = 'post'; //卡片消息:interactive,富文本消息:post
|
|
$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;
|
|
} |