<?php

namespace app\api\controller;

/**
 
 * 
 
 * 
 * ----------------------------------------------------------------------------
 * 
 
 * 控制器
 */
class MobileMember extends MobileHome
{

    public function initialize()
    {
        parent::initialize();
        $key = request()->header('X-DS-KEY');
        if (!$key) {
            $key = input('param.key'); //微信支付需要
        }
        if (!empty($key)) {
            $mbusertoken_model = model('mbusertoken');
            $mb_user_token_info = $mbusertoken_model->getMbusertokenInfoByToken($key);
            if (empty($mb_user_token_info)) {
                ds_json_encode(11001, lang('please_login'));
            }
            $member_model = model('member');
            $this->member_info = $member_model->getMemberInfoByID($mb_user_token_info['member_id']);

            if (empty($this->member_info)) {
                ds_json_encode(11001, lang('please_login'));
            } else {
                if (!$this->member_info['member_state']) {
                    ds_json_encode(11001, lang('please_login'));
                }
                $this->member_info['member_clienttype'] = $mb_user_token_info['member_clienttype'];
                $this->member_info['member_openid'] = $mb_user_token_info['member_openid'];
                $this->member_info['member_token'] = $mb_user_token_info['member_token'];
                $level_name = $member_model->getOneMemberGrade($mb_user_token_info['member_id']);
                $this->member_info['level_name'] = $level_name['level_name'];
                //读取卖家信息
                $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
                $this->member_info['store_id'] = $seller_info['store_id'];
                //考虑到模型中session
                if (session('member_id') != $this->member_info['member_id']) {
                    //避免重复查询数据库
                    $member_model->createSession(array_merge($this->member_info, $level_name), true);
                }
            }
        } else {
            ds_json_encode(11001, lang('please_login'));
        }
    }

    public function getOpenId()
    {
        return $this->member_info['member_openid'];
    }

    public function setOpenId($openId)
    {
        $this->member_info['member_openid'] = $openId;
        model('mbusertoken')->editMemberOpenId($this->member_info['member_token'], $openId);
    }


    public function getIdCardInfo($memberInfo)
    {
        $url = get_member_idcard_image($memberInfo['member_idcard_image2']);
        $url2 = get_member_idcard_image($memberInfo['member_idcard_image3']);
        $base64 = $this->getBase64($url);
        $base642 = $this->getBase64($url2);
        $res = $this->postAiOcr($base64,$base642);

        return $res;
    }

    public function getIdCardInfo2($memberInfo)
    {
        $url = get_store_joinin_imageurl($memberInfo['business_licence_number_electronic']);
        $url2 = get_store_joinin_imageurl($memberInfo['business_author_letter']);
        $base64 = $this->getBase64($url);
        $base642 = $this->getBase64($url2);
        $res = $this->postAiOcr($base64,$base642);

        return $res;
    }


    public function getCompanyInfo($imgUrl)
    {
        $base64 = $this->getBase64(get_store_joinin_imageurl($imgUrl));
        $res = $this->postCompanyAiOcr($base64);

        return $res;
    }

    public function getBase64($file)
    {
        $base64_image = base64_encode(file_get_contents($file));
        return urlencode($base64_image);
    }

    public function postAiOcr($front,$negative)
    {

//        $negative = $this->getNegative();
        $host    = "https://sidocr.market.alicloudapi.com";
        $path    = "/ocr/idcard";
        $method  = "POST";
        $appcode = "e69350d24e01496a841c3a3fa2ed1427";
        $headers = [];
        array_push($headers, "Authorization:APPCODE ".$appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys  = "front=".$front."&negative=".$negative;
        $url    = $host.$path;

        $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);
//        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $res = curl_exec($curl);
        return json_decode($res,true);
    }

    public function postCompanyAiOcr($imgUrl)
    {
        $host    = "https://slyzhizhao.market.alicloudapi.com";
        $path    = "/business_license_ocr";
        $method  = "POST";
        $appcode = "e69350d24e01496a841c3a3fa2ed1427";
        $headers = [];
        array_push($headers, "Authorization:APPCODE ".$appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys  = "image=".$imgUrl."&url";
        $url    = $host.$path;

        $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);
//        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $res = curl_exec($curl);
        return json_decode($res,true);
    }

    public function getNegative()
    {
        return '[encoded data, 176160 characters]';
    }
}
