飞书API

news/2025/2/22 15:19:09

extend目录下,API

<?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
// +----------------------------------------------------------------------
// | 飞书请求
// +----------------------------------------------------------------------
// | 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
// +----------------------------------------------------------------------
// | 消息发送脚本服务层
// +----------------------------------------------------------------------
// | 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;
    }
}


http://www.niftyadmin.cn/n/5862441.html

相关文章

电商API安全防护:JWT令牌与XSS防御实战

在电商 API 的安全防护中&#xff0c;JWT&#xff08;JSON Web Token&#xff09;令牌用于身份验证和授权&#xff0c;而 XSS&#xff08;跨站脚本攻击&#xff09;防御则是防止恶意脚本注入&#xff0c;保护用户数据和系统安全。以下是这两方面的实战介绍。 JWT 令牌实战 1. 生…

flowable适配达梦数据库

文章目录 适配相关问题无法从数据库产品名称“DM DBMS”中推断数据库类型分析解决 构建ibatis SqlSessionFactory时出错&#xff1a;inStream参数为null分析解决 liquibase相关问题问题一&#xff1a;不支持的数据库 Error executing SQL call current_schema: 无法解析的成员访…

网络安全-Mysql注入知识点

本篇文章介绍sql注入时数据库是Mysql时需要掌握的知识点。以Navicat、mysql 5.7.4为例。 注释 #或--空格是单行注释 /**/是内联注释 SQL语句 查询语句 SELECT〈目标列组〉 FROM〈数据源〉 [WHERE〈元组选择条件〉] [GROUP BY〈分列组〉[HAVING 〈组选择条件…

MySql面试宝典【刷题系列】

文章目录 一、Mysql 的存储引擎 myisam 和 innodb 的区别。二、MySQL数据库作发布系统的存储&#xff0c;一天五万条以上的增量&#xff0c;预计运维三年,怎么优化&#xff1f;三、对于大流量的网站,您采用什么样的方法来解决各页面访问量统计问题&#xff1f;四、锁的优化策略…

代码随想录算法训练day59---图论系列4

代码随想录算法训练 —day59 文章目录 代码随想录算法训练前言一、110.字符串接龙二、105.有向图的完全可达性dfs版本1dfs版本2bfs版本 三、100. 岛屿的最大面积方法一方法二 总结 前言 今天是算法营的第59天&#xff0c;希望自己能够坚持下来&#xff01; 今天继续图论part&…

工控自动化领域:数字量信号与模拟量信号的差异解析

在工控自动化的神秘世界里&#xff0c;信号如同传递指令和信息的使者&#xff0c;而数字量信号和模拟量信号则是其中的两大主角。它们各自有着独特的 “性格” 和 “使命”&#xff0c;在不同的场景中发挥着关键作用。下面&#xff0c;就让我们一起来深入了解一下它们的区别。 …

x安全服务 y安全体系 z网络安全模型 网络安全体系设计

这一年来&#xff0c;网络安全行业兴奋异常。各种会议、攻防大赛、黑客秀&#xff0c;马不停蹄。随着物联网大潮的到来&#xff0c;在这个到处都是安全漏洞的世界&#xff0c;似乎黑客才是安全行业的主宰。然而&#xff0c;我们看到的永远都是自己的世界&#xff0c;正如医生看…

关于css中bfc的理解

首先需要了解的是bfc的缩写是block formatting context 块级格式化上下文 那么块级格式化上下文意味着我们在bfc的区域内生成了一块独立于当前文本流的样式、它不受外界样式的影响、有着专属于自己的样式。 基于这样的定义我们可以避免一些样式冲突、在我们开发者面对复杂布局…