MayCurServerService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. $pageNo = 1;
  95. $pageSize = 10;
  96. if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
  97. if(! empty($data['pageSize'])) {
  98. if($data['pageSize'] >= 10) {
  99. $pageSize = 10;
  100. }else{
  101. $pageSize = $data['pageSize'];
  102. }
  103. }
  104. $return = [
  105. 'settledAtStart' => $settledAtStart * 1000,
  106. 'settledAtEnd' => $settledAtEnd * 1000,
  107. 'pageNo' => $pageNo,
  108. 'pageSize' => $pageSize,
  109. 'formStatus' => 'COMPLETED',
  110. "legalEntityBizCodes" => [
  111. "ELC2106251C5CMNI8"
  112. ],
  113. ];
  114. return [true, $return];
  115. }
  116. public function reimburseDetailGet($list){
  117. if(empty($list['list'])) return [true, $list];
  118. $return = [];
  119. foreach ($list['list'] as $key => $value){
  120. list($status, $msg) = $this->reimburseDetail($value);
  121. if(! $status) return [false, $msg];
  122. $return[] = $msg;
  123. }
  124. return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
  125. }
  126. public function reimburseDetail($data){
  127. if(empty($data['formCode'])) return [false, ["报销单详情请求缺失formCode"]];
  128. $post = [
  129. "formCode"=> $data['formCode'],
  130. "enablePayeeInfo"=> true,
  131. "enablePayerInfo"=> true,
  132. "enableAssociatedForms"=> true,
  133. "enableTravelRouteList"=> true,
  134. "enableCreditInfo"=> true,
  135. "enableExpenseList"=> true,
  136. "enableInvoiceList"=> true,
  137. "enableExpenseAllocationList"=> true,
  138. "enableTravelPartnerInfo"=> true,
  139. "enableAttachments"=> true,
  140. "enableCapitalPlanPaymentLog"=> true
  141. ];
  142. $header = $this->head;
  143. //每刻所有配置
  144. $url_array = $this->param;
  145. //组织获取参数
  146. $url = $this->domain_url . $url_array['reimburseDetail'];
  147. $header = array_merge(['Content-Type:application/json'], $header);
  148. list($status, $result) = $this->post_helper($url,$post, $header);
  149. if(! $status) return [$status, $result];
  150. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  151. return [true, $result['data'] ?? []];
  152. }
  153. public function loan($data){
  154. //校验
  155. list($status, $msg) = $this->loanRule($data);
  156. if(! $status) return [false, $msg];
  157. //获取token
  158. list($status, $token) = $this->getToken();
  159. if(! $status) return [false, $token];
  160. $header = $this->head;
  161. //每刻所有配置
  162. $url_array = $this->param;
  163. //组织获取参数
  164. $url = $this->domain_url . $url_array['loan'];
  165. $header = array_merge(['Content-Type:application/json'], $header);
  166. list($status, $result) = $this->post_helper($url,$msg, $header);
  167. if(! $status) return [$status, $result];
  168. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  169. //组织返回数据
  170. if(empty($result['data'])) return [true, ['list' => null, 'hasNextPage' => false]];
  171. //获取报销单详情填充
  172. list($status, $return) = $this->loanDetailGet($result['data']);
  173. if(! $status) return [false, $return];
  174. if(! empty($return['list'])){
  175. list($status,$msg) = $this->tradingPartner($return);
  176. if(! $status) return [false, $msg];
  177. foreach ($return['list'] as $key => $value){
  178. $return['list'][$key]['tradingPartnerBizCodeType'] = $msg[$value['tradingPartnerBizCode']] ?? [];
  179. }
  180. }
  181. return [true, $return];
  182. }
  183. public function loanRule($data){
  184. if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空'];
  185. $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00');
  186. $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59');
  187. if(empty($settledAtStart)) return [false, '单据的支付时间格式错误'];
  188. if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误'];
  189. $pageNo = 1;
  190. $pageSize = 10;
  191. if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
  192. if(! empty($data['pageSize'])) {
  193. if($data['pageSize'] >= 10) {
  194. $pageSize = 10;
  195. }else{
  196. $pageSize = $data['pageSize'];
  197. }
  198. }
  199. $return = [
  200. 'settledAtStart' => $settledAtStart * 1000,
  201. 'settledAtEnd' => $settledAtEnd * 1000,
  202. 'pageNo' => $pageNo,
  203. 'pageSize' => $pageSize,
  204. 'formStatus' => 'COMPLETED',
  205. "legalEntityBizCodes" => [
  206. "ELC2106251C5CMNI8"
  207. ],
  208. ];
  209. return [true, $return];
  210. }
  211. public function loanDetailGet($list){
  212. if(empty($list['list'])) return [true, $list];
  213. $return = [];
  214. foreach ($list['list'] as $value){
  215. list($status, $msg) = $this->loanDetail($value);
  216. if(! $status) return [false, $msg];
  217. $return[] = $msg;
  218. }
  219. return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
  220. }
  221. public function loanDetail($data){
  222. if(empty($data['formCode'])) return [false, ["借款单详情请求缺失formCode"]];
  223. $header = $this->head;
  224. //每刻所有配置
  225. $url_array = $this->param;
  226. //组织获取参数
  227. $url = $this->domain_url . $url_array['loanDetail'] . $data['formCode'];
  228. $header = array_merge(['Content-Type:application/json'], $header);
  229. list($status, $result) = $this->get_helper($url,$header);
  230. if(! $status) return [$status, $result];
  231. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  232. return [true, $result['data'] ?? []];
  233. }
  234. public function tradingPartner($data){
  235. if(empty($data['list'])) return [true, []];
  236. $bizCodes = array_values(array_filter(array_unique(array_column($data['list'], 'tradingPartnerBizCode'))));
  237. if(empty($bizCodes)) return [true, []];
  238. $post = [
  239. 'bizCodes' => $bizCodes,
  240. ];
  241. //获取token
  242. list($status, $token) = $this->getToken();
  243. if(! $status) return [false, $token];
  244. $header = $this->head;
  245. //每刻所有配置
  246. $url_array = $this->param;
  247. //组织获取参数
  248. $url = $this->domain_url . $url_array['tradingPartner'];
  249. $header = array_merge(['Content-Type:application/json'], $header);
  250. list($status, $result) = $this->post_helper($url,$post, $header);
  251. if(! $status) return [$status, $result];
  252. if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
  253. $map = array_column($result['data']['list'], 'partnerType','bizCode');
  254. return [true, $map];
  255. }
  256. public function post_helper($url, $data, $header = [], $timeout = 20){
  257. Log::channel('apiMcLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
  258. $ch = curl_init();
  259. curl_setopt($ch, CURLOPT_URL, $url);
  260. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  261. curl_setopt($ch, CURLOPT_ENCODING, '');
  262. curl_setopt($ch, CURLOPT_POST, 1);
  263. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  264. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  265. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  266. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  267. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  268. $r = curl_exec($ch);
  269. if ($r === false) {
  270. // 获取错误号
  271. $errorNumber = curl_errno($ch);
  272. // 获取错误信息
  273. $errorMessage = curl_error($ch);
  274. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  275. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $message]);
  276. return [false, $message];
  277. }
  278. curl_close($ch);
  279. $return = json_decode($r, true);
  280. Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $return]);
  281. return [true, $return];
  282. }
  283. public function get_helper($url,$header=[],$timeout = 20){
  284. Log::channel('apiMcLog')->info('每刻GET', ["api" => $url ,"header" => $header]);
  285. $ch = curl_init();
  286. curl_setopt_array($ch, array(
  287. CURLOPT_URL => $url,
  288. CURLOPT_RETURNTRANSFER => true,
  289. CURLOPT_ENCODING => '',
  290. CURLOPT_MAXREDIRS => 10,
  291. CURLOPT_TIMEOUT => $timeout,
  292. CURLOPT_FOLLOWLOCATION => true,
  293. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  294. CURLOPT_CUSTOMREQUEST => 'GET',
  295. CURLOPT_SSL_VERIFYPEER => false,
  296. CURLOPT_HTTPHEADER => $header,
  297. ));
  298. $r = curl_exec($ch);
  299. if ($r === false) {
  300. // 获取错误号
  301. $errorNumber = curl_errno($ch);
  302. // 获取错误信息
  303. $errorMessage = curl_error($ch);
  304. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  305. Log::channel('apiMcLog')->info('每刻GET', ["message" => $message]);
  306. return [false, $message];
  307. }
  308. curl_close($ch);
  309. $return = json_decode($r, true);
  310. Log::channel('apiMcLog')->info('每刻GET', ["message" => $return]);
  311. return [true, $return];
  312. }
  313. }