<?php
/**
 * AJAX数据获取
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Home\Controller;
class AjaxController extends BaseController{
    public function index(){exit;}
    public function exportplu(){
        $getdata=I('get.');
        $admininfo=session('admin_info');
        if(empty($admininfo)){
            if($getdata['is_root']==1){
                $res=$this->checkrootauth($getdata);
            }else{
                $res=$this->checkshopauth($getdata);
            }
            if($res['status']==3){
                $this->ajaxReturn($res);return;
            }
        }else{
           header("content-Type: text/html; charset=gb2312");
           header("Content-type:application/octet-stream");
           header("Content-Disposition:attachment;filename=".'plu'.date("YmdHis").".txt");
        }
        $map=array();
        $map['is_type']=1;
        $map['status']=1;
        $map['publish_wait']=1;
        $map['is_delete']=0;
        $list=M('Goods')->where($map)->field('id,goods_sn,goods_name,shop_price,goods_plu')->order('goods_plu=0 asc,goods_plu,id')->select();
        $old_plu=array();
        $empty_plu=array();
        foreach($list as $k=>$v){
            if($v['goods_plu'])$old_plu[]=$v['goods_plu']; else $empty_plu[]=$k;
        }
        if($old_plu && $empty_plu){$i=0;
            foreach($empty_plu as $v){
                $nextplu=getnexnum($i,$old_plu);
                $old_plu[]=$nextplu;
                $list[$v]['goods_plu']=$nextplu;
            }
        }
        D('Pad/Cashier')->export_plu($list,$getdata['type']);
    }
    private function checkshopauth($paramdata){
        $return=array('status'=>3,'msg'=>'获取登录信息错误,请重新登录!(001)');
        $paramdata['sid']=intval($paramdata['sid']);

        if(empty($paramdata['sid']) || empty($paramdata['token'])){return $return;}
        $accountinfo=idtoname($paramdata['sid'],'ShopsAccount','*','id',false);
        $appkeyarr=explode('.',$this->AppKey);
        $timestamp=$appkeyarr[1];
        if(empty($accountinfo['password']) || $accountinfo['password']!=$paramdata['token']){return $return;}
        $return['status']=1;
        return $return;
    }
    public function faceurl(){
        $url='https://ccc.aliyun.com/workbench/hanchao';
        echo '<iframe src="'.$url.'" width="800px" height="800px"></iframe>';
    }
    private function checkrootauth($paramdata){
        $return=array('status'=>3,'msg'=>'获取登录信息错误,请重新登录!(002)');
        $paramdata['root_id']=intval($paramdata['root_id']);
        if(empty($paramdata['root_id']) || empty($paramdata['root_token'])){return $return;}
        $admininfo=idtoname($paramdata['root_id'],'Admin','*','id',false);  
        $appkeyarr=explode('.',$this->AppKey);
        $timestamp=$appkeyarr[1];
        if(empty($admininfo['password']) || $admininfo['password']!=$paramdata['root_token']){return $return;}
        $return['status']=1;
        return $return;
    }
    //发送验证短信
    public function sendsms(){
        $postdata=I('post.');
        $backdata=D('Ajax')->sendsms($postdata);
        $this->ajaxReturn($backdata);
   }
   public function sendsms_email(){
		$postdata=I('post.');
        $backdata=D('Ajax')->sendsms_email($postdata);
        $this->ajaxReturn($backdata);
   }
   public function msg_sender(){//自动发送列表消息
        D('Ajax')->msg_sender();
   }
   public function getarearegion(){//调取地理区域缓存
        $return['status'] = 0;$id = intval(I('post.id'));$tabid = intval(I('post.tabid'));$parents = I('post.parents');
        if($id<0)$this->ajaxReturn($return);
        $result = D('Common/Area')->change_areacache($id,$tabid,$parents);//改变地区信息
        if($result)$this->ajaxReturn($result);
   }
    public function clearhistory(){//清空历史
        $result = D('Common/Ajax')->clearhistory();//改变地区信息
        $this->ajaxReturn($result);
    }
    //查询快递
    public function express(){
        $member_info=session('member_info');
        $admin_info=session('admin_info');
        $shop_info=session('shops_info');
        $supplier_info=session('suppliers_info');
        if(empty($member_info) && empty($admin_info) && empty($shop_info)&& empty($supplier_info)){$this->error('您还没有登录不能查询快递');exit;}
        $id=I('id','','intval');
        $code=I('code','','trim');
        $sn=I('sn','','trim');
        if(empty($id) && empty($code) && empty($sn)){$this->error('参数错误');exit;}
        $map=array();
        if($id)$map['id']=$id;
        $map['status']=1;
        $kuaidi=M('Express')->order('id desc')->where($map)->getField('code');
        if(empty($kuaidi)){$this->error('快递查询接口不存在!');exit;}
        $express= new \Common\Vendor\Express($kuaidi);
        $params=array(
            'code'=>$code,
            'sn'=>$sn
        );
        $res=$express->process($params);
        if(str_exists($res,'http')){//url模式
            echo "<body><script>function open_link(url){var el = document.createElement('a');document.body.appendChild(el);el.href = url;el.target = '_blank';el.click();document.body.removeChild(el);};open_link('".$res."');$.weeboxs.close('hanchao_url_box');</script></body>";
        }else{//html模式
            echo $res;
        }
    }
	public function setgeo(){//设置定位
		$param=I('param.');
		$type=$param['type']?$param['type']:1;
	    $result = D('Common/Area')->geocoding($param,$type);//改变地区信息
        $this->ajaxReturn($result);
   }
   public function Cashgeo(){//存定位
	   $param=I('param.');
       $param['ypoint']=floatval($param['ypoint']);
       $param['xpoint']=floatval($param['xpoint']);
	   if(!$param['ypoint'] || !$param['xpoint']){
		  $result['status'] = 0; 
	   }else{
		   $type=$param['type']?$param['type']:1;
		   $result = D('Common/Area')->geocoding($param,$type);//改变地区信息`
           $Cashgeo=array(); 
           $Cashgeo['temp_name'] = $result['result']['pois']['0']['name'];
           $Cashgeo['province'] = idtoname($result['result']['addressComponent']['province'],'Area','id','name',false);
           $Cashgeo['city_id'] = idtoname($result['result']['addressComponent']['city'],'Area','id','name',false);
		   $Cashgeo['district'] = idtoname($result['result']['addressComponent']['district'],'Area','id','name',false);
		   $Cashgeo['temp_lat'] = $param['ypoint'];
		   $Cashgeo['temp_lng'] = $param['xpoint'];
		   $Cashgeo['city'] = $result['result']['addressComponent']['city'];
           
	       $siteinfo=session('siteinfo')?session('siteinfo'):cookie('siteinfo');
           $siteinfo['temp_lat']=floatval($siteinfo['temp_lat']);
           $siteinfo['temp_lng']=floatval($siteinfo['temp_lng']);
           if(!$siteinfo['temp_lat'] || !$siteinfo['temp_lng']){
                session('siteinfo',$Cashgeo);
    		    cookie('siteinfo',$Cashgeo);
                save_session();
           }
		   $result['status'] = 1;
           $result['reload'] = $Cashgeo['temp_name'];
	   }
	   $this->ajaxReturn($result);
   }
   public function getnear(){//获取周边路线
		$param=I('param.');
	    $result = D('Common/Area')->getnear($param);//改变地区信息
        $this->ajaxReturn($result);
   }
   public function call_notify(){
        $str = file_get_contents("php://input");
        //file_put_contents('uploads/PAY-s'.date("YmdHis").'info.txt',var_export($str,true));  
        $arr=explode('THIRDPARTYCALL:calleeNotifyCallInformation>',$str);

        $xmlstr='<item>'.substr($arr[1],0,-2).'</item>';

        $xml = xmlToArray($xmlstr);
        call_info($xml);
    }
    public function calls_notify(){
        $str = file_get_contents("php://input");
        //file_put_contents('uploads/PAY-z'.date("YmdHis").'info.txt',var_export($str,true));
    }
	
	public function clearlocation(){
        session('member_info',null);
		cookie('ExtensionId',null);
        echo '<meta name="viewport" content="user-scalable=no, width=device-width, maximum-scale=1.0">清空缓存成功';
    }
    public function reloadindex(){
        $param = I('param.');
        $map['is_default'] = 1;
        $map['is_wxsmall'] = 0;
        $content = M('Dragedit')->where($map)->cache('tpl_mobile_default')->getField('value');
        if($content){
            $flist = D('Common/Dragedit')->formatdiylist($content);
        }else{
            $flist='{}';
        }$shop_data=array();
        $siteinfo=tempcache('siteinfo');
        if(floatval($siteinfo['temp_lng'])>0){
            $ret = D('Wxsmall/Login')->bind_shop_param($siteinfo['temp_lng'],$siteinfo['temp_lat'],1);
            if($ret['status'] == 1){$shopids=array();
                foreach($ret['data'] as $k=>$v){$shopids[]=$v['id'];}
                $map=array();
                $map['status']=1;
                $map['id']=array('in',$shopids);
                $shopinfos=M('Shops')->where($map)->field('id,name')->select();
                $shop_data['count'] = count($shopinfos);
                $shop_data['list'] = $shopinfos;
            }else{
                $shop_data['count'] = 0;
            }
        }
        $main_shop_id = get_wxsmall_shop_id();
        $return = array(
            'status'=>1,
            'data'=>json_decode($flist,true),
            'main_shop_id'=>$main_shop_id,
            'shop_data'=>$shop_data,
        );
        $this->ajaxReturn($return);
    }
}
?>