MayCurServerService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\Redis;
  5. class MayCurServerService extends Service
  6. {
  7. protected $param = "";
  8. protected $domain_url = "";
  9. protected $head = null;
  10. public function __construct()
  11. {
  12. $this->param = config("maycur");
  13. $this->domain_url = $this->param['dd_url'];
  14. }
  15. public function getToken(){
  16. //每刻所有配置
  17. $url_array = $this->param;
  18. //redis 存储的token
  19. $key = $this->domain_url . $url_array['login_redis_topic'];
  20. $token = Redis::get($key);
  21. if(! empty($token)) {
  22. $this->head = json_decode($token,true);
  23. return [true, ''];
  24. }
  25. //获取token的参数
  26. $appcode = env('maycur_appcode');
  27. $appsercet = env('maycur_appsercet');
  28. if(empty($appcode) || empty($appsercet)) return [false, '每刻鉴权认证参数不能为空'];
  29. //组织获取参数
  30. $url = $this->domain_url . $url_array['login'];
  31. $time = microtime(true);
  32. $timeStamp = intval($time * 1000);
  33. //生成secret
  34. $secret = $this->getSecret($appcode, $appsercet, $timeStamp);
  35. $post = [
  36. "secret" => $secret,
  37. "appCode" => $appcode,
  38. "timestamp" => $timeStamp
  39. ];
  40. $header = ['Content-Type:application/json'];
  41. list($status, $result) = $this->post_helper($url,$post, $header);
  42. if(! $status) return [$status, $result];
  43. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message'] ?? '鉴权失败,请联系开发者'];
  44. $token_array = [
  45. 'tokenId:' . $result['data']['tokenId'],
  46. 'entCode:' . $result['data']['entCode'],
  47. ];
  48. $this->head = $token_array;
  49. Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));
  50. return [true, ''];
  51. }
  52. private function getSecret($appCode, $appSecret, $timeStamp) {
  53. // 拼接字符串并计算 SHA-256 哈希值
  54. $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;
  55. $hashedString = hash('sha256', $stringToHash);
  56. return $hashedString;
  57. }
  58. public function reimburse($data){
  59. //校验
  60. list($status, $msg) = $this->reimburseRule($data);
  61. if(! $status) return [false, $msg];
  62. //获取token
  63. list($status, $token) = $this->getToken();
  64. if(! $status) return [false, $token];
  65. $header = $this->head;
  66. //每刻所有配置
  67. $url_array = $this->param;
  68. //组织获取参数
  69. $url = $this->domain_url . $url_array['reimburse'];
  70. $header = array_merge(['Content-Type:application/json'], $header);
  71. list($status, $result) = $this->post_helper($url,$msg, $header);
  72. if(! $status) return [$status, $result];
  73. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  74. //组织返回数据
  75. if(empty($result['data'])) return [true, ['list' => null, 'hasNextPage' => false]];
  76. //获取报销单详情填充
  77. list($status, $return) = $this->reimburseDetailGet($result['data']);
  78. if(! $status) return [false, $return];
  79. if(! empty($return['list'])){
  80. list($status,$msg) = $this->tradingPartner($return);
  81. if(! $status) return [false, $msg];
  82. foreach ($return['list'] as $key => $value){
  83. $return['list'][$key]['tradingPartnerBizCodeType'] = $msg[$value['tradingPartnerBizCode']] ?? [];
  84. }
  85. }
  86. return [true, $return];
  87. }
  88. public function reimburseRule($data){
  89. if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空'];
  90. $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00');
  91. $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59');
  92. if(empty($settledAtStart)) return [false, '单据的支付时间格式错误'];
  93. if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误'];
  94. $formCodes = [];
  95. if(! empty($data['formCodes'])) $formCodes = array_filter($data['formCodes']);
  96. $formStatus = "";
  97. if(! empty($data['formStatus'])) $formStatus = $data['formStatus'];
  98. $legalEntityBizCodes = [];
  99. if(! empty($data['legalEntityBizCodes'])) $legalEntityBizCodes = $data['legalEntityBizCodes'];
  100. $pageNo = 1;
  101. $pageSize = 10;
  102. if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
  103. if(! empty($data['pageSize'])) {
  104. if($data['pageSize'] >= 10) {
  105. $pageSize = 10;
  106. }else{
  107. $pageSize = $data['pageSize'];
  108. }
  109. }
  110. $return = [
  111. 'settledAtStart' => $settledAtStart * 1000,
  112. 'settledAtEnd' => $settledAtEnd * 1000,
  113. 'pageNo' => $pageNo,
  114. 'pageSize' => $pageSize,
  115. 'formStatus' => $formStatus,
  116. "legalEntityBizCodes" => $legalEntityBizCodes,
  117. "formCodes" => $formCodes,
  118. ];
  119. return [true, $return];
  120. }
  121. public function reimburseDetailGet($list){
  122. if(empty($list['list'])) return [true, $list];
  123. list($status,$departList) = $this->department();
  124. if(! $status) return [false, $departList];
  125. $return = [];
  126. foreach ($list['list'] as $key => $value){
  127. list($status, $msg) = $this->reimburseDetail($value);
  128. if(! $status) return [false, $msg];
  129. $parent = $this->findParent($departList,$value['departmentBizCode']);
  130. $parent = array_reverse($parent);
  131. $res = [];
  132. foreach ($parent as $value){
  133. $tmp2['code'] = $value['businessCode'];
  134. $tmp2['value'] = $value['name'];
  135. $res[] = $tmp2;
  136. }
  137. $msg['departmentList'] = $res;
  138. $return[] = $msg;
  139. }
  140. return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
  141. }
  142. // 辅助函数用于递归查找父节点
  143. function findParent($data, $businessCode) {
  144. // 存储结果的数组
  145. $result = [];
  146. foreach ($data as $item) {
  147. if ($item['businessCode'] == $businessCode) {
  148. // 找到了当前节点的父节点
  149. if ($item['parentBizCode'] != '') {
  150. // 添加父节点到结果数组
  151. $result[] = $item;
  152. // 继续递归查找父节点的父节点
  153. $result = array_merge($result, $this->findParent($data, $item['parentBizCode']));
  154. }
  155. break; // 找到后退出循环
  156. }
  157. }
  158. return $result;
  159. }
  160. function findChildren($data, $businessCode) {
  161. // 存储结果的数组
  162. $result = [];
  163. // 遍历数据,寻找匹配的子元素
  164. foreach ($data as $item) {
  165. if ($item['parentBizCode'] == $businessCode) {
  166. // 如果找到了子元素,则将其添加到结果数组中
  167. $result[] = $item;
  168. // 递归查找该子元素的所有子元素
  169. $children = $this->findChildren($data, $item['businessCode']);
  170. if (!empty($children)) {
  171. // 将找到的子元素合并到结果数组中
  172. $result = array_merge($result, $children);
  173. }
  174. }
  175. }
  176. return $result;
  177. }
  178. public function reimburseDetail($data){
  179. if(empty($data['formCode'])) return [false, ["报销单详情请求缺失formCode"]];
  180. $post = [
  181. "formCode"=> $data['formCode'],
  182. "enablePayeeInfo"=> false,
  183. "enablePayerInfo"=> false,
  184. "enableAssociatedForms"=> false,
  185. "enableTravelRouteList"=> false,
  186. "enableCreditInfo"=> false,
  187. "enableExpenseList"=> true,//
  188. "enableInvoiceList"=> true,//
  189. "enableExpenseAllocationList"=> true,//
  190. "enableTravelPartnerInfo"=> false,
  191. "enableAttachments"=> false,
  192. "enableCapitalPlanPaymentLog"=> false
  193. ];
  194. $header = $this->head;
  195. //每刻所有配置
  196. $url_array = $this->param;
  197. //组织获取参数
  198. $url = $this->domain_url . $url_array['reimburseDetail'];
  199. $header = array_merge(['Content-Type:application/json'], $header);
  200. list($status, $result) = $this->post_helper($url,$post, $header);
  201. if(! $status) return [$status, $result];
  202. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  203. return [true, $result['data'] ?? []];
  204. }
  205. public function loan($data){
  206. //校验
  207. list($status, $msg) = $this->loanRule($data);
  208. if(! $status) return [false, $msg];
  209. //获取token
  210. list($status, $token) = $this->getToken();
  211. if(! $status) return [false, $token];
  212. $header = $this->head;
  213. //每刻所有配置
  214. $url_array = $this->param;
  215. //组织获取参数
  216. $url = $this->domain_url . $url_array['loan'];
  217. $header = array_merge(['Content-Type:application/json'], $header);
  218. list($status, $result) = $this->post_helper($url,$msg, $header);
  219. if(! $status) return [$status, $result];
  220. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  221. //组织返回数据
  222. if(empty($result['data'])) return [true, ['list' => null, 'hasNextPage' => false]];
  223. //获取报销单详情填充
  224. list($status, $return) = $this->loanDetailGet($result['data']);
  225. if(! $status) return [false, $return];
  226. if(! empty($return['list'])){
  227. list($status,$msg) = $this->tradingPartner($return);
  228. if(! $status) return [false, $msg];
  229. foreach ($return['list'] as $key => $value){
  230. $return['list'][$key]['tradingPartnerBizCodeType'] = $msg[$value['tradingPartnerBizCode']] ?? [];
  231. }
  232. }
  233. return [true, $return];
  234. }
  235. public function loanRule($data){
  236. if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空'];
  237. $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00');
  238. $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59');
  239. if(empty($settledAtStart)) return [false, '单据的支付时间格式错误'];
  240. if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误'];
  241. $formCodes = [];
  242. if(! empty($data['formCodes'])) $formCodes = array_filter($data['formCodes']);
  243. $formStatus = "";
  244. if(! empty($data['formStatus'])) $formStatus = $data['formStatus'];
  245. $legalEntityBizCodes = [];
  246. if(! empty($data['legalEntityBizCodes'])) $legalEntityBizCodes = $data['legalEntityBizCodes'];
  247. $pageNo = 1;
  248. $pageSize = 10;
  249. if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
  250. if(! empty($data['pageSize'])) {
  251. if($data['pageSize'] >= 10) {
  252. $pageSize = 10;
  253. }else{
  254. $pageSize = $data['pageSize'];
  255. }
  256. }
  257. $return = [
  258. 'settledAtStart' => $settledAtStart * 1000,
  259. 'settledAtEnd' => $settledAtEnd * 1000,
  260. 'pageNo' => $pageNo,
  261. 'pageSize' => $pageSize,
  262. 'formStatus' => $formStatus,
  263. "legalEntityBizCodes" => $legalEntityBizCodes,
  264. "formCodes" => $formCodes,
  265. ];
  266. return [true, $return];
  267. }
  268. public function loanDetailGet($list){
  269. if(empty($list['list'])) return [true, $list];
  270. list($status,$departList) = $this->department();
  271. if(! $status) return [false, $departList];
  272. $return = [];
  273. foreach ($list['list'] as $value){
  274. list($status, $msg) = $this->loanDetail($value);
  275. if(! $status) return [false, $msg];
  276. $parent = $this->findParent($departList,$value['departmentBizCode']);
  277. $parent = array_reverse($parent);
  278. $res = [];
  279. foreach ($parent as $value){
  280. $tmp2['code'] = $value['businessCode'];
  281. $tmp2['value'] = $value['name'];
  282. $res[] = $tmp2;
  283. }
  284. $msg['departmentList'] = $res;
  285. $return[] = $msg;
  286. }
  287. return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
  288. }
  289. public function loanDetail($data){
  290. if(empty($data['formCode'])) return [false, ["借款单详情请求缺失formCode"]];
  291. $header = $this->head;
  292. //每刻所有配置
  293. $url_array = $this->param;
  294. //组织获取参数
  295. $url = $this->domain_url . $url_array['loanDetail'] . $data['formCode'];
  296. $header = array_merge(['Content-Type:application/json'], $header);
  297. list($status, $result) = $this->get_helper($url,$header);
  298. if(! $status) return [$status, $result];
  299. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  300. return [true, $result['data'] ?? []];
  301. }
  302. public function tradingPartner($data){
  303. if(empty($data['list'])) return [true, []];
  304. $bizCodes = array_values(array_filter(array_unique(array_column($data['list'], 'tradingPartnerBizCode'))));
  305. if(empty($bizCodes)) return [true, []];
  306. $post = [
  307. 'bizCodes' => $bizCodes,
  308. ];
  309. //获取token
  310. list($status, $token) = $this->getToken();
  311. if(! $status) return [false, $token];
  312. $header = $this->head;
  313. //每刻所有配置
  314. $url_array = $this->param;
  315. //组织获取参数
  316. $url = $this->domain_url . $url_array['tradingPartner'];
  317. $header = array_merge(['Content-Type:application/json'], $header);
  318. list($status, $result) = $this->post_helper($url,$post, $header);
  319. if(! $status) return [$status, $result];
  320. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  321. $map = array_column($result['data']['list'], 'partnerType','bizCode');
  322. return [true, $map];
  323. }
  324. public function department(){
  325. //获取token
  326. list($status, $token) = $this->getToken();
  327. if(! $status) return [false, $token];
  328. $header = $this->head;
  329. //每刻所有配置
  330. $url_array = $this->param;
  331. //组织获取参数
  332. $url = $this->domain_url . $url_array['department'];
  333. $header = array_merge(['Content-Type:application/json'], $header);
  334. $return = [];
  335. $offset = 1;
  336. $count = 100;
  337. do {
  338. $post = [
  339. 'pageNo' => $offset,
  340. 'pageSize' => $count,
  341. ];
  342. list($status, $result) = $this->post_helper($url,$post, $header);
  343. if(! $status) return [$status, $result];
  344. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  345. if(! empty($result['data']['list'])){
  346. foreach ($result['data']['list'] as $value){
  347. $return[] = [
  348. 'name' => $value['name'],
  349. 'businessCode' => $value['businessCode'],
  350. 'parentBizCode' => $value['parentBizCode'],
  351. // 'fullName' => $value['fullName']
  352. ];
  353. }
  354. }
  355. $next = $result['data']['hasNextPage'];
  356. // 更新 offset
  357. $offset += 1;
  358. } while ($next);
  359. return [true, $return];
  360. }
  361. //保存到oa
  362. public function saveOaData($data, $header){
  363. $return = [
  364. 'status' => 'error',
  365. 'success' => false,
  366. 'message' => "",
  367. 'data' => null,
  368. ];
  369. //失败
  370. if(empty($data)) {
  371. $return['message'] = '请求参数不能为空';
  372. return $return;
  373. }
  374. $url = "https://gzy.qingyaokeji.com/api/module-data/voucher_oa_pay/voucher_oa_pay/diy/save_oa_data";
  375. $post = $data;
  376. $auth = $header['authorization'][0] ?? "";
  377. $header = array_merge(['Content-Type:application/json'], ['Authorization: ' . $auth]);
  378. list($status, $result) = $this->post_helper1($url,$post, $header);
  379. if(! $status) {
  380. //失败
  381. $return['message'] = $result;
  382. return $return;
  383. }
  384. if(isset($result['message']) && isset($result['type']) && $result['type'] === 'errorVm') {
  385. //失败
  386. $return['message'] = $result['description'];
  387. return $return;
  388. }
  389. if(! empty($result) && ! empty($result[0]['message'])){
  390. //失败
  391. $return['message'] = $result[0]['message'];
  392. return $return;
  393. }
  394. //成功
  395. if(isset($result['success']) && $result['success'] == true) return $result;
  396. $return['message'] = "异常错误,请联系开发工程师";
  397. return $return;
  398. }
  399. public function post_helper($url, $data, $header = [], $timeout = 20){
  400. Log::channel('apiMcLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
  401. $ch = curl_init();
  402. curl_setopt($ch, CURLOPT_URL, $url);
  403. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  404. curl_setopt($ch, CURLOPT_ENCODING, '');
  405. curl_setopt($ch, CURLOPT_POST, 1);
  406. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  407. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  408. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  409. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  410. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  411. $r = curl_exec($ch);
  412. if ($r === false) {
  413. // 获取错误号
  414. $errorNumber = curl_errno($ch);
  415. // 获取错误信息
  416. $errorMessage = curl_error($ch);
  417. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  418. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $message]);
  419. return [false, $message];
  420. }
  421. curl_close($ch);
  422. $return = json_decode($r, true);
  423. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $return]);
  424. return [true, $return];
  425. }
  426. public function get_helper($url,$header=[],$timeout = 20){
  427. Log::channel('apiMcLog')->info('每刻GET', ["api" => $url ,"header" => $header]);
  428. $ch = curl_init();
  429. curl_setopt_array($ch, array(
  430. CURLOPT_URL => $url,
  431. CURLOPT_RETURNTRANSFER => true,
  432. CURLOPT_ENCODING => '',
  433. CURLOPT_MAXREDIRS => 10,
  434. CURLOPT_TIMEOUT => $timeout,
  435. CURLOPT_FOLLOWLOCATION => true,
  436. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  437. CURLOPT_CUSTOMREQUEST => 'GET',
  438. CURLOPT_SSL_VERIFYPEER => false,
  439. CURLOPT_HTTPHEADER => $header,
  440. ));
  441. $r = curl_exec($ch);
  442. if ($r === false) {
  443. // 获取错误号
  444. $errorNumber = curl_errno($ch);
  445. // 获取错误信息
  446. $errorMessage = curl_error($ch);
  447. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  448. Log::channel('apiMcLog')->info('每刻GET', ["message" => $message]);
  449. return [false, $message];
  450. }
  451. curl_close($ch);
  452. $return = json_decode($r, true);
  453. Log::channel('apiMcLog')->info('每刻GET', ["message" => $return]);
  454. return [true, $return];
  455. }
  456. public function post_helper1($url, $data, $header = [], $timeout = 20){
  457. Log::channel('apiMcLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
  458. $ch = curl_init();
  459. curl_setopt($ch, CURLOPT_URL, $url);
  460. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  461. curl_setopt($ch, CURLOPT_ENCODING, '');
  462. curl_setopt($ch, CURLOPT_POST, 1);
  463. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  464. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  465. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  466. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  467. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  468. $r = curl_exec($ch);
  469. if ($r === false) {
  470. // 获取错误号
  471. $errorNumber = curl_errno($ch);
  472. // 获取错误信息
  473. $errorMessage = curl_error($ch);
  474. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  475. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $message]);
  476. return [false, $message];
  477. }
  478. // 获取HTTP状态码
  479. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  480. curl_close($ch);
  481. if($httpCode == 401) return [false, '登陆凭证错误,请检查'];
  482. $return = json_decode($r, true);
  483. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $return]);
  484. return [true, $return];
  485. }
  486. }