飞书API

extend目录下,API

php 复制代码
<?php
// +----------------------------------------------------------------------
// | 飞书API
// +----------------------------------------------------------------------
// | COPYRIGHT (C) 2021 http://www.jeoshi.com All rights reserved.
// +----------------------------------------------------------------------
// | AUTHOR : 陈龙
// | DATE   : 2024/01/19 10:45:47
// | VERSION:1.0.0
// +----------------------------------------------------------------------
namespace feishu;

class Api extends Request
{
    public static $instance = null;

    /**
     * 构造函数
     */
    public function __construct($option = [])
    {
        parent::__construct();

        $this->setAppConfig($option['company_id'] ?? 0);
    }

    /**
     * @desc 单例
     * @return static|null
     * @author 陈龙
     * @date 2024-01-09 11:00
     */
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static;
        }
        return self::$instance;
    }

    /**
     * @desc 自建应用获取 tenant_access_token
     * https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
     * @param $up_cache 刷新缓存
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function getTenantToken($up_cache = false)
    {
        return $this->getTenantAccessToken($up_cache);
    }

    /**
     * @desc 创建用户
     * https://open.feishu.cn/document/server-docs/contact-v3/user/create
     * @param $data 请求参数
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function createUser($data = [])
    {
        return $this->_createUser($data);
    }

    /**
     * @desc 修改用户部分信息
     * https://open.feishu.cn/document/server-docs/contact-v3/user/patch
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function updateUserInfo($path = [], $params = [], $data = [])
    {
        return $this->_updateUserInfo($path, $params, $data);
    }

    /**
     * @desc 删除用户
     * https://open.feishu.cn/document/server-docs/contact-v3/user/delete
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function deleteUser($path = [], $params = [], $data = [])
    {
        return $this->_deleteUser($path, $params, $data);
    }

    /**
     * @desc 恢复已删除用户
     * https://open.feishu.cn/document/server-docs/contact-v3/user/resurrect
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function resurrectUser($path = [], $params = [], $data = [])
    {
        return $this->_resurrectUser($path, $params, $data);
    }

    /**
     * @desc 获取单个用户信息
     * https://open.feishu.cn/document/server-docs/contact-v3/user/get
     * @param $path 路径参数
     * @param $params 查询参数
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function getUser($path = [], $params = [])
    {
        if (empty($params)) $params = ['user_id_type' => 'user_id'];

        return $this->_getUser($path, $params);
    }

    /**
     * @desc 批量获取用户信息
     * https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/user/batch
     * @param $params 查询参数
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function batchGetUser($params = [])
    {
        return $this->_batchGetUser($params);
    }

    /**
     * @desc 获取部门直属用户列表
     * https://open.feishu.cn/document/server-docs/contact-v3/user/find_by_department
     * @param $params 查询参数
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function findUserByDepartment($params = [])
    {
        return $this->_findUserByDepartment($params);
    }

    /**
     * @desc 通过手机号或邮箱获取用户 ID
     * https://open.feishu.cn/document/server-docs/contact-v3/user/batch_get_id
     * @param $params 查询参数
     * @param $data 请求参数
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function batchGetId($params = [], $data = [])
    {
        if (empty($params)) $params = ['user_id_type' => 'user_id'];

        return $this->_batchGetId($params, $data);
    }

    /**
     * @desc 更新用户ID
     * https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/user/update_user_id
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function updateUserId($path = [], $params = [], $data = [])
    {
        return $this->_updateUserId($path, $params, $data);
    }

    /**
     * @desc 创建部门
     * https://open.feishu.cn/document/server-docs/contact-v3/department/create
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:49
     */
    public function createDepartment($data = [])
    {
        return $this->_createDepartment($data);
    }

    /**
     * @desc 删除部门
     * https://open.feishu.cn/document/server-docs/contact-v3/department/delete
     * @param $path 路径参数
     * @param $params 查询参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:55
     */
    public function deleteDepartment($path = [], $params = [])
    {
        return $this->_deleteDepartment($path, $params);
    }

    /**
     * @desc 修改部门部分信息
     * https://open.feishu.cn/document/server-docs/contact-v3/department/patch
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function updateDepartmentInfo($path = [], $params = [], $data = [])
    {
        return $this->_updateDepartmentInfo($path, $params, $data);
    }

    /**
     * @desc 更新部门所有信息
     * https://open.feishu.cn/document/server-docs/contact-v3/department/update
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function updateDepartmentAllInfo($path = [], $params = [], $data = [])
    {
        return $this->_updateDepartmentAllInfo($path, $params, $data);
    }

    /**
     * @desc 获取单个部门信息
     * https://open.feishu.cn/document/server-docs/contact-v3/department/get
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function getDepartment($path = [], $params = [])
    {
        return $this->_getDepartment($path, $params);
    }

    /**
     * @desc 批量获取部门信息
     * https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/department/batch
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function batchGetDepartment($params = [])
    {
        return $this->_batchGetDepartment($params);
    }

    /**
     * @desc 获取子部门列表
     * https://open.feishu.cn/document/server-docs/contact-v3/department/children
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function getDepartmentChildren($path = [], $params = [])
    {
        return $this->_getDepartmentChildren($path, $params);
    }

    /**
     * @desc 获取父部门信息
     * https://open.feishu.cn/document/server-docs/contact-v3/department/parent
     * @return mixed
     * @author 陈龙
     * @date 2024-01-09 11:02
     */
    public function getDepartmentParent($params = [])
    {
        return $this->_getDepartmentParent($params);
    }

    /**
     * @desc 更新部门ID
     * https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/department/update_department_id
     * @param $path 路径参数
     * @param $params 查询参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function updateDepartmentId($path = [], $params = [], $data = [])
    {
        return $this->_updateDepartmentId($path, $params, $data);
    }

    /**
     * @desc 创建角色
     * https://open.feishu.cn/document/server-docs/contact-v3/functional_role/create
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:50
     */
    public function createRole($data = [])
    {
        return $this->_createRole($data);
    }

    /**
     * @desc 修改角色名称
     * https://open.feishu.cn/document/server-docs/contact-v3/functional_role/update
     * @param $path 路径参数
     * @param $data 请求参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:55
     */
    public function updateRoleName($path = [], $data = [])
    {
        return $this->_updateRoleName($path, $data);
    }

    /**
     * @desc 删除角色
     * https://open.feishu.cn/document/server-docs/contact-v3/functional_role/delete
     * @param $path 路径参数
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:55
     */
    public function deleteRole($path = [])
    {
        return $this->_deleteRole($path);
    }

    /**
     * @desc 发送消息
     * https://open.feishu.cn/document/server-docs/im-v1/message/create
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-08-14 13:57
     */
    public function messages($data)
    {
        if (empty($params)) $params = ['receive_id_type' => 'user_id'];

        return $this->_messages($params, $data);
    }
}

REQUEST:

php 复制代码
<?php
// +----------------------------------------------------------------------
// | 飞书请求
// +----------------------------------------------------------------------
// | COPYRIGHT (C) 2021 http://www.jeoshi.com All rights reserved.
// +----------------------------------------------------------------------
// | AUTHOR : 陈龙
// | DATE   : 2024/01/19 10:45:47
// | VERSION:1.0.0
// +----------------------------------------------------------------------
namespace feishu;

use think\Cache;
use think\Config;

class Request
{
	/**
	 * @desc  redis 对象
	 * @var   object
	 */
	private $redis;

	/**
	 * 未经过封装的redis
	 */
	private $raw_redis;

    /**
     * 请求域名
     */
    private $domain = "https://open.feishu.cn/open-apis";

	/**
	 * 应用唯一标识
	 */
	private $app_id = '';

	/**
	 * 应用秘钥
	 */
	private $app_secret = '';

    /**
     * 公司ID
     */
    private $company_id = '';

    /**
     * 构造函数
     */
	public function __construct()
	{
		$this->redis      = Cache::connect(Config::get('cache.redis'));
		$this->raw_redis  = $this->redis->handler();
	}

    /**
     * @desc setAppConfig
     * @return void
     * @author 陈龙
     * @date 2024-04-20 9:57
     */
    public function setAppConfig($company_id)
    {
        $this->company_id = $company_id;
        if ($company_id == 23){
            $this->app_id = Config::get('feishu.app')[23]['app_id'] ?? '';
            $this->app_secret = Config::get('feishu.app')[23]['app_secret'] ?? '';
        }elseif ($company_id == 36){
            $this->app_id = Config::get('feishu.app')[36]['app_id'] ?? '';
            $this->app_secret = Config::get('feishu.app')[36]['app_secret'] ?? '';
        }else{
            $this->app_id = Config::get('feishu.app')[0]['app_id'] ?? '';
            $this->app_secret = Config::get('feishu.app')[0]['app_secret'] ?? '';
        }
    }

    /**
     * @desc Curl请求
     * @param $url
     * @param $data
     * @param $method
     * @param $withToken
     * @param $header
     * @return bool|string
     * @author 陈龙
     * @date 2024-01-09 17:32
     */
    public function httpCurl($url, $data, $method = 'POST', $withToken = true, $header = '')
    {
        $headers = $this->buildHeader($header,$withToken);
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $method = strtoupper($method);
        switch ($method){
            case 'POST':
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
                break;
            case 'PUT':
            case 'PATCH':
            case 'DELETE':
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
                break;
        }

        $res = curl_exec($curl);
        curl_close($curl); //关闭URL请求
        return $res;
    }

    /**
     * @desc 构建头部参数
     * @param $header
     * @param $withToken
     * @return array
     * @author 陈龙
     * @date 2024-01-09 11:28
     */
    public function buildHeader($header = '',$withToken = true)
    {
        $headers[] = "Content-Type: application/json; charset=utf-8";

        if ($withToken) $headers[] = "Authorization: Bearer " . $this->getTenantAccessToken();

        if (!empty($header)) $headers[] = $header;

        return $headers;
    }

    /**
     * @desc 返回数据
     * @param $code 错误码,非 0 表示失败
     * @param $msg 错误描述
     * @param $data
     * @return array
     * @author 陈龙
     * @date 2024-01-09 18:13
     */
	public function back($code = 0, $msg = '', $data = [])
	{
		return ['code' => $code, 'msg' => $msg, 'data' => $data];
	}

    /**
     * @desc 错误返回
     * @param $msg
     * @param $code
     * @return array
     * @author 陈龙
     * @date 2024-01-09 18:34
     */
    public function error($msg = '', $code = 400)
    {
        return $this->back($code,$msg);
    }

    /**
     * @desc 自建应用获取 tenant_access_token
     * @param $up_cache
     * @return mixed|string
     * @author 陈龙
     * @date 2024-01-09 11:51
     */
    public function getTenantAccessToken($up_cache = false)
	{
        $redisKey = 'ehr:feishu:tenant_access_token:' . $this->company_id;
        $tenant_access_token = $this->redis->get($redisKey);
        if (empty($tenant_access_token) || $up_cache == true) {
            $url = $this->domain.'/auth/v3/tenant_access_token/internal';
            $data = [
                'app_id' => $this->app_id,
                'app_secret' => $this->app_secret,
            ];
            $res = $this->httpCurl($url,$data,'POST',false);
            $res = json_decode($res,true);
            $tenant_access_token = $res['tenant_access_token'] ?? '';
            $expire = max(($res['expire'] ?? 0) - 900, 0);
            $this->redis->set($redisKey, $tenant_access_token, $expire);
        }
        return $tenant_access_token;
	}

    /**
     * @desc 创建用户
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:21
     */
    public function _createUser($data = [])
    {
        if (empty($data['name'] ?? '')) return $this->error('用户名不能为空');
        if (empty($data['mobile'] ?? '')) return $this->error('手机号不能为空');
        if (empty($data['department_ids'] ?? '')) return $this->error('用户所属部门不能为空');
        if (empty($data['employee_type'] ?? '')) return $this->error('员工类型不能为空');

        $url = $this->domain.'/contact/v3/users';
        $res = $this->httpCurl($url, $data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 修改用户部分信息
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _updateUserInfo($path = [], $params = [], $data = [])
    {
        $user_id = $path['user_id'] ?? '';
        if (empty($user_id)) return $this->error('用户ID不能为空');

        $url = $this->domain."/contact/v3/users/{$user_id}?".http_build_query($params);
        $res = $this->httpCurl($url,$data,'PATCH');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 删除用户
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _deleteUser($path = [], $params = [], $data = [])
    {
        $user_id = $path['user_id'] ?? '';
        if (empty($user_id)) return $this->error('用户ID不能为空');

        $url = $this->domain."/contact/v3/users/{$user_id}?".http_build_query($params);
        $res = $this->httpCurl($url,$data,'DELETE');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 恢复已删除用户
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _resurrectUser($path = [], $params = [], $data = [])
    {
        $user_id = $path['user_id'] ?? '';
        if (empty($user_id)) return $this->error('用户ID不能为空');

        $url = $this->domain."/contact/v3/users/{$user_id}/resurrect?".http_build_query($params);
        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 获取单个用户信息
     * @param $path
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _getUser($path = [], $params = [])
    {
        $user_id = $path['user_id'] ?? '';
        if (empty($user_id)) return $this->error('用户ID不能为空');

        $url = $this->domain."/contact/v3/users/{$user_id}?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 批量获取用户信息
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _batchGetUser($params = [])
    {
        $user_ids = $params['user_ids'] ?? '';
        if (empty($user_ids)) return $this->error('用户ID不能为空');

        $url = $this->domain."/contact/v3/users/batch?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 获取部门直属用户列表
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _findUserByDepartment($params = [])
    {
        $url = $this->domain."/contact/v3/users/find_by_department?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 通过手机号或邮箱获取用户 ID
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _batchGetId($params = [],$data = [])
    {
        $url = $this->domain."/contact/v3/users/batch_get_id?".http_build_query($params);
        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 更新用户ID
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _updateUserId($path = [], $params = [], $data = [])
    {
        $user_id = $path['user_id'] ?? '';
        if (empty($user_id)) return $this->error('用户ID不能为空');
        $new_user_id = $data['new_user_id'] ?? '';
        if (empty($new_user_id)) return $this->error('新用户ID不能为空');

        $url = $this->domain."/contact/v3/users/{$user_id}/update_user_id?".http_build_query($params);
        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 创建部门
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:48
     */
    public function _createDepartment($data = [])
    {
        if (empty($data['name'] ?? '')) return $this->error('部门名称不能为空');

        $url = $this->domain.'/contact/v3/departments';

        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 删除部门
     * @param $path
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:54
     */
    public function _deleteDepartment($path = [], $params = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}?".http_build_query($params);

        $res = $this->httpCurl($url,'','DELETE');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 修改部门部分信息
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _updateDepartmentInfo($path = [], $params = [], $data = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}?".http_build_query($params);
        $res = $this->httpCurl($url,$data,'PATCH');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 更新部门所有信息
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _updateDepartmentAllInfo($path = [], $params = [], $data = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');
        $name = $path['name'] ?? '';
        if (empty($name)) return $this->error('部门名称不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}?".http_build_query($params);
        $res = $this->httpCurl($url,$data,'PUT');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 获取单个部门信息
     * @param $path
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _getDepartment($path = [], $params = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 批量获取部门信息
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _batchGetDepartment($params = [])
    {
        $department_ids = $params['department_ids'] ?? '';
        if (empty($department_ids)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/batch?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 获取子部门列表
     * @param $path
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _getDepartmentChildren($path = [], $params = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}/children?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 获取父部门信息
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _getDepartmentParent($params = [])
    {
        $department_id = $params['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}/parent?".http_build_query($params);
        $res = $this->httpCurl($url,'','GET');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 更新部门ID
     * @param $path
     * @param $params
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 18:31
     */
    public function _updateDepartmentId($path = [], $params = [], $data = [])
    {
        $department_id = $path['department_id'] ?? '';
        if (empty($department_id)) return $this->error('部门ID不能为空');
        $department_id = $data['new_department_id'] ?? '';
        if (empty($department_id)) return $this->error('新部门ID不能为空');

        $url = $this->domain."/contact/v3/departments/{$department_id}/update_department_id?".http_build_query($params);
        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 创建角色
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:50
     */
    public function _createRole($data = [])
    {
        if (empty($data['role_name'] ?? '')) return $this->error('角色名称不能为空');

        $url = $this->domain.'/contact/v3/functional_roles';

        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 修改角色名称
     * @param $path
     * @param $data
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:54
     */
    public function _updateRoleName($path = [], $data = [])
    {
        $role_id = $path['role_id'] ?? '';
        if (empty($role_id)) return $this->error('角色ID不能为空');
        if (empty($data['role_name'] ?? '')) return $this->error('角色名称不能为空');

        $url = $this->domain."/contact/v3/functional_roles/{$role_id}";

        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }

    /**
     * @desc 删除角色
     * @param $path
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 19:54
     */
    public function _deleteRole($path = [])
    {
        $role_id = $path['role_id'] ?? '';
        if (empty($role_id)) return $this->error('角色ID不能为空');

        $url = $this->domain."/contact/v3/functional_roles/{$role_id}";

        $res = $this->httpCurl($url,'','DELETE');
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }


    /**
     * @desc 发送消息
     * @param $path
     * @param $params
     * @return array|mixed
     * @author 陈龙
     * @date 2024-01-09 20:13
     */
    public function _messages($params = [], $data = [])
    {
        $receive_id = $data['receive_id'] ?? '';
        if (empty($receive_id)) return $this->error('receive_id不能为空');

        $url = "https://open.feishu.cn/open-apis/im/v1/messages?".http_build_query($params);

        $res = $this->httpCurl($url,$data);
        $res = json_decode($res,true);

        if (empty($res)) return $this->error('返回异常');
        return $res;
    }
}

service调用:

php 复制代码
<?php
// +----------------------------------------------------------------------
// | 消息发送脚本服务层
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.jeoshi.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 陈龙
// +----------------------------------------------------------------------

namespace app\command\message\service;

use app\common\model\Message;
use app\common\model\Users;
use app\common\model\UsersMessages;
use feishu\Api;
use think\Cache;
use think\Config;
use think\console\Output;
use think\QueueAmqp;

class FeishuService extends Base
{
    protected $id;
    protected $to_third_id;
    protected $msg_type;
    protected $msg_type_attr;
    protected $msg_title;
    protected $msg_content;
    protected $uuid;

    /**
     * @var null 单列对象
     */
    public static $instance = null;

    /**
     * 输出对象
     * @var Output
     */
    public $output;

    public function __construct()
    {
        $this->output = new Output();
    }

    /**
     * @desc 陈龙
     * @return static|null
     * @author 陈龙
     * @date 2023-03-28 14:10
     */
    public static function instance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static;
        }
        return self::$instance;
    }

    /**
     * @desc 发送信息
     * @param $message
     * @return array
     * @author 陈龙
     * @date 2024-08-14 13:36
     */
    public function send($message = [])
    {
        $this->id             =   $message['id'] ?? '';
        $this->uuid           =   $message['uuid'] ?? '';
        $this->to_third_id    =   $message['to_third_id'] ?? 0;
        $this->msg_type       =   $this->typeAttr($message['msg_type'] ?? 1);
        $this->msg_type_attr  =   $message['msg_type_attr'] ?? 0;
        $this->msg_title      =   $message['msg_title'] ?? '';
        $msg_content          =   $message['msg_content'] ?? '';
        $this->msg_content    =   $this->msg_type == 'text' ? $msg_content : json_decode($msg_content, true);
        $this->msg_content    =   $this->contentAttr();

        $msg_data = [
            'receive_id'    =>  $this->to_third_id,
            'msg_type'      =>  $this->msg_type,
            'content'       =>  json_encode($this->msg_content),
            'uuid'          =>  md5($this->id . "|" . $this->uuid)
        ];

        $feishuApi = new Api(['company_id' => $message['company_id'] ?? 0]);
        $res = $feishuApi->messages($msg_data);
        $code = $res['code'] ?? '';
        if ($code != 0) return back(0, $res['msg'] ?? '');
        return back(1, '发送成功');
    }

    /**
     * @desc 消息类型转换
     * @param $type
     * @return string
     * @author 陈龙
     * @date 2024-08-13 14:49
     */
    private function typeAttr($type = 1)
    {
        switch ($type){
            case 1:
                $type_name = 'text'; //文本
                break;
            case 2:
                $type_name = 'interactive'; //卡片
                break;
            default:
                $type_name = 'text';
                break;
        }
        return $type_name;
    }

    /**
     * @desc 内容类型转换
     * @param $content
     * @return string
     * @author 陈龙
     * @date 2024-08-13 14:49
     */
    private function contentAttr()
    {
        switch ($this->msg_type){
            case 'text':
                return $this->textAttr();
            case 'interactive':
                return $this->cardAttr();
        }
    }

    /**
     * @desc 文本转换
     * @return string
     * @author 陈龙
     * @date 2024-08-13 14:49
     */
    private function textAttr()
    {
        $data[$this->msg_type] = $this->msg_content;
        return $data;
    }

    /**
     * @desc 卡片转换
     * @return array|null
     * @author 陈龙
     * @date 2024-08-20 19:45
     */
    private function cardAttr()
    {
        switch ($this->msg_type_attr){
            case 1:
                return $this->cardTask();
            default:
                return $this->msg_content;
        }
    }

    /**
     * @desc 任务类卡片消息(审核/处理)
     * @return array
     * @author 陈龙
     * @date 2024-08-20 19:45
     */
    private function cardTask()
    {
        $data = [
            'config' => [
                'wide_screen_mode' => true
            ],
            'header' => [
                'template' => 'turquoise',
                'title' => [
                    'content' => $this->msg_title,
                    'tag' => 'plain_text',
                ],
            ],
            'elements' => $this->cardTaskAttr()
        ];

        return $data;
    }

    /**
     * @desc 卡片任务转换
     * @return array
     * @author 陈龙
     * @date 2024-08-14 17:51
     */
    private function cardTaskAttr()
    {
        // 内容
        $elements = $this->_handleElements();

        // 操作
        $actions = $this->_handleActions();
        if (!empty($actions)){
            $elements[] = [
                'actions' => $actions,
                'tag' => 'action',
            ];
        }

        return $elements;
    }

    /**
     * @desc 处理操作
     * @return array
     * @author 陈龙
     * @date 2024-08-20 17:43
     */
    private function _handleActions()
    {
        $actions = [];
        $temp_actions = $this->msg_content['actions'] ?? [];
        foreach ($temp_actions as $action){
            $actions[] = [
                'tag' => 'button',
                'text' => [
                    'content' => $action['content'],
                    'tag' => 'plain_text',
                ],
                'type' => 'button',
                'url' => $action['url'],
            ];
        }
        return $actions;
    }

    /**
     * @desc 处理元素
     * @return array
     * @author 陈龙
     * @date 2024-08-20 17:44
     */
    private function _handleElements()
    {
        $elements = [];
        $temp_elements = $this->msg_content['elements'] ?? [];
        foreach ($temp_elements as $item){
            $columns = $item['columns'] ?? [];
            if (empty($columns)) {
                // 单行
                $lable = $item['lable'] ?? '';
                $content = $item['content'] ?? '';

                $elements[] = [
                    'tag' => 'markdown',
                    'content' => "**{$lable}:**\n{$content}",
                    'text_align' => 'left',
                    'text_size' => 'normal',
                ];
            }else{
                $columns_elements = [];
                // 多行
                foreach ($columns as $column){
                    $lable = $column['lable'] ?? '';
                    $content = $column['content'] ?? '';

                    $columns_elements[] = [
                        "tag" => "column",
                        "width" => "weighted",
                        "vertical_align" => "top",
                        "vertical_spacing" => "8px",
                        "background_style" => "default",
                        "elements" => [
                            [
                                "tag" => "markdown",
                                "content" => "**{$lable}:**\n{$content}",
                                "text_align" => "left",
                                "text_size" => "normal"
                            ]
                        ],
                        "weight" => 1
                    ];
                }

                $elements[] = [
                    "tag" => "column_set",
                    "flex_mode" => "stretch",
                    "background_style" => "default",
                    "horizontal_spacing" => "8px",
                    "horizontal_align" => "left",
                    "columns" => $columns_elements,
                    "margin" => "16px 0px 0px 0px"
                ];
            }
        }
        return $elements;
    }
}
相关推荐
宝哥聊编程2 天前
主流 Agent 平台对比:Coze、Dify、飞书、扣子、OpenClaw 落地实操手册
前端·人工智能·飞书
guoran_shini4 天前
Agent系统服务化封装和IM接入
飞书
mudtools6 天前
在飞书 SDK 上用 Microsoft Agent Framework 搭一个能干活的工作搭子
人工智能·ai·.net·飞书·agent
极致了7 天前
飞书插件、RPA、API三条路的真实分界线
飞书·rpa
玩泥巴的8 天前
在飞书 SDK 上用 Microsoft Agent Framework 搭一个能干活的工作搭子
ai·.net·飞书·agent
玩泥巴的9 天前
一份声明,多份产物:飞书 SDK 的 AI 工具面是怎么生成的
ai·.net·飞书·agent
OpenPortal认证计费系统11 天前
基于 飞书认证 通过 华为、H3C 结合 OpenPortal认证计费系统 实现 网络准入 飞书授权 实名认证
华为·飞书·h3c·portal认证·openportal
前进的李工12 天前
飞书企业知识库设计+后端高频八股
数据库·redis·后端·mysql·性能优化·飞书·rag
长弓三石12 天前
钉钉与飞书机器人接入企业级智能体过程及排障实录
人工智能·机器人·钉钉·飞书·企业级harness