Commit f3c6b1a867f8f878f50c932fd3e2f986d95ec2da
0 parents
初始化代码
Showing
6 changed files
with
612 additions
and
0 deletions
.gitignore
0 → 100644
README.md
0 → 100644
composer.json
0 → 100644
1 | +++ a/composer.json | |
1 | +{ | |
2 | + "name": "deepseath/hris", | |
3 | + "description": "iClick HRIS SYSTEM SDK for PHP", | |
4 | + "type": "think-extend", | |
5 | + "minimum-stability": "dev", | |
6 | + "require": { | |
7 | + "php": ">=8.1", | |
8 | + "ext-hash": "*", | |
9 | + "ext-openssl": "*", | |
10 | + "ext-json": "*", | |
11 | + "ext-mbstring": "*", | |
12 | + "ext-curl": "*", | |
13 | + "yurunsoft/yurun-http": "^4.3", | |
14 | + "symfony/cache": "^6.0" | |
15 | + }, | |
16 | + "license": "MIT", | |
17 | + "autoload": { | |
18 | + "psr-4": { | |
19 | + "deepseath\\hris\\": "src/" | |
20 | + } | |
21 | + }, | |
22 | + "authors": [ | |
23 | + { | |
24 | + "name": "Deepseath", | |
25 | + "email": "deepseath@foxmail.com" | |
26 | + } | |
27 | + ], | |
28 | + "extra": { | |
29 | + "think": { | |
30 | + "config":{ | |
31 | + "hris": "src/config.php" | |
32 | + } | |
33 | + } | |
34 | + } | |
35 | +} | ... | ... |
src/Api/User.php
0 → 100644
1 | +++ a/src/Api/User.php | |
1 | +<?php | |
2 | +/** | |
3 | + * User.php | |
4 | + * 用户接口 | |
5 | + * @author Deepseath | |
6 | + * @version $Id$ | |
7 | + */ | |
8 | +namespace deepseath\hris\Api; | |
9 | + | |
10 | +class User | |
11 | +{ | |
12 | + /** | |
13 | + * 基类服务对象 | |
14 | + * @var Object | |
15 | + */ | |
16 | + protected $service = null; | |
17 | + | |
18 | + public function __construct(\deepseath\hris\Hris $service) | |
19 | + { | |
20 | + $this->service = $service; | |
21 | + } | |
22 | + | |
23 | + /** | |
24 | + * 员工基础信息 | |
25 | + * @desc 查询 HRIS 中的员工基础信息 | |
26 | + * @param array $params | |
27 | + * @return array | |
28 | + */ | |
29 | + public function baseList(array $params) : array | |
30 | + { | |
31 | + $params = array_merge([ | |
32 | + 'nameZH' => '', | |
33 | + 'nameEN' => '', | |
34 | + 'email' => '' | |
35 | + ], $params); | |
36 | + | |
37 | + return $this->service->apiRequest('get', 'employeeList', $params); | |
38 | + } | |
39 | + | |
40 | + /** | |
41 | + * 新增员工 | |
42 | + * @desc 向HRIS中插入新用户 | |
43 | + * @param array $params | |
44 | + * @return array | |
45 | + */ | |
46 | + public function add(array $params) : array | |
47 | + { | |
48 | + $this->_fieldCheck($params); | |
49 | + if (!isset($params['orgUnitZH'])) { | |
50 | + $params['orgUnitZH'] = $this->service->config['orgUnitZH']; | |
51 | + } | |
52 | + if (!isset($params['orgUnitEN'])) { | |
53 | + $params['orgUnitEN'] = $this->service->config['orgUnitEN']; | |
54 | + } | |
55 | + return $this->service->apiRequest('post', 'employeeAdd', $params); | |
56 | + } | |
57 | + | |
58 | + /** | |
59 | + * 更新员工 | |
60 | + * @desc 用户修改 | |
61 | + * @param array $params | |
62 | + * @return array | |
63 | + */ | |
64 | + public function update(array $params) : array | |
65 | + { | |
66 | + $this->_fieldCheck($params); | |
67 | + if (!isset($params['employeeId']) || empty($params['employeeId'])) { | |
68 | + // 如果未提供员工 HRIS ID,则尝试查询 | |
69 | + $result = $this->baseList([ | |
70 | + 'nameZH' => $params['nameZH'], | |
71 | + 'nameEN' => $params['nameEN'], | |
72 | + 'email' => $params['email'] | |
73 | + ]); | |
74 | + if (empty($result)) { | |
75 | + throw new \Exception('Hris SDK: 更新员工失败,无法查找员工 ID', 91002); | |
76 | + } | |
77 | + if (count($result) > 1) { | |
78 | + throw new \Exception('Hris SDK: 更新员工失败,无法确定员工 ID', 91003); | |
79 | + } | |
80 | + $params['employeeId'] = $result[0]['employeeId']; | |
81 | + } | |
82 | + | |
83 | + return $this->service->apiRequest('post', 'employeeUpdate', $params); | |
84 | + } | |
85 | + | |
86 | + /** | |
87 | + * 员工数据参数检查 | |
88 | + * @param array $params | |
89 | + * @throws \Exception | |
90 | + * @return bool | |
91 | + */ | |
92 | + private function _fieldCheck(array $params) : bool | |
93 | + { | |
94 | + // 必须提供的参数 | |
95 | + $requires = [ | |
96 | + 'nameZH', // *员工姓名(中文) | |
97 | + 'nameEN', // *员工姓名(英文) | |
98 | + 'entityId', // *法务实体ID(畅移上海157 . 畅移西安 158) | |
99 | + //'orgUnitZH', // *一级部门(中文) | |
100 | + //'orgUnitEN', // *一级部门(英文) | |
101 | + 'locationId', // *工作地点ID 上海65 西安 116 | |
102 | + 'employeeType', // *员工类型(全职为:Full Time Employee、 需要转义,请看转义说明) | |
103 | + 'positionZH', // *职位名称(中文) | |
104 | + 'positionEN', // *职位名称(英文) | |
105 | + 'gradeId', // *职级ID (需要转义,如不填核对grade名称) | |
106 | + 'bizLeader', // *业务汇报线 | |
107 | + 'pmLeader', // *直接汇报线 | |
108 | + 'certificateType', // *证件类型 | |
109 | + 'certificateNumber', // *证件号 | |
110 | + 'highestEducationId', // *最高学历 (需要转义,如不填需要核对highestEducation名称) | |
111 | + 'major', // *主修专业 | |
112 | + 'schoolStartDate', // *学校开始学习日期 | |
113 | + 'schoolEndDate', // *结束学习日期 | |
114 | + 'mobile', // *手机号 | |
115 | + 'email', // *公司邮箱 | |
116 | + 'startWorkDate', // *开始工作日期 | |
117 | + 'contractStartDate', // *合同开始时间 | |
118 | + 'probationEndDate', // *试用期结束时间 | |
119 | + ]; | |
120 | + foreach ($requires as $_key) { | |
121 | + if (!isset($params[$_key]) || !is_scalar($params[$_key])) { | |
122 | + throw new \Exception('Add user lose params "'.$_key.'"', 91001); | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + return true; | |
127 | + } | |
128 | + | |
129 | + /** | |
130 | + * 在职状态描述映射关系 | |
131 | + * @var array | |
132 | + */ | |
133 | + const MAP_STATUS = [ | |
134 | + '1' => '在职', | |
135 | + '3' => '离职' | |
136 | + ]; | |
137 | + | |
138 | + /** | |
139 | + * 法务实体映射关系 | |
140 | + * @var array | |
141 | + */ | |
142 | + const MAP_ENTOTY = [ | |
143 | + '157' => '畅移(上海)信息科技有限公司', | |
144 | + '158' => '西安畅展信息科技有限公司', | |
145 | + ]; | |
146 | + | |
147 | + /** | |
148 | + * 工作地映射关系 | |
149 | + * @var array | |
150 | + */ | |
151 | + const MAP_LOCATION = [ | |
152 | + '65' => '上海', | |
153 | + '116' => '西安', | |
154 | + ]; | |
155 | + | |
156 | + /** | |
157 | + * 员工类型映射关系 | |
158 | + * @var array | |
159 | + */ | |
160 | + const MAP_TYPE = [ | |
161 | + '0' => '请选择', | |
162 | + '1' => 'Full Time Employee',// 全职 | |
163 | + '2' => 'Intern',// 实习生 | |
164 | + '3' => 'Consultant',// 顾问 | |
165 | + '4' => 'Others on Payroll',// 编外 | |
166 | + '5' => 'Temp',// 临时工 | |
167 | + '6' => 'Part Time Employee',// 兼职 | |
168 | + '7' => '其他', | |
169 | + ]; | |
170 | + | |
171 | + /** | |
172 | + * 职级映射关系 | |
173 | + * @var array | |
174 | + */ | |
175 | + const MAP_GRADE_ID = [ | |
176 | + '179' => ['E2', 'Sr. Executive'], | |
177 | + '181' => ['E1', 'Executive'], | |
178 | + '183' => ['M4', 'GM/VP'], | |
179 | + '187' => ['M3', 'Director'], | |
180 | + '189' => ['IC6', 'Director'], | |
181 | + '191' => ['M2', 'Sr. Manager'], | |
182 | + '193' => ['IC5', 'Sr. Manager'], | |
183 | + '195' => ['M1', 'Manager'], | |
184 | + '197' => ['IC4', 'Manager'], | |
185 | + '199' => ['IC3', 'Associate Manager'], | |
186 | + '201' => ['IC2', 'Sr. Specialist'], | |
187 | + '203' => ['IC1', 'Specialist'], | |
188 | + '205' => ['S2', 'Coordinator'], | |
189 | + '207' => ['S1', 'Assistant'], | |
190 | + '209' => ['E1', 'COO'], | |
191 | + '213' => ['IC7', 'GM/VP'], | |
192 | + '315' => ['E1', 'CFO'], | |
193 | + '316' => ['E3', 'Chairman'], | |
194 | + '319' => ['M5', 'GM/VP'] | |
195 | + ]; | |
196 | + | |
197 | + /** | |
198 | + * 身份证件类型映射关系 | |
199 | + * @var array | |
200 | + */ | |
201 | + const MP_CERTIFICATE_TYPE = [ | |
202 | + '0' => '请选择', | |
203 | + '1' => '身份证', | |
204 | + '2' => '护照', | |
205 | + '3' => '台胞证', | |
206 | + '4' => '身份证(香港)', | |
207 | + '5' => '身份证(新加坡)', | |
208 | + '6' => '港澳通行证', | |
209 | + ]; | |
210 | + | |
211 | + /** | |
212 | + * 婚姻状态映射关系 | |
213 | + * @var array | |
214 | + */ | |
215 | + const MP_MARITAL_STATUS = [ | |
216 | + '0' => '请选择', | |
217 | + '1' => '未婚', | |
218 | + '2' => '已婚', | |
219 | + ]; | |
220 | + | |
221 | + /** | |
222 | + * 最高学历映射关系 | |
223 | + * @var array | |
224 | + */ | |
225 | + const MAP_HIGHEST_EDUCATION = [ | |
226 | + '0' => ['请选择', ''], | |
227 | + '95' => ['PhD', '博士'], | |
228 | + '97' => ['Master', '硕士'], | |
229 | + '99' => ['Bachelor', '本科'], | |
230 | + '103' => ['Senior High', '高中'], | |
231 | + '105' => ['Junior High', '初中'], | |
232 | + '107' => ['Diploma', '大学毕业'], | |
233 | + '109' => ['Associate Degree', ''], | |
234 | + '111' => ['Advanced Diploma', ''], | |
235 | + '113' => ['Higher Diploma', ''], | |
236 | + '115' => ['Juris Doctor', ''], | |
237 | + '117' => ['Certificate', ''], | |
238 | + '119' => ['Secondary ', ''], | |
239 | + '121' => ['F.5', ''], | |
240 | + ]; | |
241 | + | |
242 | + /** | |
243 | + * 民族映射关系 | |
244 | + * @var array | |
245 | + */ | |
246 | + const NATION = [ | |
247 | + '0' => '请选择', | |
248 | + '1' => '汉族', | |
249 | + '10' => '瑶族', | |
250 | + '11' => '锡伯族', | |
251 | + '12' => '苗族', | |
252 | + '2' => '蒙古族', | |
253 | + '3' => '土家族', | |
254 | + '4' => '满族', | |
255 | + '5' => '壮族', | |
256 | + '6' => '仫佬族', | |
257 | + '7' => '回族', | |
258 | + '8' => '英国', | |
259 | + '9' => '维吾尔族', | |
260 | + ]; | |
261 | + | |
262 | + /** | |
263 | + * 国籍映射关系 | |
264 | + * @var array | |
265 | + */ | |
266 | + const MAP_NATION_NALITY = [ | |
267 | + '0' => '请选择', | |
268 | + '1' => '中国', | |
269 | + '2' => '美国', | |
270 | + '3' => '新加坡', | |
271 | + '5' => '加拿大', | |
272 | + '7' => '英国', | |
273 | + '9' => '泰国', | |
274 | + '11' => '日本', | |
275 | + '13' => '韩国', | |
276 | + '15' => '法国', | |
277 | + '17' => '香港', | |
278 | + '19' => '台湾', | |
279 | + '21' => '澳大利亚', | |
280 | + '23' => '菲律宾', | |
281 | + '24' => '德国', | |
282 | + '25' => '意大利', | |
283 | + '26' => '马来西亚', | |
284 | + '27' => '新西兰', | |
285 | + '28' => '西班牙', | |
286 | + '29' => '其他', | |
287 | + ]; | |
288 | + | |
289 | + /** | |
290 | + * 角色映射关系 | |
291 | + * @var array | |
292 | + */ | |
293 | + const MAP_JOB_ROLE = [ | |
294 | + '0' => '请选择', | |
295 | + '10' => 'Engineering', | |
296 | + '12' => 'Finance', | |
297 | + '14' => 'Human Resources', | |
298 | + '16' => 'Management', | |
299 | + '17' => 'Marketing', | |
300 | + '18' => 'Media', | |
301 | + '19' => 'Optimization', | |
302 | + '20' => 'Product Management', | |
303 | + '21' => 'Project Management', | |
304 | + '23' => 'Sales', | |
305 | + '28' => 'Creative Design', | |
306 | + '29' => 'Legal', | |
307 | + '3' => 'Account Management', | |
308 | + '30' => 'Product Marketing', | |
309 | + '31' => 'Product Trainer', | |
310 | + '32' => 'Sales Operations', | |
311 | + '37' => 'Solution Planning', | |
312 | + '4' => 'Accounting', | |
313 | + '40' => 'Biz Planning & Operations', | |
314 | + '41' => 'Solution Development', | |
315 | + '43' => 'Product Operations', | |
316 | + '44' => 'User Experience Design', | |
317 | + '45' => 'Public Relations', | |
318 | + '46' => 'Data Analysis', | |
319 | + '47' => 'Kol Operations', | |
320 | + '48' => 'Content Operations', | |
321 | + '49' => 'Creative', | |
322 | + '5' => 'Ad. Operations', | |
323 | + '6' => 'Administration', | |
324 | + '8' => 'Biz Development', | |
325 | + '9' => 'Biz Intelligence', | |
326 | + ]; | |
327 | + | |
328 | + /** | |
329 | + * 性别映射关系 | |
330 | + * @var array | |
331 | + */ | |
332 | + const MAP_GENDER = [ | |
333 | + '0' => '未填写', | |
334 | + '1' => '先生', | |
335 | + '2' => '女士' | |
336 | + ]; | |
337 | +} | ... | ... |
src/Hris.php
0 → 100644
1 | +++ a/src/Hris.php | |
1 | +<?php | |
2 | +/** | |
3 | + * Hris.php | |
4 | + * HRIS 系统接口服务 | |
5 | + * @author Deepseath | |
6 | + * @version $Id$ | |
7 | + */ | |
8 | +namespace deepseath\hris; | |
9 | + | |
10 | +use Symfony\Component\Cache\Adapter\FilesystemAdapter; | |
11 | + | |
12 | +class Hris | |
13 | +{ | |
14 | + /** | |
15 | + * 缓存池 | |
16 | + * @var object | |
17 | + */ | |
18 | + protected $_cache = null; | |
19 | + | |
20 | + /** | |
21 | + * HRIS 系统原始 token | |
22 | + * @var string | |
23 | + */ | |
24 | + protected $_originToken = ''; | |
25 | + | |
26 | + /** | |
27 | + * token 过期时间,单位:秒 | |
28 | + * @var int | |
29 | + */ | |
30 | + protected $_tokenExpire = 7200; | |
31 | + | |
32 | + /** | |
33 | + * HRIS 接口服务根 URL | |
34 | + * @var string | |
35 | + */ | |
36 | + protected $_baseUrl = ''; | |
37 | + | |
38 | + /** | |
39 | + * 库版本号 | |
40 | + * @var string | |
41 | + */ | |
42 | + protected $_libVersion = '1.0'; | |
43 | + | |
44 | + /** | |
45 | + * HTTP 请求对象 | |
46 | + * @var object | |
47 | + */ | |
48 | + protected $_http = null; | |
49 | + | |
50 | + /** | |
51 | + * 调试模式 | |
52 | + * @var boolean | |
53 | + */ | |
54 | + protected $_debug = false; | |
55 | + | |
56 | + /** | |
57 | + * 配置信息 | |
58 | + * @var array | |
59 | + */ | |
60 | + public $config = []; | |
61 | + | |
62 | + /** | |
63 | + * 单点实例 | |
64 | + * @param array $options | |
65 | + * @return \deepseath\hris\Hris | |
66 | + */ | |
67 | + public static function &instance(array $options) | |
68 | + { | |
69 | + static $instance = null; | |
70 | + if (empty($instance)) { | |
71 | + $instance = new self($options); | |
72 | + } | |
73 | + return $instance; | |
74 | + } | |
75 | + | |
76 | + private function __construct(array $options = []) | |
77 | + { | |
78 | + $this->_cache = new FilesystemAdapter('hris', 86400, 'deepseath' . $this->_libVersion); | |
79 | + if (function_exists('config')) { | |
80 | + $config = config('hris'); | |
81 | + if (empty($options)) { | |
82 | + $options = $config; | |
83 | + } else { | |
84 | + $options = array_merge($config, $options); | |
85 | + } | |
86 | + unset($config); | |
87 | + } | |
88 | + $this->config = $options; | |
89 | + if (empty($options['originToken'])) { | |
90 | + throw new \Exception('原始 TOKEN 未定义', 9001); | |
91 | + } | |
92 | + if (!empty($options['baseUrl'])) { | |
93 | + $this->_baseUrl = $options['baseUrl']; | |
94 | + } | |
95 | + if (isset($options['debug'])) { | |
96 | + $this->_debug = $options['debug']; | |
97 | + } | |
98 | + | |
99 | + $this->_origin= $options['originToken']; | |
100 | + $this->_http = \Yurun\Util\HttpRequest::newSession(); | |
101 | + } | |
102 | + | |
103 | + /** | |
104 | + * 获取 token 信息 | |
105 | + * @param $force bool 是否强制获取更新 token | |
106 | + * @return array | |
107 | + * <pre> | |
108 | + * + access_token String Y 令牌 | |
109 | + * + token_type String Y 令牌类型 | |
110 | + * + expires_in String Y token有效时间,时间单位秒 | |
111 | + * + scope String Y 授权作用域 | |
112 | + * </pre> | |
113 | + */ | |
114 | + public function getTokenInfo(bool $force = false) : array | |
115 | + { | |
116 | + $token = $this->_cache->getItem('token'); | |
117 | + if ($token->isHit() || $force !== false) { | |
118 | + return $token->get(); | |
119 | + } | |
120 | + | |
121 | + // 初始化 token 信息 | |
122 | + $newTokenInfo = []; | |
123 | + // 构造 token 获取接口地址 | |
124 | + $url = $this->_baseUrl . 'tokenFlush'; | |
125 | + $params = [ | |
126 | + 'token' => $this->_originToken | |
127 | + ]; | |
128 | + // 请求接口 | |
129 | + $this->_http->headers([ | |
130 | + 'Authorization' => $this->_originToken, | |
131 | + 'Content-Type' => 'application/json;charset=utf-8' | |
132 | + ]); | |
133 | + $response = $this->_http->post($url, $params, 'json'); | |
134 | + $newTokenInfo = $response->json(true); | |
135 | + | |
136 | + if (empty($newTokenInfo) || !isset($newTokenInfo['code'])) { | |
137 | + throw new \Exception('request hris token error', 9001); | |
138 | + } | |
139 | + if ($newTokenInfo['code'] != 200) { | |
140 | + throw new \Exception('HRIS TOKEN GET ERROR:' . $newTokenInfo['msg'] . ':' . $newTokenInfo['code']); | |
141 | + } | |
142 | + | |
143 | + $newTokenInfo['_datetime'] = date('Y-m-d H:i:s'); | |
144 | + // 将本次获取的 token 值写入到缓存 | |
145 | + $token->set($newTokenInfo); | |
146 | + if (isset($newTokenInfo['expires_in'])) { | |
147 | + $expire = $newTokenInfo['expires_in']; | |
148 | + } else { | |
149 | + $expire = $this->_tokenExpire; | |
150 | + } | |
151 | + $token->expiresAfter($expire - 60 * 5); | |
152 | + $this->_cache->save($token); | |
153 | + | |
154 | + return $newTokenInfo; | |
155 | + } | |
156 | + | |
157 | + /** | |
158 | + * 接口 API POST | |
159 | + * @param string $path 接口基于版本目录的路径 | |
160 | + * @param array $data 请求的数据 | |
161 | + * @param bool $flushToken 是否强制刷新 token | |
162 | + * @throws \Exception | |
163 | + * @return array|string | |
164 | + */ | |
165 | + public function apiRequest($method, $path, $data, $flushToken = false) | |
166 | + { | |
167 | + static $tryCount; | |
168 | + if ($tryCount === null || $flushToken === false) { | |
169 | + $tryCount = 1; | |
170 | + } else { | |
171 | + $tryCount++; | |
172 | + } | |
173 | + if ($tryCount > 3) { | |
174 | + throw new \Exception('hris SDK Error: Get token error, retry maximum number.'); | |
175 | + } | |
176 | + $this->_http->headers([ | |
177 | + 'Content-Type' => 'application/json;charset=utf-8', | |
178 | + 'Authorization' => $this->getTokenInfo($flushToken)['data']['token'] | |
179 | + ]); | |
180 | + $url = $this->_baseUrl . $path; | |
181 | + $method = strtolower($method); | |
182 | + if ($method == 'post') { | |
183 | + $dataString = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); | |
184 | + $response = $this->_http->post($url, $dataString, 'json'); | |
185 | + } elseif ($method == 'get') { | |
186 | + $response = $this->_http->get($url, $data); | |
187 | + } | |
188 | + $result = $response->json(true); | |
189 | + if (!isset($result['code']) || $result['code'] != 0) { | |
190 | + if ($result['code'] == 3001) { | |
191 | + // token 无效,尝试重新刷新 token | |
192 | + return $this->apiRequest($method, $path, $data, true); | |
193 | + } | |
194 | + throw new \Exception('hris SDK Error: ' . $result['msg'] . ':' . $result['code'], $result['code']); | |
195 | + } | |
196 | + | |
197 | + return isset($result['data']) ? $result['data'] : []; | |
198 | + } | |
199 | +} | ... | ... |
src/config.php
0 → 100644
1 | +++ a/src/config.php | |
1 | +<?php | |
2 | +/** | |
3 | + * config.php | |
4 | + * HRIS 接口对接配置文件 | |
5 | + * @author Deepseath | |
6 | + * @version $Id$ | |
7 | + */ | |
8 | +use think\facade\Env; | |
9 | + | |
10 | +return [ | |
11 | + // 分配到的原始 TOKEN | |
12 | + 'appKey' => Env::get('hris.originToken', ''), | |
13 | + // HRIS 接口 URL 前缀 | |
14 | + 'baseUrl' => Env::get('hris.baseUrl', ''), | |
15 | + // 调试模式 | |
16 | + 'debug' => Env::get('hris.debug', false), | |
17 | + // 一级部门名称(中文) | |
18 | + 'orgUnitZH' => Env::get('hris.orgUnitZH', ''), | |
19 | + // 一级部门名称(英文) | |
20 | + 'orgUnitEN' => Env::get('hris.orgUnitEN', '') | |
21 | +]; | ... | ... |