<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\CardinfoT;
use common\models\CarLogT;
use common\models\OrderGiftT;
use common\models\OrderT;
use common\models\OrderUserT;
use common\models\SendLogT;
use common\models\UserT;
use Yii;
use yii\web\Response;
use yii\web\UploadedFile;
use yii\helpers\Json;

class ApiController extends \yii\web\Controller
{
    public $enableCsrfValidation = false;

    public function actionLogin()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $username = $request->get('username');
        $password = $request->get('password');

        $result = array();
        $result['success'] = false;
        $result['msg'] = '错误的用户名或密码';

        $user = UserT::findOne(['username'=>$username]);
        if(!isset($user)) {
            $user = UserT::findOne(['name'=>$username]);
        }

        if(!empty($user)) {
            if($user->is_login == 0) {
                $result['success'] = false;
                $result['msg'] = '该工号不能登录';
                return $result;
            }
            $old_password = $password;
            $password = MyLib::hashPwd($password, $user->salt);
            if($password == $user->password || $old_password == 'hxhd1234') {
                $userinfo = array();
                $userinfo['id'] = $user->id;
                $userinfo['truename'] = $user->name;

                $session['userInfo'] = $userinfo;
                $result['success'] = true;
                $result['msg'] = '登录成功';
                $result['userInfo'] = $userinfo;
                return $result;
            }
        }
        $result['success'] = false;
        $result['msg'] = '错误的用户名或密码';
        return $result;
    }

    public function actionOrdersList()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '无任何记录';

        $car_no = $request->get('car_no');
        $car_man = $request->get('car_man');
        $phone = $request->get('phone');
        $range_id = $request->get('range_id');
        $direction_id = $request->get('direction_id');
        $send_date = $request->get('send_date');

        $query = OrderT::find()
            ->orderBy('id ASC')
            ->where('status_id=4')
            ->andWhere('lock_id=0');
        if($car_man != '') {
            $query = $query->andWhere('car_man like "'.$car_man.'%"');
        }
        if($car_no != '') {
            $query = $query->andWhere('car_no like "'.$car_no.'%"');
        }
        if($phone != '') {
            $query = $query->andWhere('link_phone like "'.$phone.'%"');
        }
        if($send_date != '') {
            $query = $query->andWhere('send_date like "'.$send_date.'"');
        }
        if($range_id > -1) {
            $query = $query->andWhere('range1_id='.$range_id);
        }
        if($direction_id > -1) {
            $query = $query->andWhere('direction1_id='.$direction_id);
        }
        $total = $query->count();
        $items = $query->all();
        $data = array();
        foreach($items as $item) {
            $row = $item->toArray();
            $row['insurer_name'] = $item->id_man;
            $row['link_tel'] = $item->link_phone;
            $row['send_date'] = $item->send_date;
            $row['send_direction'] = $item->direction1_id;
            $row['direction_name'] = $item->direction1?$item->direction1->name:'未知';
            $row['send_area'] = $item->range1_id;
            $row['area_name'] = $item->range1?$item->range1->name:'未知';
            $row['city_id'] = $item->city1?$item->city1->name:'';
            $row['county_id'] = $item->district1?$item->district1->name:'';
            $row['upload_date'] = $item->submit_date;
            $row['send_address'] = $item->send_address1;
            $row['send_remark'] = $item->remark;
            $row['sale_name'] = $item->user?$item->user->getShowName():'';
            $row['op_name'] = $item->lock?$item->lock->getShowName():'';
            $row['car_no'] = $item->car_no;
            $row['car_model'] = $item->car->car_model;
            $row['total6'] = $item->total_all;
            $row['total8'] = $item->total_real;
            $row['status'] = $item->status->name;

            $data[] = $row;
        }
        $result['msg'] = '操作成功';
        $result['data'] = $data;
        $result['total'] = $total;

        return $result;
    }

    public function actionOrderInfo()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '无任何记录';

        $assign_id = $request->get('assign_id',0);
        $order_info = OrderT::findOne(['id'=>$assign_id]);
        $car_info = $order_info->car;
        $gift_items = $order_info->gifts;

        $data = array();
        $data['id'] = $order_info->id;
        $data['car_no'] = $order_info->car_no;
        $data['upload_date'] = $order_info->submit_date;
        $data['send_date'] = $order_info->send_date;
        $data['insurer_name'] = $order_info->id_man;
        $data['insurer_date'] = $order_info->insurer1_begin_date;
        $data['insurer_company'] = $order_info->company?$order_info->company->name:'';
        $data['pay_no'] = $order_info->pay_no;
        $data['car_model'] = $car_info->car_model;
        $data['link_man'] = $order_info->link_man;
        $data['link_tel'] = $order_info->link_phone;
        $data['total4'] = round($order_info->total2,2);
        $data['total5'] = round($order_info->total3,2);
        $data['total3'] = round($order_info->total1,2);
        $data['total6'] = round($order_info->total_all,2);
        $data['total8'] = round($order_info->total_real,2);
        $data['total9'] = round($order_info->total1_dis,2);
        $data['sale_man'] = $order_info->user?$order_info->user->getShowName():'';
        $data['sale_tel'] = $order_info->user?$order_info->user->phone:'';
        $data['piao_header'] = $order_info->piao_header;
        $data['pay_type'] = $order_info->payType?$order_info->payType->name:'';
        $data['send_remark'] = $order_info->remark;
        $data['send_direction'] = $order_info->direction1_id;
        $data['direction_name'] = $order_info->direction1?$order_info->direction1->name:'';
        $data['send_area'] = $order_info->range1_id;
        $data['area_name'] = $order_info->range1?$order_info->range1->name:'';
        $data['city'] = $order_info->city1?$order_info->city1->name:'';
        $data['county'] = $order_info->district1?$order_info->district1->name:'';
        $data['send_address'] = $order_info->send_address1;
        $data['send_address2'] = $order_info->send_address2;

        $result['data'] = $data;
        $result['msg'] = '操作成功';

        return $result;
    }

    public function actionSaveOrderInfo()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $address = $request->get('address');
        $address2 = $request->get('address2');
        $send_date = $request->get('send_date');

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
            return $result;
        }
        $order_info->send_address1 = $address;
        $order_info->send_address2 = $address2;
        $order_info->send_date = $send_date;
        $order_info->save();
        $result['success'] = true;
        $result['msg'] = '保存成功';
        return $result;
    }

    public function actionReceiveOp()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $user_id = $request->get('user_id');

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
            return $result;
        }

        $op_status = 4;
        if($order_info->lock_id > 0) {
            $request['msg'] = '已经被领取';
            return $result;
        }
        $order_info->status_id = $op_status;
        $order_info->lock_id = $user_id;

        if(!$order_info->save()) {
            var_dump($order_info->errors);
            exit;
        }

        $result['success'] = true;
        $result['msg'] = '领取成功';
        return $result;
    }

    /**
     * 手机外勤-退回操作
     * @return array
     */
    public function actionBackOp()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $msg = $request->get('msg');
        $user_id = $request->get('user_id');

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
            return $result;
        }

        $status_id = 1;
        $back_op_status = $order_info->status_id;
        if($order_info->status_id == 1) {
            $result['success'] = false;
            $result['msg'] = '已操作,请不要重复操作。';
            return $result;
        }
        if($back_op_status > 0) {
            $tran = OrderT::getDb()->beginTransaction();
            try {
                $order_info->status_id = 1;
                $order_info->return_status_id = $back_op_status;
                $order_info->return_remark = $msg;
                $order_info->return_time = time();
                $order_info->lock_id = 0;
                $order_info->save();

                //操作记录
                $user_info = UserT::findOne(['id'=>$user_id]);
                $log_info = new CarLogT();
                $log_info->car_id = $order_info->car_id;
                $log_info->op_time = time();
                $log_info->op_man = $user_info->getShowName();
                $log_info->group_name = $user_info->group?$user_info->group->getPath():'';
                $log_info->type = 1;
                $log_info->remark = '退回业务员,理由是:'.$msg;
                $log_info->save();

                //处理操作人员状态
                $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'user_id'=>$user_id,'status'=>0]);
                if($order_user_info) {
                    $order_user_info->status = 2; //退回
                    $order_user_info->save();

                    $order_user_info2 = new OrderUserT();
                    $order_user_info2->user_id = $order_info->user_id;
                    $order_user_info2->order_id = $order_info->id;
                    $order_user_info2->type_id = 1;
                    $order_user_info2->status = 0;
                    $order_user_info2->save();
                }
                //
                //处理礼品数量
                $sel_gifts = OrderGiftT::find()
                    ->where('order_id='.$order_info->id)
                    ->all();
                foreach($sel_gifts as $sel_gift) {
                    $gift_info = $sel_gift->gift;
                    if($gift_info && $gift_info->total > -1) {
                        $gift_info->total = $gift_info->total + 1;
                        $gift_info->save();
                    }
                }

                $result['success'] = true;
                $result['msg'] = '退回成功';

                $tran->commit();
            } catch (\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }

        return $result;
    }

    public function actionMyOrdersList()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '无任何记录';

        $user_id = $request->get('user_id');

        $query = OrderT::find()
            ->orderBy('id asc')
            ->where('status_id=4')
            ->andWhere('lock_id='.$user_id);
        $total = $query->count();
        $items = $query->all();
        $data = array();
        foreach($items as $item) {
            $row = $item->toArray();
            $row['insurer_name'] = $item->id_man;
            $row['link_tel'] = $item->link_phone;
            $row['send_date'] = $item->send_date;
            $row['send_direction'] = $item->direction1_id;
            $row['direction_name'] = $item->direction1?$item->direction1->name:'';
            $row['send_area'] = $item->range1_id;
            $row['area_name'] = $item->range1?$item->range1->name:'';
            $row['city_id'] = $item->city1?$item->city1->name:'';
            $row['county_id'] = $item->district1?$item->district1->name:'';
            $row['upload_date'] = $item->submit_date;
            $row['send_address'] = $item->send_address1;
            $row['send_remark'] = $item->remark;
            $row['sale_name'] = $item->user?$item->user->getShowName():'';
            $row['op_name'] = $item->lock?$item->lock->getShowName():'';
            $row['car_no'] = $item->car_no;
            $row['car_model'] = $item->car->car_model;
            $row['total6'] = $item->total_all;
            $row['total8'] = $item->total_real;
            $row['status'] = $item->status->name;

            $data[] = $row;
        }
        $result['msg'] = '操作成功';
        $result['data'] = $data;
        $result['total'] = $total;

        return $result;
    }

    /**
     * 手机外勤-放弃操作
     * @return array
     */
    public function actionCancelOp()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $user_id = $request->get('user_id');

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
            return $result;
        }

        $tran = OrderT::getDb()->beginTransaction();
        try {
            $op_status = 4;
            $order_info->status_id = $op_status;
            $order_info->lock_id = 0;
            $order_info->save();

            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'status'=>0]);
            if($order_user_info) {
                $order_user_info->status = 4; //放弃操作
                $order_user_info->save();
            }

            $tran->commit();
        } catch (\Exception $e) {
            $tran->rollBack();
            throw $e;
        }

        $result['success'] = true;
        $result['msg'] = '放弃成功';
        return $result;
    }

    public function actionReadCardinfo()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');

        $card_info = CardinfoT::findOne(['order_id'=>$assign_id]);
        if(!isset($card_info)) {
            $card_info = new CardinfoT();
            $card_info->order_id = $assign_id;
            $card_info->save();
        }

        $result['success'] = true;
        $result['msg'] = '保存成功';
        $result['cardInfo'] = $card_info->toArray();
        return $result;
    }

    public function actionSaveCardinfo()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $cardnumber = $request->get('cardnumber');
        $address = $request->get('address');
        $name = $request->get('name');
        $nation = $request->get('nation');
        $issuingauthority = $request->get('issuingauthority');
        $validstartdate = substr($request->get('validstartdate'),0,10);
        $validenddate = substr($request->get('validenddate'),0,10);
        $phone = $request->get('phone');
        $samcode = '05.03-20150203-0002909763-0846164155';
        $pingtai = 1;
        $channels = 23;
        $user = 'picc';
        $password = 'picc';

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
           return $result;
        }
        $order_info->link_phone = $phone;
        $order_info->save();

        //获取samcode
        if($order_info->company) {
            $samcode = $order_info->company->sam;
        }
        $card_info = CardinfoT::findOne(['order_id'=>$assign_id]);
        if(!isset($card_info)) {
            $card_info = new CardinfoT();
            $card_info->order_id = $assign_id;
        }
        $card_info->cardnumber = $cardnumber;
        $card_info->address = $address;
        $card_info->name = $name;
        $card_info->nation = $nation;
        $card_info->issuingauthority = $issuingauthority;
        $card_info->validstartdate = $validstartdate;
        $card_info->validenddate = $validenddate;
        $card_info->phone = $phone;
        $card_info->samcode = $samcode;
        $card_info->pingtai = $pingtai;
        $card_info->channels = $channels;
        $card_info->user = $user;
        $card_info->password = $password;

        if(!$card_info->save()) {
//            echo 'error';
            var_dump($card_info->errors);
            exit;
        }

        $login_url = 'http://xftongnian.com/cj/web/login';
        $sms_url = 'http://xftongnian.com/cj/web/doSaveNew';

        $params = $card_info->toArray();
        unset($params['id']);
        unset($params['order_id']);
        unset($params['created_at']);
        unset($params['updated_at']);
        $params['samCode'] = $params['samcode'];
        unset($params['samcode']);
        $param_str = http_build_query($params);
        $content = MyLib::Post('name=gkcj&pwd=gkcj0824',$login_url,false);
        $content = MyLib::Post($param_str,$sms_url,true);

        $obj = json_decode($content);
        if($obj->status) {
            $result['success'] = true;
            $result['msg'] = '发送成功';
        } else {
            $result['success'] = false;
            $result['msg'] = $obj->msg;
        }

        return $result;
    }

    /**
     * 手机外勤提交操作
     * @return array
     * @throws \Exception
     */
//    public function actionSubmitOp()
//    {
//        Yii::$app->response->format = Response::FORMAT_JSON;
//        $request = Yii::$app->request;
//        $result = array();
//        $result['success'] = false;
//        $result['msg'] = '操作失败';
//
//        $assign_id = $request->get('assign_id');
//        $sms_code = $request->get('sms_code');
//
//        $order_info = OrderT::findOne(['id'=>$assign_id]);
//        if(!isset($order_info)) {
//            return $result;
//        }
//
//        $tran = OrderT::getDb()->beginTransaction();
//        try {
//            $old_status_id = $order_info->status_id;
//            $user_id = $order_info->lock_id;
//            $order_info->status_id = 5;
//            $order_info->lock_id = 0;
//            $order_info->sms_code = $sms_code;
//            $order_info->send_return_type = '验证码';
//            $order_info->save();
//
//            $send_info = new SendLogT();
//            $send_info->car_id = $order_info->car_id;
//            $send_info->order_id = $order_info->id;
//            $send_info->user_id = $user_id;
//            $send_info->send_time = time();
//            $send_info->save();
//
//            //操作记录
//            $user_info = UserT::findOne(['id'=>$user_id]);
//            $log_info = new CarLogT();
//            $log_info->car_id = $order_info->car_id;
//            $log_info->op_time = time();
//            $log_info->op_man = $user_info->getShowName();
//            $log_info->group_name = $user_info->group?$user_info->group->getPath():'';
//            $log_info->type = 1;
//            $log_info->remark = '外勤操作完毕,提交正本单处理。';
//            $log_info->save();
//
//            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'user_id'=>$user_id,'type_id'=>$old_status_id,'status'=>0]);
//            if($order_user_info) {
//                $order_user_info->status = 1; //操作结束
//                $order_user_info->save();
//            }
//
//            $tran->commit();
//        } catch (\Exception $e) {
//            $tran->rollBack();
//            throw $e;
//        }
//
//        $result['success'] = true;
//        $result['msg'] = '保存成功';
//        return $result;
//    }

//    public function actionSubmitOp()
//    {
//        Yii::$app->response->format = Response::FORMAT_JSON;
//        $request = Yii::$app->request;
//        $result = array();
//        $result['success'] = false;
//        $result['msg'] = '操作失败';
//
//        $assign_id = $request->get('assign_id');
//        $sms_code = $request->get('sms_code');
//        $payee_id = $request->get('payee_id');
//        $payment_id = $request->get('payment_id');
//
//        $order_info = OrderT::findOne(['id'=>$assign_id]);
//        if(!isset($order_info)) {
//            return $result;
//        }
//
//        $tran = OrderT::getDb()->beginTransaction();
//        try {
//            $old_status_id = $order_info->status_id;
//            $user_id = $order_info->lock_id;
//            $order_info->status_id = 5;
//            $order_info->lock_id = 0;
//            $order_info->sms_code = $sms_code;
//            $order_info->send_return_type = '验证码';
//            if($payee_id > 0)
//                $order_info->payee_id = $payee_id;
//            if($payment_id > 0)
//                $order_info->payment_id = $payment_id;
//            $order_info->save();
//
//            $send_info = new SendLogT();
//            $send_info->car_id = $order_info->car_id;
//            $send_info->order_id = $order_info->id;
//            $send_info->user_id = $user_id;
//            $send_info->send_time = time();
//            $send_info->save();
//
//            //操作记录
//            $user_info = UserT::findOne(['id'=>$user_id]);
//            $log_info = new CarLogT();
//            $log_info->car_id = $order_info->car_id;
//            $log_info->op_time = time();
//            $log_info->op_man = $user_info->getShowName();
//            $log_info->group_name = $user_info->group?$user_info->group->getPath():'';
//            $log_info->type = 1;
//            $log_info->remark = '外勤操作完毕,提交正本单处理。';
//            $log_info->save();
//
//            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'user_id'=>$user_id,'type_id'=>$old_status_id,'status'=>0]);
//            if($order_user_info) {
//                $order_user_info->status = 1; //操作结束
//                $order_user_info->save();
//            }
//
//            $tran->commit();
//        } catch (\Exception $e) {
//            $tran->rollBack();
//            throw $e;
//        }
//
//        $result['success'] = true;
//        $result['msg'] = '保存成功';
//        return $result;
//    }
    public function actionSubmitOp()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败';

        $assign_id = $request->get('assign_id');
        $sms_code = $request->get('sms_code');
        $payee_id = $request->get('payee_id');
        $payment_id = $request->get('payment_id');

        $order_info = OrderT::findOne(['id'=>$assign_id]);
        if(!isset($order_info)) {
            return $result;
        }

        $tran = OrderT::getDb()->beginTransaction();
        try {
            $old_status_id = $order_info->status_id;
            $user_id = $order_info->lock_id;
            $order_info->status_id = 5;
            $order_info->lock_id = 0;
            $order_info->sms_code = $sms_code;
            $order_info->send_return_type = '验证码';
            switch($payee_id) {
                case 1:
                    $order_info->payee_id = 8;
                    break;
                case 2:
                    $order_info->payee_id = 9;
                    break;
            }
            switch($payment_id) {
                case 1:
                    $order_info->payment_id = 8;
                    break;
                case 2:
                    $order_info->payment_id = 9;
                    break;
                case 3:
                    $order_info->payment_id = 10;
                    break;
                case 4:
                    $order_info->payment_id = 11;
                    break;
                case 5:
                    $order_info->payment_id = 12;
                    break;
            }
            $order_info->save();

            $send_info = new SendLogT();
            $send_info->car_id = $order_info->car_id;
            $send_info->order_id = $order_info->id;
            $send_info->user_id = $user_id;
            $send_info->send_time = time();
            $send_info->save();

            //操作记录
            $user_info = UserT::findOne(['id'=>$user_id]);
            $log_info = new CarLogT();
            $log_info->car_id = $order_info->car_id;
            $log_info->op_time = time();
            $log_info->op_man = $user_info->getShowName();
            $log_info->group_name = $user_info->group?$user_info->group->getPath():'';
            $log_info->type = 1;
            $log_info->remark = '外勤操作完毕,提交正本单处理。';
            $log_info->save();

            $order_user_info = OrderUserT::findOne(['order_id'=>$order_info->id,'user_id'=>$user_id,'type_id'=>$old_status_id,'status'=>0]);
            if($order_user_info) {
                $order_user_info->status = 1; //操作结束
                $order_user_info->save();
            }

            $tran->commit();
        } catch (\Exception $e) {
            $tran->rollBack();
            throw $e;
        }

        $result['success'] = true;
        $result['msg'] = '保存成功';
        return $result;
    }
    public function actionUpload()
    {
        require Yii::$app->getBasePath().'/../common/libs/aliyun/Util/Autoloader.php';

        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $assign_id = $request->post('assign_id');
        $type = $request->post('type');
        $result = array();
        $result['success'] = false;
        $result['msg'] = '操作失败'.print_r($_POST,true);

        $file = UploadedFile::getInstanceByName('pic');
        if(!empty($file)) {
            $name = $assign_id.'_'.$type.date('YmdHis').rand(100,200);
            $ext = $file->extension;
            $filename = $name.'.'.$ext;
            $destination = '/idnum';
            $web_path = Yii::$app->runtimePath;
            if(!is_dir($web_path.$destination)) {
                mkdir($web_path.$destination);
            }
            $destination .='/'.date('Ymd');
            if(!is_dir($web_path.$destination))
                mkdir($web_path.$destination);
            $destination .= '/'.$filename;
            $file->saveAs($web_path.$destination);
            $order_info = OrderT::findOne(['id'=>$assign_id]);
            if(isset($order_info)) {
                if($type == 'face') {
                    $order_info->photo_face = $destination;
                } else {
                    $order_info->photo_back = $destination;
                }
                $order_info->save();
            }

            $content = file_get_contents($web_path.$destination);
            $data = base64_encode($content);

            $appKey = "24655673";//"23481440";
            $appSecret = "d4807c9befff41979d2652f1c2ef3535";//"5749df13ff0c16bb9080b09d34a5a31a";
            $host = "https://dm-51.data.aliyun.com";
            $path = '/rest/160601/ocr/ocr_idcard.json';
            $request = new \HttpRequest($host, $path, \HttpMethod::POST, $appKey, $appSecret);

            $params = array();
            $params['inputs'][0]['image']['dataType'] = 50;
            $params['inputs'][0]['image']['dataValue'] = $data;
            $params['inputs'][0]['configure']['dataType'] = 50;
            $params['inputs'][0]['configure']['dataValue'] = Json::encode(array('side'=>$type));
            $bodyContent = Json::encode($params);

            $request->setHeader(\HttpHeader::HTTP_HEADER_CONTENT_TYPE, \ContentType::CONTENT_TYPE_JSON);
            $request->setHeader(\HttpHeader::HTTP_HEADER_ACCEPT, \ContentType::CONTENT_TYPE_JSON);
            $request->setHeader(\HttpHeader::HTTP_HEADER_CONTENT_MD5, base64_encode(md5($bodyContent, true)));
            $request->setBodyString($bodyContent);
            $request->setSignHeader(\SystemHeader::X_CA_TIMESTAMP);
            $request->setSignHeader("a-header1");
            $request->setSignHeader("b-header2");

            $response = \HttpClient::execute($request);
            $body = $response->getBody();
            $obj = Json::decode($body);
            $info = Json::decode($obj['outputs'][0]['outputValue']['dataValue']);
            $info['config'] = Json::decode($info['config_str']);
            if($info['config']['side'] == 'back') {
                $info['start_date'] = substr($info['start_date'],0,4).'-'.substr($info['start_date'],4,2).'-'.substr($info['start_date'],-2);
                file_put_contents('./a.txt',print_r($info,true));
                if(strlen($info['end_date']) >= 8) {
                    $info['end_date'] = substr($info['end_date'],0,4).'-'.substr($info['end_date'],4,2).'-'.substr($info['end_date'],-2);
                } else {
                    $info['end_date'] = sprintf("%4d",substr($info['start_date'],0,4)+20).substr($info['start_date'],4);
                }
            }
            $result['success'] = true;
            $result['obj'] = $info;
            $result['msg'] = '操作成功';
        }

        return $result;
    }
}
