<?php

namespace app\realname\controller;

use fastphp\base\Controller;
use Jdcloud\Credentials\Credentials;
use Jdcloud\Result;
use Jdcloud\Cloudauth\CloudauthClient;
use app\api\model\User;

class Index extends Controller
{
    private function getCred(){
        $ak = "JDC_A3B38E573D7404F976472FB42F52";
        $sk = "C9BAE98F49B42DA2DC02868A75AA3AD8";
        $cred = new Credentials($ak, $sk);
        return $cred;
    }
    
    public function verify()
    {        

        $temp = json_decode(file_get_contents("php://input"),true);
        
        $user = new User;
        $user_one_info = $user->getOne(['Id'=>$this->userinfo['UserId']]);
        $update_one = [];

        if ($user_one_info['is_verify'] == 1){
            $data = [
                'Code'=>20000
            ];
            echo  json_encode($data);
            die;
        }
        if ($user_one_info['is_verify'] == 2){
            $data = [
                'Code'=>-10000
            ];
            echo  json_encode($data);
            die;
        }
        $flag = $user->getOne(['id_code'=>$temp['id_code'],'is_verify'=>1]);
        if($flag) {
            $data = [
                'Code'=>30000
            ];
            echo  json_encode($data);
            die;
        }

        $client = new CloudauthClient([
            'credentials'  => $this->getCred(),
            'version' => 'latest',
            'scheme' => 'http'
        ]);

        $res = $client->GetAliveUrl([
            'name'  => $temp['name'],
            'idcard'  => $temp['id_code'],
            'returnUrl'  => 'https://php-api.juip.com/realname/Publicget/getresult?id_code='.$temp['id_code']
        ]);

        if(isset($res['result']['code']) && $res['result']['code'] == 'ok'){
            $update_one['certify_id'] = $res['result']['token'];
            $update_one['Name'] = $temp['name'];
            $update_one['id_code'] = $temp['id_code'];
            $data['url'] = $res['result']['url'];
            if($user->updateOne(['Id'=>$this->userinfo['UserId']],$update_one)){
                $data['Code'] = 10000;
            } else {
                $data = [
                    'Code'=>-10000
                ];
            }
        }  else {
            $data = [
                'Code'=>-10000
            ];
        }
        echo  json_encode($data);

        
        
    }

}