| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | <?phpnamespace 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, $result['data']];       //获取报销单详情填充       list($status, $return) = $this->reimburseDetailGet($result['data']);       if(! $status) return [false, $return];       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, '单据的支付时间格式错误'];        $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' => 'COMPLETED',            "legalEntityBizCodes" => [                "ELC2106251C5CMNI8"            ],        ];        return [true, $return];    }    public function reimburseDetailGet($list){        if(empty($list['list'])) return [true, $list];        foreach ($list['list'] as $key => $value){            list($status, $msg) = $this->reimburseDetail($value);            if(! $status) return [false, $msg];            $list['list'][$key]['reimburseDetail'] = $msg;        }        return [true, $list];    }    public function reimburseDetail($data){        if(empty($data['formCode'])) return [false, ["报销单详情请求缺失formCode"]];        $post = [            "formCode"=> $data['formCode'],            "enablePayeeInfo"=> true,            "enablePayerInfo"=> true,            "enableAssociatedForms"=> true,            "enableTravelRouteList"=> true,            "enableCreditInfo"=> true,            "enableExpenseList"=> true,            "enableInvoiceList"=> true,            "enableExpenseAllocationList"=> true,            "enableTravelPartnerInfo"=> true,            "enableAttachments"=> true,            "enableCapitalPlanPaymentLog"=> true        ];        $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, $result['data']];        //获取报销单详情填充        list($status, $return) = $this->loanDetailGet($result['data']);        if(! $status) return [false, $return];        return [true, $return];    }    public function loanRule($data){        if(empty($data['createdAtStart']) || empty($data['createdAtEnd'])) return [false, '单据的创建时间不能为空'];        $createAtStart = strtotime($data['createdAtStart'] . '00:00:00');        $createdAtEnd = strtotime($data['createdAtEnd'] . '23:59:59');        if(empty($createAtStart)) return [false, '单据的创建开始时间格式错误'];        if(empty($createdAtEnd)) return [false, '单据的创建结束时间格式错误'];        $pageNo = 1;        $pageSize = 10;        if(! empty($data['pageNo'])) $pageNo = $data['pageNo'];        if(! empty($data['pageSize'])) {            if($data['pageSize'] >= 100) {                $pageSize = 10;            }else{                $pageSize = $data['pageSize'];            }        }        $return = [            'createdAtStart' => $createAtStart * 1000,            'createdAtEnd' => $createdAtEnd * 1000,            'pageNo' => $pageNo,            'pageSize' => $pageSize,        ];        return [true, $return];    }    public function loanDetailGet($list){        if(empty($list['list'])) return [true, $list];        foreach ($list['list'] as $key => $value){            list($status, $msg) = $this->loanDetail($value);            if(! $status) return [false, $msg];            $list['list'][$key]['loanDetail'] = $msg;        }        return [true, $list];    }    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 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];    }}
 |