Commit ee676cc5 authored by qihongchuan's avatar qihongchuan

Repository snapshot migration

Source tree: ee511d04de4e4e168938557c6188e62787aeaf65
parents
.env
.git
.svn
.idea
.vscode
*.log
.DS_Store
vendor
runtime
safefile
nbproject
composer.lock
# HRIS SDK for PHP
{
"name": "deepseath/hris",
"description": "iClick HRIS SYSTEM SDK for PHP",
"type": "think-extend",
"minimum-stability": "dev",
"require": {
"php": ">=8.1",
"ext-hash": "*",
"ext-openssl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-curl": "*",
"yurunsoft/yurun-http": "^4.3",
"symfony/cache": "^6.0"
},
"license": "MIT",
"autoload": {
"psr-4": {
"deepseath\\hris\\": "src/"
}
},
"authors": [
{
"name": "Deepseath",
"email": "deepseath@foxmail.com"
}
],
"extra": {
"think": {
"config":{
"hris": "src/config.php"
}
}
}
}
<?php
/**
* Department.php
* 部门接口
* @author Deepseath
* @version $Id$
*/
namespace deepseath\hris\Api;
class Department
{
/**
* 基类服务对象
* @var \deepseath\hris\Hris
*/
protected $service = null;
/**
* 部门删除状态:正常(未删除)
* @var integer
*/
const DELETE_NO = 1;
/**
* 部门删除状态:已删除
* @var integer
*/
const DELETE_YES = 2;
/**
* 畅移本地顶级部门(智慧零售部)的 id
* md5('2022-04-27 17:00:01') —— 该值没具体意义主要就是一个在hris识别“智慧零售事业部”的标识<br>
* 该值的来源是,首次导给 hris 全量部门数据时,以当时时间md5做顶级部门标识,方便 hris 导入处理
* @var string
*/
const CY_TOP_ID = '6e691aaf7d16f7b5199cce086e781368';
public function __construct(\deepseath\hris\Hris $service)
{
$this->service = $service;
}
/**
* 获取部门信息
* @desc 查询HRIS中的部门基础信息
* @param array $params
* <pre>
* orgUnitZH String 一级部门(中文)
* buFunctionZH String 二级部门(中文)
* departmentZH String 三级部门(中文)
* teamZH String 四级部门(中文)
* </pre>
* @return array
* <strong>map</strong>
* <pre>
* orgUnitId string 一级部门id
* orgUnitZH string 一级部门名称中文
* orgUnitEN string 一级部门名称英文
* buFunctionId string 二级部门id
* buFunctionZH string 二级部门名称中文
* buFunctionEN string 二级部门名称英文
* departmentId string 三级部门id
* departmentZH string 三级部门名称中文
* departmentEN string 三级部门名称英文
* teamId string 四级部门id
* teamZH string 四级部门名称中文
* teamEN string 四级部门名称英文
* </pre>
*/
public function branchList(array $params) : array
{
$params = array_merge([
'orgUnitZH' => '',
'buFunctionZH' => '',
'departmentZH' => '',
'teamZH' => ''
], $params);
$result = $this->service->apiRequest('get', 'branchList', $params);
return isset($result['branchList']) ? $result['branchList'] : (isset($result['data']) ? $result['data'] : []);
}
/**
* 新增OR更新部门
* @desc 新增OR更新HRIS中的部门
* @param array $params
* <pre>
* branchNo String 畅移部门ID
* parentBranchNo String 畅移上级部门ID
* nameZH String 部门名称(中文)
* nameEN String 部门名称(英文)
* </pre>
* @return array
* <pre>
* affectRow string 受影响的行数
* branchId string hris中的部门ID
* </pre>
*/
public function branchAddOrUpdate(array $params) : array
{
$params = array_merge([
'branchNo' => '',
'parentBranchNo' => '',
'nameZH' => '',
'nameEN' => '',
'deleted' => self::DELETE_NO
], $params);
if ($params['parentBranchNo'] === '') {
// 如果父级部门 id 不存在,则认为更新的是畅移的一级部门(在 hris 是属于“智慧零售事业部”下的二级部门)
// 使用虚拟的“智慧零售部” id 做为父级 id
$params['parentBranchNo'] = self::CY_TOP_ID;
}
$result = $this->service->apiRequest('post', 'branchAddOrUpdate', $params);
return $result;
}
/**
* 删除部门
* @desc 删除HRIS中的部门
* @param array $params
* <pre>
* branchNo String 畅移部门ID
* parentBranchNo String 畅移上级部门ID
* nameZH String 部门名称(中文)
* nameEN String 部门名称(英文)
* </pre>
* @return array
* <pre>
* affectRow string 受影响的行数
* branchId string hris中的部门ID
* </pre>
*/
public function branchDelete(array $params) : array
{
$params = array_merge([
'branchNo' => '',
'parentBranchNo' => '',
'nameZH' => '',
'nameEN' => '',
'deleted' => self::DELETE_YES
], $params);
return $this->branchAddOrUpdate($params);
}
}
This diff is collapsed.
<?php
/**
* Hris.php
* HRIS 系统接口服务
* @author Deepseath
* @version $Id$
*/
namespace deepseath\hris;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
class Hris
{
/**
* 缓存池
* @var object
*/
protected $_cache = null;
/**
* HRIS 系统原始 token
* @var string
*/
protected $_originToken = '';
/**
* token 过期时间,单位:秒
* @var int
*/
protected $_tokenExpire = 7200;
/**
* HRIS 接口服务根 URL
* @var string
*/
protected $_baseUrl = '';
/**
* 库版本号
* @var string
*/
protected $_libVersion = '1.0';
/**
* HTTP 请求对象
* @var object
*/
protected $_http = null;
/**
* 调试模式
* @var boolean
*/
protected $_debug = false;
/**
* 配置信息
* @var array
*/
public $config = [];
/**
* 单点实例
* @param array $options
* @return \deepseath\hris\Hris
*/
public static function &instance(array $options)
{
static $instance = null;
if (empty($instance)) {
$instance = new self($options);
}
return $instance;
}
private function __construct(array $options = [])
{
$this->_cache = new FilesystemAdapter('hris', 86400, null);
if (function_exists('config')) {
$config = config('hris');
if (empty($options)) {
$options = $config;
} else {
$options = array_merge($config, $options);
}
unset($config);
}
$this->config = $options;
if (empty($options['originToken'])) {
throw new \Exception('原始 TOKEN 未定义', 9001);
}
if (!empty($options['baseUrl'])) {
$this->_baseUrl = $options['baseUrl'];
}
if (isset($options['debug'])) {
$this->_debug = $options['debug'];
}
$this->_originToken= $options['originToken'];
$this->_http = \Yurun\Util\HttpRequest::newSession();
}
/**
* 获取 token 信息
* @param $force bool 是否强制获取更新 token
* @return array
* <pre>
* + access_token String Y 令牌
* + token_type String Y 令牌类型
* + expires_in String Y token有效时间,时间单位秒
* + scope String Y 授权作用域
* </pre>
*/
public function getTokenInfo(bool $force = false) : array
{
$token = $this->_cache->getItem('token');
if ($token->isHit() && $force === false) {
return $token->get();
}
// 初始化 token 信息
$newTokenInfo = [];
// 构造 token 获取接口地址
$url = $this->_baseUrl . 'tokenFlush';
$params = [
'token' => $this->_originToken
];
// 请求接口
$headers = [
'Authorization' => $this->_originToken,
'Content-Type' => 'application/json;charset=utf-8'
];
$this->_http->headers($headers);
$this->log($url, 'Get Token');
$this->log($params, 'Request');
$this->log($headers, 'Header');
$response = $this->_http->post($url, $params, 'json');
$newTokenInfo = $response->json(true);
$this->log($newTokenInfo, 'Result');
if (empty($newTokenInfo) || !isset($newTokenInfo['code'])) {
throw new \Exception('request hris token error' . var_export($newTokenInfo, true) . '|' . $url . var_export($params, true), 9001);
}
if ($newTokenInfo['code'] != 200) {
throw new \Exception('HRIS TOKEN GET ERROR:' . $newTokenInfo['msg'] . ':' . $newTokenInfo['code']);
}
$newTokenInfo['_datetime'] = date('Y-m-d H:i:s');
// 将本次获取的 token 值写入到缓存
$token->set($newTokenInfo);
if (isset($newTokenInfo['expires_in'])) {
$expire = $newTokenInfo['expires_in'];
} else {
$expire = $this->_tokenExpire;
}
$token->expiresAfter($expire - 10);
$this->log($token->get(), 'Save Token');
$this->_cache->save($token);
return $newTokenInfo;
}
/**
* 接口 API POST
* @param string $path 接口基于版本目录的路径
* @param array $data 请求的数据
* @param bool $flushToken 是否强制刷新 token
* @throws \Exception
* @return array|string
*/
public function apiRequest($method, $path, $data, $flushToken = false)
{
static $tryCount;
if ($tryCount === null || $flushToken === false) {
$tryCount = 1;
} else {
$tryCount++;
}
if ($tryCount > 3) {
throw new \Exception('hris SDK Error: Get token error, retry maximum number.');
}
$headers = [
'Content-Type' => 'application/json;charset=utf-8',
'Authorization' => $this->getTokenInfo($flushToken)['data']['token']
];
$this->_http->headers($headers);
$url = $this->_baseUrl . $path;
$method = strtolower($method);
$this->log($url, 'Url');
$this->log($method, 'Method');
$this->log($headers, 'Header');
$this->log($data, 'Request');
if ($method == 'post') {
$dataString = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$response = $this->_http->post($url, $dataString, 'json');
} elseif ($method == 'get') {
$response = $this->_http->get($url, $data);
}
$body = $response->body();
$this->log($response->getStatusCode(), 'StatusCode');
$this->log($body, 'Body');
$result = $response->json(true);
$this->log($result, 'Result');
if (!isset($result['code']) || $result['code'] != 200) {
if (isset($result['code']) && ($result['code'] == '3001')) {
// token 无效,尝试重新刷新 token
$this->log($tryCount, 'Try count');
return $this->apiRequest($method, $path, $data, true);
}
if (isset($result['msg']) && isset($result['code'])) {
throw new \Exception(json_encode([
'url' => $url,
'title' => 'hris SDK request error',
'msg' => $result['msg'],
'code' => $result['code'],
'request' => $data,
'result' => $result,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $result['code']);
} else {
throw new \Exception(json_encode([
'url' => $url,
'title' => 'hris SDK response error',
'msg' => $result,
'result' => $result,
'response' => $body,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 90001);
}
}
return isset($result['data']) ? $result['data'] : [];
}
/**
* 打印调试日志
* @param mixed $info
*/
public function log($info, $title = '', $level = 'hris')
{
if ($this->_debug) {
$info = is_scalar($info) ? $info : json_encode($info, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$title = $title ? "[{$title}]" : '';
\think\facade\Log::write($title . $info, 'hris');
}
}
}
<?php
/**
* config.php
* HRIS 接口对接配置文件
* @author Deepseath
* @version $Id$
*/
use think\facade\Env;
return [
// 分配到的原始 TOKEN
'appKey' => Env::get('hris.originToken', ''),
// HRIS 接口 URL 前缀
'baseUrl' => Env::get('hris.baseUrl', ''),
// 调试模式
'debug' => Env::get('hris.debug', false),
// 一级部门名称(中文)
'orgUnitZH' => Env::get('hris.orgUnitZH', ''),
// 一级部门名称(英文)
'orgUnitEN' => Env::get('hris.orgUnitEN', '')
];
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