<?php

namespace app\taobao\controller;

use app\agent\model\User as UserModel;
use app\taobao\model\SellCouponUserOrgin as CouponModel;
use alipay\AliverifyPublic as Aliverify;
use extend\alisms\Sms;
use extend\redis\Redisop;
use Jdcloud\Credentials\Credentials;
use Jdcloud\Result;
use Jdcloud\Cloudauth\CloudauthClient;

class Realname
{

    public function getCode() {
        $phone = json_decode(file_get_contents("php://input"), true)['phone'];
        $redis = new Redisop();
        $user_model = new UserModel();
        $is_exit = $user_model->getOne(['Phone' => $phone]);
        if($is_exit) {
            $return_data = [
                'code' => 0,
                'msg' => '用户已经存在'
            ];
            echo json_encode($return_data);die;
        }


        $getCode = $redis->get($phone);

        if (empty($getCode)) {
            $code = rand(1000,9999);
            $redis->setOfTime($phone,$code);
            $alisms = new Sms;
            $alisms::sendVerify($phone, $code);
            $return_data = [
                'code' => 1,
                'msg' => '验证码已发送'
            ];
            echo json_encode($return_data);die;
        } else {
            $return_data = [
                'code' => 0,
                'msg' => '请稍后再试'
            ];
            echo json_encode($return_data);die;
        }
        
    }
    //---------------

    //推广用户注册
    public function register()
    {
        $data = json_decode(file_get_contents("php://input"), true);
        // dump($data);die;
        $data['LoginCode'] = trim($data['LoginCode']);

        if(empty($data['LoginCode'])){
            $info = [
                'Code' => 10003,
                'Data' => '',
                'Message' => '账号为空',
            ];
            echo json_encode($info);
            die;
        }
        $redis = new Redisop();
        $code = $redis->get($data['LoginCode']);


        if (empty($code)) {
            $return_data = [
                'code' => 0,
                'msg' => '验证码不正确'
            ];
            echo json_encode($return_data);die;
        } 
        if ($data['code'] != $code) {
            $return_data = [
                'code' => 0,
                'msg' => '验证码不正确'
            ];
            echo json_encode($return_data);die;
        }
        //----

        $data['Phone'] = $data['LoginCode'];
        $data['TenantId'] = 0;
        $data['Enabled'] = 1;
        $data['UpdateTime'] = date('Y-m-d H:i:s',time());
        $data['CreateTime'] = date('Y-m-d H:i:s',time());
        $data['CreateType'] = 4;
        $data['ProductAccountCount'] = 0;
        $data['ExpiredProductAccountCount'] = 0;
        $data['RestAmount'] = 0;
        $data['ConsumeAmount'] = 0;
        $data['DeleteTag'] = 0;
        $data['UseTestCount'] = 0;
        $data['Sex'] = 0;
        $data['Wx'] = '';
        $data['QQ'] = '';
        $data['LastLoginDate'] = date('Y-m-d H:i:s',time());
        $data['Password'] = empty($data['Password'])?cToMd5('123654'):cToMd5(trim($data['Password']));

        $user_model = new UserModel;
        unset($data['code']);

        $has_user = $user_model->isExit("LoginCode='".$data['LoginCode']."' or Phone='".$data['Phone']."' or TaoBao='".$data['Phone']."'");
        if ($has_user){
            if ($has_user['is_verify'] == 0) {
                $data_user_old = [
                    'LoginCode' => $has_user['LoginCode'].'-del',
                    'Phone' => $has_user['Phone'].'-del',
                ];

                $user_model->updateOne(['Id'=>$has_user['Id']],$data_user_old);
            }
            // $info = [
            //     'Code' => 20000,
            //     'Data' => '',
            //     'Message' => '该账号或者手机号被注册了',
            // ];
            // echo json_encode($info);
            // die;
        }

        $has_user2 = $user_model->isExit("id_code='".$data['id_code']."' and is_verify=1");
        
        if ($has_user2){
            $info = [
                'Code' => 30000,
                'Data' => '',
                'Message' => '该身份已经进行认证，无法再次进行认证！',
            ];
            echo json_encode($info);
            die;
        }

        if ($user_model->add($data)) {

            echo json_encode($this->aliverify([
                'id_code' =>  $data['id_code'],
                'name' =>  $data['Name'],
                'phone' => $data['LoginCode']
            ]));
        }
        die;
    }


    private function getCred(){
        $ak = "JDC_A3B38E573D7404F976472FB42F52";
        $sk = "C9BAE98F49B42DA2DC02868A75AA3AD8";
        $cred = new Credentials($ak, $sk);
        return $cred;
    }
    public function aliverify($temp)
    {
        $user = new UserModel;
        $update_one = [];

        $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/taobao/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(['LoginCode'=>$temp['phone']],$update_one)){
                $data['Code'] = 10000;
            } else {
                $data = [
                    'Code'=>-10000
                ];
            }
        }  else {
            $data = [
                'Code'=>-10000
            ];
        }
        return $data;
    }

    
    public function isverify() {
        $user_phone = $_GET['phone'];
        
        $user_model = new UserModel;

        $userinfo = $user_model->getOne(['LoginCode' => $user_phone],'is_verify');

        echo json_encode($userinfo);
    }


}