<?php
/**
 * 美团外卖订单管理
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Common\Vendor\Threeorder;
class Meituan extends \Common\Vendor\Threeorder{
    private $config=array(
        'appkey'=>'101663',
        'appsecret'=>'4tj5pgm7ux5rpf4x',
    );
    private $is_sandbox= true;//是否为测试
    private $connectUrl="https://api.open.cater.meituan.com";//平台的正式接口地址;
    public function __construct($config=array()) {
        if($config){
            $this->config=array_merge($this->config,$config);
        }
        if( $this->is_sandbox){
             $this->connectUrl="https://openo2o.jd.com/mockapi";//平台的测试接口地址;
        }
        $this->class_name='meituan';
    }
    public function setConfig($venderId=''){
        if(is_array($venderId)){
            $this->config=array_merge($this->config,$venderId);
        }else{
            $config=$this->orderconfig($venderId);
            $this->config=array_merge($this->config,$config);
        }
    }
    //调取商家信息
    public function getshopinfo(){
        $return=array('status'=>0,'info'=>'');
        return $return;
    }
    /**
     * 订单已送达
     * @param $request array (ordersn =>订单号 operator 操作人姓名)
     */
    public function orderend($request=''){
        
    }
    /**
     * 订单第三方配送转商家自送
     * @param $request array (ordersn =>订单号 operator 操作人姓名)
     */
    public function orderdeliverytoself($request=''){
        
    }
    /**
     * 商家订单发货
     * @param $request array (ordersn =>订单号 status=>1第三方 0自有配送 operator 操作人姓名)
     */
    public function orderdelivery($request=''){
        
    }
    /**
     * 确认退款/驳回退款
     * @param $request array (ordersn =>订单号 status=> 1确认 0驳回 operator 操作人姓名 reason 确认退款详情)
     */
    public function orderrefund($request=''){
        
    }
    
    /**
     * 确认接单/不接单
     * @param $request array (ordersn =>订单号 status=> 1接单 0 不接单 operator 操作人姓名)
     */
    public function orderconfirm($request=''){
        
    }
    /**
     * 订单列表
     * @param $request array (ordersn =>订单号)
     */
    public function orderlist($request=''){
        $return=array('status'=>0,'info'=>'');
        return $return;
    }
    /**
     * 查看订单详情
     * @param $orderNo 订单号
     */
    public function orderdetail($orderNo){
        $return=array('status'=>0,'info'=>'');
        if(empty($orderNo)){$return['info']='请填写订单号';return $return;}
        return $return;
    }
    /**
     * 响应回调入口文件
     */
    public function notify($postdata=''){
        if(empty($postdata))$postdata=I('post.');
        $get=I('get.');
        $request=$this->format_param($postdata);
        $log=var_export($request,1);
        $log.=var_export($get,1);
        //file_put_contents('uploads/meituan'.NOW_TIME.'.txt',$log);
        /*if($get['djsw']){//订单状态回调
           return $this->change_order_status($get['djsw'],$request);
        }*/
        $this->saveconfig($request);
        $return=array('data'=>'ok');
        $this->ajaxReturn($return);
    }
    //请求服务端
    public function execute($path,$request) {
		$apiParams = array();
		$apiParams['app_id']     = $this->config['appkey'];
		$apiParams['timestamp']  = NOW_TIME;
        if($request){
            $apiParams=array_merge($apiParams,$request);
        }
		$apiParams["sig"] 		 = $this->generateSign($apiParams);
		$requestUrl = $this->connectUrl.$path;print_r($apiParams);
		$resp = curlget($requestUrl,$apiParams,'POST');
        $result= json_decode($resp, true);
        return $result;
	}     
    //生成签名
    protected function generateSign($params){
        $stringToBeSigned = $this->paramsToString($params);
		return md5($stringToBeSigned);
	}
    //验签
	protected function verifySign($params){
        $stringToBeSigned = $this->paramsToString($params);
        $sign = $params["sign"];
        if(!empty($sign)){return $sign == md5($stringToBeSigned);}
        return false;
	}
    //组合待签名字符
    protected function paramsToString($params){
		ksort($params);
		$sortedString = format_array2http($params);
        $sortedString .= $this->config['appsecret'];
        return $sortedString;
    }
    //保存配置文件
	private function saveconfig($param){
		if(empty($param) || (empty($param['token']['venderId']) && empty($param['code']['venderId'])))return false;
        $venderId=$param['token']['venderId']?$param['token']['venderId']:$param['code']['venderId'];
        if($param['token'] && $param['code'])$value=array_merge($param['token'],$param['code']);
        else $value=$param['token']?$param['token']:$param['code'];
        return $this->orderconfig($venderId,$value);
	}
}
?>