<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


/**
 * 实名认证，真人认证相关
 */
class Userauth extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function __construct(){
        $this->error('投票活动结束了');
    }



    //实名认证信息
    public function idcard_info(){
        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
        $this->success('success',$check);
    }

    //申请实名认证
    public function apply_idcard_confirm(){
        $truename = input('truename','');
        $idcard   = input('idcard'  ,'');

        if(empty($truename) || empty($idcard)){
            $this->error('实名认证信息必填');
        }

        if($this->auth->idcard_status == 1){

            $this->error('您已经完成实名认证');
        }

        if($this->auth->idcard_status == 0){
            $this->error('您已经提交实名认证,请等待审核');
        }


        Db::startTrans();
        $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
        if(!empty($check)){
            if($check['status'] == 0){
                Db::rollback();
                $this->error('您已经提交实名认证,请等待审核');
            }
            if($check['status'] == 1){
                Db::rollback();
                $this->error('您已经完成实名认证');
            }
        }

        $count = Db::name('user_idconfirm')->where(['idcard' => $idcard, 'user_id' => ['neq', $this->auth->id]])->count('id');
        if ($count) {
            $this->error('该身份证号已被他人使用');
        }

        $data = [
            'user_id' => $this->auth->id,
            'truename' => $truename,
            'idcard' => $idcard,
            'status' => 0, //人工审核
            'createtime' => time(),
            'updatetime' => time(),
        ];


        //更新
        $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>$data['status']]);
        if(!empty($check)){
            $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
        }else{
            $rs = Db::name('user_idconfirm')->insertGetId($data);
        }

        if(!$rs || !$update_rs){
            Db::rollback();
            $this->error('提交失败');
        }

        Db::commit();

        $this->success('提交成功,请等待审核');
    }


    //产品链接：https://market.aliyun.com/products/57000002/cmapi026109.html#sku=yuncode20109000025
    //阿里云-数脉api
    //姓名+身份证号
    private function userauth_aliyun_two($cardNo = '',$realname = ''){
        if(!$cardNo || !$realname){
            return false;
        }

        $config = config('aliyun_auth_shumai_two');

        $host = "https://eid.shumaidata.com";
        $path = "/eid/check";
        $method = "POST";
        $appcode = $config['app_code'];
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);

        $querys = "idcard=".$cardNo."&name=".urlencode($realname);
        $bodys  = '';
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设定返回信息中是否包含响应信息头，启用时会将头文件的信息作为数据流输出，true 表示输出信息头, false表示不输出信息头
        //如果需要将字符串转成json，请将 CURLOPT_HEADER 设置成 false
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $returnRes = curl_exec($curl);
        //var_dump($returnRes);
        curl_close($curl);
        $result = json_decode($returnRes,true);
        //dump($result);

        if(is_array($result) && isset($result['code']) && $result['code'] == 0){
            if(isset($result['result']) && isset($result['result']['res'])){
                if($result['result']['res'] == 1){
                    //实名过了
                    return true;
                }
            }
        }
        return false;
    }




}
