<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use Illuminate\Http\Request;
use League\Flysystem\Sftp\SftpAdapter;
use League\Flysystem\Filesystem;
use XuanChen\UnionPay\Check;
use XuanChen\UnionPay\Models\UnionpayLog;

class SkyxuController extends Controller
{

    /**
     * Notes: 链接sftp
     * @Author: 玄尘
     * @Date  : 2021/1/26 8:18
     */
    public function sftp()
    {
        $app = app('xuanchen.unionpay.check');
        //        $app->date = '20190722';
        $app->login();
        $app->hasFile();
        $app->start();
        dump($app->msg);
        dump($app);

        die();
        $path    = '/home/wwwroot/pingan/upload/';
        $name    = 'JYMX66007320190722.txt';
        $adapter = new SftpAdapter([
            'host'          => '123.57.16.212',
            'port'          => 22,
            'username'      => 'root',
            'password'      => 'Anetadmin1',
            'privateKey'    => '',
            'passphrase'    => '',
            'root'          => $path,
            'timeout'       => 10,
            'directoryPerm' => 0755,
        ]);

        $filesystem = new Filesystem($adapter);
        $lists      = $filesystem->listContents();

        $content = $filesystem->read($name);

        $content = str_replace("\n", "br", $content);
        dump($content);

        $content = explode("br", $content);

        foreach ($content as $item) {
            $array = explode('|', $item);
            foreach ($array as $item) {
                echo $item . "<br>";
            }
            echo '----------------------------------------' . "<br>";
        }
        dd($content);
    }

    public function index(Request $request)
    {
        if (method_exists($this, $request->action)) {

            return call_user_func_array([$this, $request->action], [$request->all()]);

        } else {
            dd('未找到方法');
        }
    }

    /**
     * Notes: 获取sign
     * @Author: 玄尘
     * @Date  : 2021/2/19 8:17
     */
    public function checkUnionLog()
    {
        $url        = 'http://pac.ysd-bs.com/api/V1/unionpay/query';
        $info       = UnionpayLog::find(177);
        $in_source  = $info->in_source;
        $out_source = $info->out_source;
        $res        = $this->http($in_source, $url);
        dd($res);
        dump($info->in_source);
        dump($info->out_source);
    }

    /**
     * Notes: 校验签名
     * @Author: 玄尘
     * @Date  : 2021/2/19 9:03
     * @param $params
     * @throws \Exception
     */
    public function checkSign($params)
    {
        $sign    = $params['sign'];
        $signStr = $params['str'];
        $app     = app('xuanchen.unionpay');
        $out     = false;
        if (isset($params['isout'])) {
            $out = true;
        }
        $public_key = $app->getPublic($out);

        $pub_key_id = openssl_get_publickey($public_key);

        dump('sign: ' . $sign);
        dump('signStr: ' . $signStr);

        $sign = $app->hexXbin($sign);
        if (!$sign) {
            throw new \Exception('签名错误');
        }

        if ($pub_key_id) {
            $result = (bool) openssl_verify($signStr, $sign, $pub_key_id);
            openssl_free_key($pub_key_id);
        } else {
            throw new \Exception('私钥格式有误');
        }
        $res_str = ($result === true) ? '成功' : '失败';
        dd('验签结果：' . $res_str);
    }

    public function http($data, $url)
    {
        $client   = new Client();
        $response = $client->request('POST', $url,
            [
                'form_params' => $data,
                'http_errors' => false,
            ]);

        $body    = $response->getBody();
        $content = $body->getContents();
        $result  = json_decode($content, true);

        return $result;
    }

}