MayCurServerService.php 12 KB

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