| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 | 
							- <?php
 
- namespace App\Service;
 
- use Illuminate\Support\Facades\Log;
 
- use Illuminate\Support\Facades\Redis;
 
- class MayCurServerService extends Service
 
- {
 
-     protected $param = "";
 
-     protected $domain_url = "";
 
-     protected $head = null;
 
-     public function __construct()
 
-     {
 
-         $this->param = config("maycur");
 
-         $this->domain_url = $this->param['dd_url'];
 
-     }
 
-     public function getToken(){
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //redis 存储的token
 
-         $key = $this->domain_url . $url_array['login_redis_topic'];
 
-         $token = Redis::get($key);
 
-         if(! empty($token)) {
 
-             $this->head = json_decode($token,true);
 
-             return [true, ''];
 
-         }
 
-         //获取token的参数
 
-         $appcode = env('maycur_appcode');
 
-         $appsercet = env('maycur_appsercet');
 
-         if(empty($appcode) || empty($appsercet)) return [false, '每刻鉴权认证参数不能为空'];
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['login'];
 
-         $time = microtime(true);
 
-         $timeStamp = intval($time * 1000);
 
-         //生成secret
 
-         $secret = $this->getSecret($appcode, $appsercet, $timeStamp);
 
-         $post = [
 
-             "secret" => $secret,
 
-             "appCode" => $appcode,
 
-             "timestamp" => $timeStamp
 
-         ];
 
-         $header = ['Content-Type:application/json'];
 
-         list($status, $result) = $this->post_helper($url,$post, $header);
 
-         if(! $status) return [$status, $result];
 
-         if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message'] ?? '鉴权失败,请联系开发者'];
 
-         $token_array = [
 
-             'tokenId:' . $result['data']['tokenId'],
 
-             'entCode:' . $result['data']['entCode'],
 
-         ];
 
-         $this->head = $token_array;
 
-         Redis::setex($key, $url_array['login_expire_time'], json_encode($token_array));
 
-         return [true, ''];
 
-     }
 
-     private function getSecret($appCode, $appSecret, $timeStamp) {
 
-         // 拼接字符串并计算 SHA-256 哈希值
 
-         $stringToHash = $appSecret . ":" . $appCode . ":" . $timeStamp;
 
-         $hashedString = hash('sha256', $stringToHash);
 
-         return $hashedString;
 
-     }
 
-     public function reimburse($data){
 
-        //校验
 
-        list($status, $msg) = $this->reimburseRule($data);
 
-        if(! $status) return [false, $msg];
 
-        //获取token
 
-        list($status, $token) = $this->getToken();
 
-        if(! $status) return [false, $token];
 
-        $header = $this->head;
 
-        //每刻所有配置
 
-        $url_array = $this->param;
 
-        //组织获取参数
 
-        $url = $this->domain_url . $url_array['reimburse'];
 
-        $header = array_merge(['Content-Type:application/json'], $header);
 
-        list($status, $result) = $this->post_helper($url,$msg, $header);
 
-        if(! $status) return [$status, $result];
 
-        if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-        //组织返回数据
 
-        if(empty($result['data'])) return [true, ['list' => null, 'hasNextPage' => false]];
 
-        //获取报销单详情填充
 
-        list($status, $return) = $this->reimburseDetailGet($result['data']);
 
-        if(! $status) return [false, $return];
 
-        if(! empty($return['list'])){
 
-            list($status,$msg) = $this->tradingPartner($return);
 
-            if(! $status) return [false, $msg];
 
-            foreach ($return['list'] as $key => $value){
 
-                $return['list'][$key]['tradingPartnerBizCodeType'] = $msg[$value['tradingPartnerBizCode']] ?? [];
 
-            }
 
-        }
 
-        return [true, $return];
 
-     }
 
-     public function reimburseRule($data){
 
-         if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空'];
 
-         $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00');
 
-         $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59');
 
-         if(empty($settledAtStart)) return [false, '单据的支付时间格式错误'];
 
-         if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误'];
 
-         $formCodes = [];
 
-         if(! empty($data['formCodes'])) $formCodes = array_filter($data['formCodes']);
 
-         $formStatus = "";
 
-         if(! empty($data['formStatus'])) $formStatus = $data['formStatus'];
 
-         $legalEntityBizCodes = [];
 
-         if(! empty($data['legalEntityBizCodes'])) $legalEntityBizCodes = $data['legalEntityBizCodes'];
 
-         $pageNo = 1;
 
-         $pageSize = 10;
 
-         if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
 
-         if(! empty($data['pageSize'])) {
 
-             if($data['pageSize'] >= 10) {
 
-                 $pageSize = 10;
 
-             }else{
 
-                 $pageSize = $data['pageSize'];
 
-             }
 
-         }
 
-         $return = [
 
-             'settledAtStart' => $settledAtStart * 1000,
 
-             'settledAtEnd' => $settledAtEnd * 1000,
 
-             'pageNo' => $pageNo,
 
-             'pageSize' => $pageSize,
 
-             'formStatus' => $formStatus,
 
-             "legalEntityBizCodes" => $legalEntityBizCodes,
 
-             "formCodes" => $formCodes,
 
-         ];
 
-         return [true, $return];
 
-     }
 
-     public function reimburseDetailGet($list){
 
-         if(empty($list['list'])) return [true, $list];
 
-         list($status,$departList) = $this->department();
 
-         if(! $status) return [false, $departList];
 
-         $return = [];
 
-         foreach ($list['list'] as $key => $value){
 
-             list($status, $msg) = $this->reimburseDetail($value);
 
-             if(! $status) return [false, $msg];
 
-             $parent = $this->findParent($departList,$value['departmentBizCode']);
 
-             $parent = array_reverse($parent);
 
-             $res = [];
 
-             foreach ($parent as $value){
 
-                 $tmp2['code'] = $value['businessCode'];
 
-                 $tmp2['value'] = $value['name'];
 
-                 $res[] = $tmp2;
 
-             }
 
-             $msg['departmentList'] = $res;
 
-             $return[] = $msg;
 
-         }
 
-         return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
 
-     }
 
-     // 辅助函数用于递归查找父节点
 
-     function findParent($data, $businessCode) {
 
-         // 存储结果的数组
 
-         $result = [];
 
-         foreach ($data as $item) {
 
-             if ($item['businessCode'] == $businessCode) {
 
-                 // 找到了当前节点的父节点
 
-                 if ($item['parentBizCode'] != '') {
 
-                     // 添加父节点到结果数组
 
-                     $result[] = $item;
 
-                     // 继续递归查找父节点的父节点
 
-                     $result = array_merge($result, $this->findParent($data, $item['parentBizCode']));
 
-                 }
 
-                 break; // 找到后退出循环
 
-             }
 
-         }
 
-         return $result;
 
-     }
 
-     function findChildren($data, $businessCode) {
 
-         // 存储结果的数组
 
-         $result = [];
 
-         // 遍历数据,寻找匹配的子元素
 
-         foreach ($data as $item) {
 
-             if ($item['parentBizCode'] == $businessCode) {
 
-                 // 如果找到了子元素,则将其添加到结果数组中
 
-                 $result[] = $item;
 
-                 // 递归查找该子元素的所有子元素
 
-                 $children = $this->findChildren($data, $item['businessCode']);
 
-                 if (!empty($children)) {
 
-                     // 将找到的子元素合并到结果数组中
 
-                     $result = array_merge($result, $children);
 
-                 }
 
-             }
 
-         }
 
-         return $result;
 
-     }
 
-     public function reimburseDetail($data){
 
-         if(empty($data['formCode'])) return [false, ["报销单详情请求缺失formCode"]];
 
-         $post = [
 
-             "formCode"=> $data['formCode'],
 
-             "enablePayeeInfo"=> false,
 
-             "enablePayerInfo"=> false,
 
-             "enableAssociatedForms"=> false,
 
-             "enableTravelRouteList"=> false,
 
-             "enableCreditInfo"=> false,
 
-             "enableExpenseList"=> true,//
 
-             "enableInvoiceList"=> true,//
 
-             "enableExpenseAllocationList"=> true,//
 
-             "enableTravelPartnerInfo"=> false,
 
-             "enableAttachments"=> false,
 
-             "enableCapitalPlanPaymentLog"=> false
 
-         ];
 
-         $header = $this->head;
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['reimburseDetail'];
 
-         $header = array_merge(['Content-Type:application/json'], $header);
 
-         list($status, $result) = $this->post_helper($url,$post, $header);
 
-         if(! $status) return [$status, $result];
 
-         if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-         return [true, $result['data'] ?? []];
 
-     }
 
-     public function loan($data){
 
-         //校验
 
-         list($status, $msg) = $this->loanRule($data);
 
-         if(! $status) return [false, $msg];
 
-         //获取token
 
-         list($status, $token) = $this->getToken();
 
-         if(! $status) return [false, $token];
 
-         $header = $this->head;
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['loan'];
 
-         $header = array_merge(['Content-Type:application/json'], $header);
 
-         list($status, $result) = $this->post_helper($url,$msg, $header);
 
-         if(! $status) return [$status, $result];
 
-         if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-         //组织返回数据
 
-         if(empty($result['data'])) return [true, ['list' => null, 'hasNextPage' => false]];
 
-         //获取报销单详情填充
 
-         list($status, $return) = $this->loanDetailGet($result['data']);
 
-         if(! $status) return [false, $return];
 
-         if(! empty($return['list'])){
 
-             list($status,$msg) = $this->tradingPartner($return);
 
-             if(! $status) return [false, $msg];
 
-             foreach ($return['list'] as $key => $value){
 
-                 $return['list'][$key]['tradingPartnerBizCodeType'] = $msg[$value['tradingPartnerBizCode']] ?? [];
 
-             }
 
-         }
 
-         return [true, $return];
 
-     }
 
-     public function loanRule($data){
 
-         if(empty($data['settledAtStart']) || empty($data['settledAtEnd'])) return [false, '单据的支付时间不能为空'];
 
-         $settledAtStart = strtotime($data['settledAtStart'] . '00:00:00');
 
-         $settledAtEnd = strtotime($data['settledAtEnd'] . '23:59:59');
 
-         if(empty($settledAtStart)) return [false, '单据的支付时间格式错误'];
 
-         if(empty($settledAtEnd)) return [false, '单据的支付时间格式错误'];
 
-         $formCodes = [];
 
-         if(! empty($data['formCodes'])) $formCodes = array_filter($data['formCodes']);
 
-         $formStatus = "";
 
-         if(! empty($data['formStatus'])) $formStatus = $data['formStatus'];
 
-         $legalEntityBizCodes = [];
 
-         if(! empty($data['legalEntityBizCodes'])) $legalEntityBizCodes = $data['legalEntityBizCodes'];
 
-         $pageNo = 1;
 
-         $pageSize = 10;
 
-         if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];
 
-         if(! empty($data['pageSize'])) {
 
-             if($data['pageSize'] >= 10) {
 
-                 $pageSize = 10;
 
-             }else{
 
-                 $pageSize = $data['pageSize'];
 
-             }
 
-         }
 
-         $return = [
 
-             'settledAtStart' => $settledAtStart * 1000,
 
-             'settledAtEnd' => $settledAtEnd * 1000,
 
-             'pageNo' => $pageNo,
 
-             'pageSize' => $pageSize,
 
-             'formStatus' => $formStatus,
 
-             "legalEntityBizCodes" => $legalEntityBizCodes,
 
-             "formCodes" => $formCodes,
 
-         ];
 
-         return [true, $return];
 
-     }
 
-     public function loanDetailGet($list){
 
-         if(empty($list['list'])) return [true, $list];
 
-         list($status,$departList) = $this->department();
 
-         if(! $status) return [false, $departList];
 
-         $return = [];
 
-         foreach ($list['list'] as $value){
 
-             list($status, $msg) = $this->loanDetail($value);
 
-             if(! $status) return [false, $msg];
 
-             $parent = $this->findParent($departList,$value['departmentBizCode']);
 
-             $parent = array_reverse($parent);
 
-             $res = [];
 
-             foreach ($parent as $value){
 
-                 $tmp2['code'] = $value['businessCode'];
 
-                 $tmp2['value'] = $value['name'];
 
-                 $res[] = $tmp2;
 
-             }
 
-             $msg['departmentList'] = $res;
 
-             $return[] = $msg;
 
-         }
 
-         return [true, ['list' => $return, 'hasNextPage' => $list['hasNextPage']]];
 
-     }
 
-     public function loanDetail($data){
 
-         if(empty($data['formCode'])) return [false, ["借款单详情请求缺失formCode"]];
 
-         $header = $this->head;
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['loanDetail'] . $data['formCode'];
 
-         $header = array_merge(['Content-Type:application/json'], $header);
 
-         list($status, $result) = $this->get_helper($url,$header);
 
-         if(! $status) return [$status, $result];
 
-         if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-         return [true, $result['data'] ?? []];
 
-     }
 
-     public function tradingPartner($data){
 
-         if(empty($data['list'])) return [true, []];
 
-         $bizCodes = array_values(array_filter(array_unique(array_column($data['list'], 'tradingPartnerBizCode'))));
 
-         if(empty($bizCodes)) return [true, []];
 
-         $post = [
 
-             'bizCodes' => $bizCodes,
 
-         ];
 
-         //获取token
 
-         list($status, $token) = $this->getToken();
 
-         if(! $status) return [false, $token];
 
-         $header = $this->head;
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['tradingPartner'];
 
-         $header = array_merge(['Content-Type:application/json'], $header);
 
-         list($status, $result) = $this->post_helper($url,$post, $header);
 
-         if(! $status) return [$status, $result];
 
-         if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-         $map = array_column($result['data']['list'], 'partnerType','bizCode');
 
-         return [true, $map];
 
-     }
 
-     public function department(){
 
-         //获取token
 
-         list($status, $token) = $this->getToken();
 
-         if(! $status) return [false, $token];
 
-         $header = $this->head;
 
-         //每刻所有配置
 
-         $url_array = $this->param;
 
-         //组织获取参数
 
-         $url = $this->domain_url . $url_array['department'];
 
-         $header = array_merge(['Content-Type:application/json'], $header);
 
-         $return = [];
 
-         $offset = 1;
 
-         $count = 100;
 
-         do {
 
-             $post = [
 
-                 'pageNo' => $offset,
 
-                 'pageSize' => $count,
 
-             ];
 
-             list($status, $result) = $this->post_helper($url,$post, $header);
 
-             if(! $status) return [$status, $result];
 
-             if(isset($result['code']) && $result['code'] != 'ACK') return [false, $result['message']];
 
-             if(! empty($result['data']['list'])){
 
-                 foreach ($result['data']['list'] as $value){
 
-                     $return[] = [
 
-                         'name' => $value['name'],
 
-                         'businessCode' => $value['businessCode'],
 
-                         'parentBizCode' => $value['parentBizCode'],
 
- //                        'fullName' => $value['fullName']
 
-                     ];
 
-                 }
 
-             }
 
-             $next = $result['data']['hasNextPage'];
 
-             // 更新 offset
 
-             $offset += 1;
 
-         } while ($next);
 
-         return [true, $return];
 
-     }
 
-     //保存到oa
 
-     public function saveOaData($data, $header){
 
-         $return = [
 
-             'status' => 'error',
 
-             'success' => false,
 
-             'message' => "",
 
-             'data' => null,
 
-         ];
 
-         //失败
 
-         if(empty($data)) {
 
-             $return['message'] = '请求参数不能为空';
 
-             return $return;
 
-         }
 
-         $url = "https://gzy.qingyaokeji.com/api/module-data/voucher_oa_pay/voucher_oa_pay/diy/save_oa_data";
 
-         $post = $data;
 
-         $auth = $header['authorization'][0] ?? "";
 
-         $header = array_merge(['Content-Type:application/json'], ['Authorization: ' . $auth]);
 
-         list($status, $result) = $this->post_helper1($url,$post, $header);
 
-         if(! $status) {
 
-             //失败
 
-             $return['message'] = $result;
 
-             return $return;
 
-         }
 
-         if(isset($result['message']) && isset($result['type']) && $result['type'] === 'errorVm') {
 
-             //失败
 
-             $return['message'] = $result['description'];
 
-             return $return;
 
-         }
 
-         if(! empty($result) && ! empty($result[0]['message'])){
 
-             //失败
 
-             $return['message'] = $result[0]['message'];
 
-             return $return;
 
-         }
 
-         //成功
 
-         if(isset($result['success']) && $result['success'] == true) return $result;
 
-         $return['message'] = "异常错误,请联系开发工程师";
 
-         return $return;
 
-     }
 
-     public function post_helper($url, $data, $header = [], $timeout = 20){
 
-         Log::channel('apiMcLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
 
-         $ch = curl_init();
 
-         curl_setopt($ch, CURLOPT_URL, $url);
 
-         curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
 
-         curl_setopt($ch, CURLOPT_ENCODING, '');
 
-         curl_setopt($ch, CURLOPT_POST, 1);
 
-         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 
-         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 
-         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
-         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
 
-         if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
 
-         $r = curl_exec($ch);
 
-         if ($r === false) {
 
-             // 获取错误号
 
-             $errorNumber = curl_errno($ch);
 
-             // 获取错误信息
 
-             $errorMessage = curl_error($ch);
 
-             $message = "cURL Error #{$errorNumber}: {$errorMessage}";
 
-             Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $message]);
 
-             return [false, $message];
 
-         }
 
-         curl_close($ch);
 
-         $return = json_decode($r, true);
 
-         Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $return]);
 
-         return [true, $return];
 
-     }
 
-     public function get_helper($url,$header=[],$timeout = 20){
 
-         Log::channel('apiMcLog')->info('每刻GET', ["api" => $url ,"header" => $header]);
 
-         $ch = curl_init();
 
-         curl_setopt_array($ch, array(
 
-             CURLOPT_URL => $url,
 
-             CURLOPT_RETURNTRANSFER => true,
 
-             CURLOPT_ENCODING => '',
 
-             CURLOPT_MAXREDIRS => 10,
 
-             CURLOPT_TIMEOUT => $timeout,
 
-             CURLOPT_FOLLOWLOCATION => true,
 
-             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 
-             CURLOPT_CUSTOMREQUEST => 'GET',
 
-             CURLOPT_SSL_VERIFYPEER => false,
 
-             CURLOPT_HTTPHEADER => $header,
 
-         ));
 
-         $r = curl_exec($ch);
 
-         if ($r === false) {
 
-             // 获取错误号
 
-             $errorNumber = curl_errno($ch);
 
-             // 获取错误信息
 
-             $errorMessage = curl_error($ch);
 
-             $message = "cURL Error #{$errorNumber}: {$errorMessage}";
 
-             Log::channel('apiMcLog')->info('每刻GET', ["message" => $message]);
 
-             return [false, $message];
 
-         }
 
-         curl_close($ch);
 
-         $return = json_decode($r, true);
 
-         Log::channel('apiMcLog')->info('每刻GET', ["message" => $return]);
 
-         return [true, $return];
 
-     }
 
-     public function post_helper1($url, $data, $header = [], $timeout = 20){
 
-         Log::channel('apiMcLog')->info('每刻POST', ["api" => $url , "param" => $data ,"header" => $header]);
 
-         $ch = curl_init();
 
-         curl_setopt($ch, CURLOPT_URL, $url);
 
-         curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
 
-         curl_setopt($ch, CURLOPT_ENCODING, '');
 
-         curl_setopt($ch, CURLOPT_POST, 1);
 
-         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 
-         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 
-         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
-         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
 
-         if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
 
-         $r = curl_exec($ch);
 
-         if ($r === false) {
 
-             // 获取错误号
 
-             $errorNumber = curl_errno($ch);
 
-             // 获取错误信息
 
-             $errorMessage = curl_error($ch);
 
-             $message = "cURL Error #{$errorNumber}: {$errorMessage}";
 
-             Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $message]);
 
-             return [false, $message];
 
-         }
 
-         // 获取HTTP状态码
 
-         $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
-         curl_close($ch);
 
-         if($httpCode == 401) return [false, '登陆凭证错误,请检查'];
 
-         $return = json_decode($r, true);
 
-         Log::channel('apiMcLog')->info('每刻POST结果', ["message" => $return]);
 
-         return [true, $return];
 
-     }
 
- }
 
 
  |