Commit 037b3a90 authored by huanle's avatar huanle

0525_

parent 5d020cf3
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src;
/**
* 钉钉SDK
*/
class Dingtalk
{
/**
* 钉钉实例
* @var object
*/
protected static $instance = null;
/**
* 接口调用地址
* @var string
*/
protected static $baseUrl = 'https://oapi.dingtalk.com/';
/**
* 请求头信息
* @var array
*/
protected static $headers = [
'Content-Type' => 'application/json',
];
/**
* 错误信息
* @var null
*/
protected static $error = null;
/**
* 钉钉的配置信息
* @var array
*/
protected static $config = [
'agentid' => 781256835,
'appkey' =>'ding205fbymmwea4mtln',
'appsecret' =>'S_E2VRls4GZLU11K-mlEyFAslu2I0T1jbtAKrAZBJea_A32fLP1FV9QQ5D7_mYdW',
'corpid' => 'dingeb18cb3ded3f0424',
'corpsecret' => 'ding205fbymmwea4mtln',
'ssosecret' => 'S_E2VRls4GZLU11K-mlEyFAslu2I0T1jbtAKrAZBJea_A32fLP1FV9QQ5D7_mYdW',
'access_token' => '',
'jsapi_ticket'=>'',
];
/**
* 实例化钉钉SDK
* @param array $config 配置信息
*/
public function __construct($config = [])
{
if (!empty($config) && is_array($config)) {
self::$config = array_merge(self::$config, $config);
}
}
/**
* 实例化的静态方法
* @param array $config 配置信息
* @param boolean $force 强制重新实例化
* @return \tools\DDing
*/
public static function instance($config = [], $force = false)
{
if (is_null(self::$instance) || $force == true) {
self::$instance = new static($config);
}
return self::$instance;
}
/**
* 设置/获取 配置变量
* @param string $key
* @param string $value
* @return string
*/
public static function config($key, $value = null)
{
if (is_null($value)) {
return self::$config[$key];
} else {
self::$config[$key] = $value;
}
}
/**
* JS-API权限验证参数生成
* @return array
*/
public static function ddConfig()
{
$nonceStr = uniqid();
$timestamp = time();
$config = [
'agentId' => self::$config['agentid'],
'corpId' => self::$config['corpid'],
'timeStamp' => $timestamp,
'nonceStr' => $nonceStr,
];
$config['signature'] = self::sign($nonceStr, $timestamp);
return $config;
}
/**
* 钉钉签名算法
* @param string $noncestr
* @param string $timestamp
* @return string
*/
private static function sign($noncestr, $timestamp)
{
$signArr = [
'jsapi_ticket' => self::$config['jsapi_ticket'],
'noncestr' => $noncestr,
'timestamp' => $timestamp,
'url' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], // 获取当前页面地址 有待优化
];
ksort($signArr);
//var_dump($signArr);
$signStr = urldecode(http_build_query($signArr));
return sha1($signStr);
}
/**
* 设置/获取 错误信息
* @param string $msg
* @return string
*/
public static function error($msg = null)
{
if (!is_null($msg)) {
self::$error = $msg;
} else {
return self::$error;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
*
*/
class AppClient extends Dingtalk
{
/**
* 初始化APP端钉钉配置
* @param string $agentId agentId
* @param array $jsApiList 需要使用的jsapi列表
* @return array
*/
public static function init($agentId, $jsApiList = [])
{
$return = '<script src="https://g.alicdn.com/dingding/open-develop/0.7.0/dingtalk.js" type="text/javascript" charset="utf-8"></script>';
$return .= '<script type="text/javascript" charset="utf-8">';
$return .= 'dd.config(';
$return .= self::ddConfig($agentId, $jsApiList);
$return .= ');';
$return .= '</script>' . "\r\n";
return [
'init' => $return,
'corpid' => parent::$config['corpid'],
];
}
/**
* JS-API权限验证参数生成
* @return array
*/
private static function ddConfig($agentId, $jsApiList)
{
$nonceStr = uniqid();
$timestamp = time();
$config = [
'agentId' => $agentId,
'corpId' => parent::$config['corpid'],
'timeStamp' => $timestamp,
'nonceStr' => $nonceStr,
];
$config['signature'] = self::sign($nonceStr, $timestamp);
$config['jsApiList'] = $jsApiList;
return json_encode($config);
}
/**
* 钉钉签名算法
* @param string $noncestr
* @param string $timestamp
* @return string
*/
private static function sign($noncestr, $timestamp)
{
$jsapi_ticket = parent::$config['jsapi_ticket'];
if (empty($jsapi_ticket)) {
$jsapi_ticket = Token::jsapi();
}
$signArr = [
'jsapi_ticket' => $jsapi_ticket,
'noncestr' => $noncestr,
'timestamp' => $timestamp,
'url' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], // 获取当前页面地址 有待优化
];
ksort($signArr);
$signStr = urldecode(http_build_query($signArr));
return sha1($signStr);
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 考勤打卡
*/
class Attendance extends Dingtalk
{
/**
* 获取考勤打卡数据
* @param dateTime $from 考勤开始时间
* @param dateTime $to 考勤结束时间 跨度不能超过7天
* @param string $userId 用户userid
* @return array|boolean
*/
public static function get($from, $to, $userId = null)
{
$params = [
'workDateFrom' => $from,
'workDateTo' => $to,
];
if (!is_null($userId)) {
$params['userId'] = $userId;
}
$result = Utils::post('attendance/list', $params);
if (false !== $result) {
return $result['recordresult'];
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* Department 通讯录管理
*/
class Department extends Dingtalk
{
/**
* 获取部门列表
* @return array|boolean
*/
public static function lists()
{
$result = Utils::get('department/list');
if (false !== $result) {
return $result['department'];
} else {
return false;
}
}
/**
* 获取部门详情
* @param integer $id 部门ID
* @return array|boolean
*/
public static function info($id)
{
$params = [
'id' => $id,
];
$result = Utils::get('department/get', $params);
if (false !== $result) {
return $result;
} else {
return false;
}
}
/**
* 创建部门
* @param string|array $name
* @param integer $parentid
* @return integer|boolean
*/
public static function create($name, $parentid = 1)
{
if (is_array($name)) {
$params = $name;
} else {
$params = [
'name' => $name,
'parentid' => $parentid,
];
}
$result = Utils::post('department/create', $params);
if (false !== $result) {
return $result['id'];
} else {
return false;
}
}
/**
* 更新部门
* @param string|array $name
* @param integer $id
* @return boolean
*/
public static function update($name, $id)
{
if (is_array($name)) {
$params = $name;
} else {
$params = [
'id' => $id,
'name' => $name,
];
}
$result = Utils::post('department/update', $params);
if (false !== $result) {
return true;
} else {
return false;
}
}
/**
* 删除部门
* @param integer $id
* @return boolean
*/
public static function delete($id)
{
$params = [
'id' => $id,
];
$result = Utils::get('department/delete', $params);
if (false !== $result) {
return true;
} else {
return false;
}
}
/**
* 获取部门用户USERID列表
* @param integer $departmentId
* @return array|boolean
*/
public static function users($departmentId)
{
$params = [
'department_id' => $departmentId,
];
$result = Utils::get('user/simplelist', $params);
if (false !== $result) {
return $result['userlist'];
} else {
return false;
}
}
/**
* 获取部门用户详情列表
* @param [type] $departmentId [description]
* @return [type] [description]
*/
public static function usersDetail($departmentId)
{
$params = [
'department_id' => $departmentId,
];
$result = Utils::get('user/list', $params);
if (false !== $result) {
return $result['userlist'];
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 管理多媒体文件
*/
class Media extends Dingtalk
{
/**
* 上传多媒体文件/ 接口未完成
* @return [type] [description]
*/
public static function upload()
{
$params = [
'type' => $type,
'media' => $media,
];
$result = Utils::post('media/upload', $params);
if (false !== $result) {
return $result['media_id'];
} else {
return false;
}
}
/**
* 获取媒体文件
* @param string $mediaId 媒体文件的唯一标示
* @return [type]
*/
public static function get($mediaId)
{
$params = [
'media_id ' => $mediaId,
];
$result = Utils::get('media/get', $params);
if (false !== $result) {
return $result;
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 会话消息
*/
class Message extends Dingtalk
{
/**
* 发送企业会话消息
* @param [type] $touser [description]
* @param [type] $agentid [description]
* @param [type] $msgtype [description]
* @param [type] $msgbody [description]
* @return [type] [description]
*/
public static function send($touser, $agentid, $msgtype, $msgbody)
{
$params = [
'touser' => $touser,
'agentid' => $agentid,
'msgtype' => $msgtype,
$msgbody,
];
$result = Utils::post('message/send', $params);
if (false !== $result) {
unset($result['errcode']);
unset($result['errmsg']);
return $result;
} else {
return false;
}
}
/**
* 获取企业会话消息已读未读状态
* @param string $messageId 消息id
* @return array|boolean
*/
public static function status($messageId)
{
$params = [
'messageId' => $messageId,
];
$result = Utils::post('message/list_message_status', $params);
if (false !== $result) {
unset($result['errcode']);
unset($result['errmsg']);
return $result;
} else {
return false;
}
}
/**
* 发送普通会话消息
* @param [type] $sender [description]
* @param [type] $cid [description]
* @param [type] $msgtype [description]
* @param [type] $msgbody [description]
* @return [type] [description]
*/
public static function common($sender, $cid, $msgtype, $msgbody)
{
$params = [
'sender' => $sender,
'cid' => $cid,
'msgtype' => $msgtype,
$msgbody,
];
$result = Utils::post('message/send_to_conversation', $params);
if (false !== $result) {
return $result['receiver'];
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 管理微应用
*/
class Microapp extends Dingtalk
{
/**
* 创建微应用
* @return [type] [description]
*/
public static function create()
{
// "appIcon": "@HIdsabikkhjsdsas",
// "appName": "测试微应用",
// "appDesc": "测试使用的微应用",
// "homepageUrl": "http://oa.dingtalk.com/?h5",
// "pcHomepageUrl": "http://oa.dingtalk.com/?pc",
// "ompLink": ""
$params = [
];
$result = Utils::post('microapp/create', $params);
if (false !== $result) {
return $result['id'];
} else {
return false;
}
}
/**
* 获取企业设置的微应用可见范围
* @param [type] $agentId [description]
*/
public static function scopes($agentId)
{
$params = [
'agentId' => $agentId,
];
$result = Utils::post('microapp/visible_scopes', $params);
if (false !== $result) {
unset($result['errcode']);
unset($result['errmsg']);
return $result;
} else {
return false;
}
}
/**
* 设置微应用的可见范围
* @param [type] $agentId [description]
* @param [type] $params [description]
*/
public static function setScopes($agentId, $params)
{
$params['agentId'] = $agentId;
$result = Utils::post('microapp/set_visible_scopes', $params);
if (false !== $result) {
return true;
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
*
*/
class PcClient extends Dingtalk
{
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* Token 获取
*/
class Token extends Dingtalk
{
/**
* 获取ACCESS_TOKEN
* @return string|boolean
*/
public static function get()
{
$params = [
'appkey' => parent::$config['appkey'],
'appsecret' => parent::$config['appsecret'],
];
$result = Utils::get('gettoken', $params, false);
if (false !== $result) {
return $result['access_token'];
} else {
return false;
}
}
/**
* 获取 免登SsoToken
* @return string|boolean
*/
public static function sso()
{
$params = [
'corpid' => parent::$config['corpid'],
'corpsecret' => parent::$config['ssosecret'],
];
$result = Utils::get('sso/gettoken', $params, false);
if (false !== $result) {
return $result['access_token'];
} else {
return false;
}
}
/**
* 获取jsapi_ticket
* @return string|boolean
*/
public static function jsapi()
{
$result = Utils::get('get_jsapi_ticket');
if (false !== $result) {
return $result['ticket'];
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 用户管理
*/
class User extends Dingtalk
{
/**
* 获取企业员工人数
* @param integer $active 0:总数,1:已激活
* @return array|boolean
*/
public static function count($active = 0)
{
$params = [
'onlyActive' => $active,
];
$result = Utils::get('user/get_org_user_count', $params);
if (false !== $result) {
return $result['count'];
} else {
return false;
}
}
/**
* 获取用户详情
* @param string $userid 员工在企业内的UserID
* @return array|boolean
*/
public static function get($userid)
{
$params = [
'userid' => $userid,
];
$result = Utils::get('user/get', $params);
if (false !== $result) {
return $result;
} else {
return false;
}
}
/**
* 创建成员
* @param [type] $name [description]
* @param string $mobile [description]
* @param array $department [description]
* @return [type] [description]
*/
public static function create($name, $mobile, $department = [])
{
$params = [
'name' => $name,
'mobile' => $mobile,
'department' => $department,
];
$result = Utils::post('user/create', $params);
if (false !== $result) {
return $result['userid'];
} else {
return false;
}
}
/**
* 更新成员
* @param [type] $userid [description]
* @param [type] $name [description]
* @param array $data [description]
* @return [type] [description]
*/
public static function update($userid, $name, $data = [])
{
#Todo..
}
/**
* 删除成员
* @param [type] $userid [description]
* @return [type] [description]
*/
public static function delete($userid)
{
$params = [
'userid' => $userid,
];
$result = Utils::get('user/delete', $params);
if (false !== $result) {
return true;
} else {
return false;
}
}
/**
* 批量删除用户
* @param array $useridlist
* @return boolean
*/
public static function batchDelete($useridlist = [])
{
$params = [
'useridlist' => $useridlist,
];
$result = Utils::post('user/batchdelete', $params);
if (false !== $result) {
return true;
} else {
return false;
}
}
/**
* 获取管理员列表
* @return array|boolean
*/
public static function admin()
{
$result = Utils::get('user/get_admin');
if (false !== $result) {
return $result['adminList'];
} else {
return false;
}
}
/**
* 通过CODE换取用户身份
* @param string $code requestAuthCode接口中获取的CODE
* @return string|boolean
*/
public static function code($code)
{
$params = [
'code' => $code,
];
$result = Utils::get('user/getuserinfo', $params);
if (false !== $result) {
return $result['userid'];
} else {
return false;
}
}
}
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace src\Dingtalk;
use src\Dingtalk;
/**
* 基础工具类
*/
class Utils extends Dingtalk
{
/**
* GET 方式请求接口
* @param string $api
* @param array $params
* @param boolean $token
* @return array|boolean
*/
public static function get($api, $params = [], $token = true)
{
$url = Dingtalk::$baseUrl . $api;
//var_dump($url);
if ($token === true) {
$access_token = Dingtalk::config('access_token');
//var_dump($access_token);
if (empty($access_token)) {
$access_token = Token::get();
}
$params['access_token'] = $access_token;
}
$url .= '?' . http_build_query($params);
//var_dump($url);
$result = self::http($url, 'GET', $params, Dingtalk::$headers);
//var_dump($result);
if ($result !== false) {
$result = json_decode($result, true);
if ($result['errcode'] == 0) {
return $result;
} else {
Dingtalk::error($result['errmsg']);
return false;
}
} else {
return false;
}
}
/**
* POST 方式请求接口
* @param string $api
* @param array $params
* @return array|boolean
*/
public static function post($api, $params)
{
$access_token = Dingtalk::config('access_token');
if (empty($access_token)) {
$access_token = Token::get();
}
$url = Dingtalk::$baseUrl . $api . '?access_token=' . $access_token;
$result = self::http($url, 'POST', json_encode($params, JSON_UNESCAPED_UNICODE), Dingtalk::$headers);
if ($result !== false) {
$result = json_decode($result, true);
if ($result['errcode'] == 0) {
return $result;
} else {
Dingtalk::error($result['errmsg']);
return false;
}
} else {
return false;
}
}
/**
* curl操作函数
* @param string $url 请求地址
* @param string $method 提交方式
* @param array $postFields 提交内容
* @param array $header 请求头
* @return mixed 返回数据
*/
public static function http($url, $method = 'GET', $postFields = null, $headers = null)
{
$method = strtoupper($method);
if (!in_array($method, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'])) {
return false;
}
$opts = [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_URL => $url,
CURLOPT_FAILONERROR => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 30,
];
if ($method == 'POST' && !is_null($postFields)) {
$opts[CURLOPT_POSTFIELDS] = $postFields;
}
if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == 'https') {
$opts[CURLOPT_SSL_VERIFYPEER] = false;
$opts[CURLOPT_SSL_VERIFYHOST] = false;
}
if (!empty($headers) && is_array($headers)) {
$httpHeaders = [];
foreach ($headers as $key => $value) {
array_push($httpHeaders, $key . ':' . $value);
}
$opts[CURLOPT_HTTPHEADER] = $httpHeaders;
}
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
if ($err > 0) {
Dingtalk::error(curl_error($ch));
return false;
} else {
return $data;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment