<?php

namespace app\api\controller;

use fastphp\base\Controller;
use app\api\model\User;
use alipay\Aliverify;

class Test extends Controller
{

    public function aliverify()
    {
        $temp = json_decode(file_get_contents("php://input"),true);
        $info = Aliverify::initVerify2($temp);
        $user = new User;
        $data = [];
        $user_one_info = $user->getOne(['Id'=>$this->userinfo['UserId']]);
        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;
        }

        if($info){
            $update_one['certify_id'] = $info['certifyId'];
            $update_one['Name'] = $temp['name'];
            if(isset($temp['phone'])){
                $update_one['Phone'] = $temp['phone'];
            }
            $update_one['id_code'] = $temp['id_code'];
            $data['certify_id'] = $info['certifyId'];
            $data['url'] = $info['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);
    }

    public function aliGetResult()
    {
        $user = new User;
        $user_one_info = $user->getOne(['Id'=>$this->userinfo['UserId']]);
        $info = Aliverify::getResult2($user_one_info['certify_id']);
        $data = [];
        if($info){
            $user_data['is_verify'] = 1;
            if($user->updateOne(['Id'=>$this->userinfo['UserId']],$user_data)){
                $data = [
                    'Code'=>10000
                ];
            }
        } else {
            $data = [
                'Code'=>-10000
            ];
        }
        echo  json_encode($data);
    }

}