<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2013.11.19
*/

class control extends adminbase {

	
    public function action_run() {
        $searchsql = " AND status not in ('待付款')";
        $model = parent::model('order', 'am');
        list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
     //   print_r($data);
        //die;
        $url = XRequest::getPhpSelf();
        $url .= '?c=order';
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::admin($total, $this->pagesize, $this->page, $url, 10),
            'data'=>$data,
        );
        unset($model);
		TPL::assign($var_array);
		TPL::display($this->cptpl.'order.tpl');
	}
    
    
    public function action_add() {
        TPL::assign($var_array);
        TPL::display($this->cptpl.'htmllabel.tpl'); 
    }
    
    
    public function action_edit() {
 
        $id = XRequest::getInt('id');
        $this->_validID($id);
        $model = parent::model('product', 'im');
        $data = $model->getorderinfo($id);
        unset($model);
        if (empty($data)) {
            XHandle::halt('对不起，读取数据不存在！', '', 1);
        }
        else {
            $var_array = array(
                'data'=>$data,
                'id'=>$id,
                'page'=>$this->page,
            );
            TPL::assign($var_array);
            TPL::display($this->cptpl.'order.tpl');
        }
    }
    
    
    public function action_saveadd() {
       
        $args = $this->_validAdd();
        $model = parent::model('htmllabel', 'am');
        if (true === $model->doCheckLabel($args['labelname'])) {
            XHandle::halt('对不起，该标签名已存在，请填写另外一个。', '', 1);
        }
        else {
            $result = $model->doAdd($args);
            if (true === $result) {
                $this->log('htmllabel_add', '', 1);
                XHandle::halt('添加成功', $this->cpfile.'?c=htmllabel', 0);
            }
            else {
                $this->log('htmllabel_add', '', 0);
                XHandle::halt('添加失败', '', 1);
            }
        }
        unset($model);	
	}
    
    
    public function action_saveedit() {
            
        $id=$_REQUEST['id'];
        if($id==''){
             XHandle::halt('ID丢失，请正确操作', '', 1);
        }
        $coompany=$_REQUEST['company'];
        if($coompany==''){
            XHandle::halt('物流公司不能为空', '', 1);
        }
        $wuliunum=$_REQUEST['wuliunum'];
        if($wuliunum==''){
            XHandle::halt('物流单号不能为空', '', 1);
        }
        $args['company']=$coompany;
        $args['wuliunum']=$wuliunum;
        $args['status']='待收货';
        $model = parent::model('product', 'im');
        $result = $model->doEdit($id, $args);
        
        if (true === $result) {
            $this->log('order_edit', "id={$id}", 1);
            XHandle::halt('编辑成功', '', 0);
        }
        else {
            $this->log('htmllabel_edit', "id={$id}", 0);
            XHandle::halt('编辑失败', '', 1);
        }
        unset($model);
    }
    
    
    public function action_del() {
      
        $array_id = XRequest::getArray('id');
        $this->_validID($array_id);
        $model = parent::model('htmllabel', 'am');
        for($ii=0; $ii<count($array_id); $ii++){
            $id = intval($array_id[$ii]);
            if (true === $model->doCheckSystemLabel($id)){
                XHandle::halt('对不起，不能删除系统默认的标签。', '', 1);
            }
            $result = $model->doDel($id);            
        }
        unset($model);
        
        if (true === $result) {
            $this->log('htmllabel_del', "id={$array_id}", 1);
            XHandle::halt('删除成功', $this->cpfile.'?c=htmllabel', 0);
        }
        else {
            $this->log('htmllabel_del', "id={$array_id}", 0);
            XHandle::halt('删除失败', '', 1);
        }
         
    }
    
    
    public function action_update() {
   
        $args = XRequest::getGpc(array('id', 'type'));
        $model = parent::model('htmllabel', 'am');
        $model->doUpdate(intval($args['id']), trim($args['type']));
        unset($model);
    }
    
    
    private function _validAdd() {
        $args = XRequest::getGpc(array('labelname', 'description', 'flag'));
        $content = XRequest::getArgs('content', '', false);
        if (empty($args['labelname'])) {
            XHandle::halt('标签名不能为空', '', 1);
        }
        else {
            if (false === XValid::isLabel($args['labelname'])) {
                XHandle::halt('标签名格式不正确', '', 1);
            }
        }
        $args['flag'] = intval($args['flag']);
        $args = array_merge($args, array('content'=>$content));
        return $args;
    }
    
    
    private function _validEdit() {
        $id = XRequest::getInt('id');
        $description = XRequest::getArgs('description');
        $content = XRequest::getArgs('content', '', false);
        $flag = XRequest::getInt('flag');
        return array($id, array('description'=>$description, 'content'=>$content, 'flag'=>$flag));
    }
    
    
    private function _validID($id) {
        if (empty($id)) {
            XHandle::halt('ID丢失，请选择要操作的ID', '', 1);
        }
    }
         public function action_ajax_get_action()
     {     
     	$advContrl = get_class_methods('control');
   
        
     	$html = '';
     	foreach ($advContrl as $val){
            if(strpos($val,'action') !== false){
                   
               $html .= "<option value='".$val."'>".$val."</option>";
                    
            } else {
               
            }
     		
     	}
     	exit($html);
     }


    public function action_tixian() {

   //     $searchsql = " 1=1";
        $model = parent::model('order', 'am');
        list($total, $data) = $model->gettiList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
        //   print_r($data);
        //die;
        $url = XRequest::getPhpSelf();
        $url .= '?c=order';
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::admin($total, $this->pagesize, $this->page, $url, 10),
            'data'=>$data,
        );
        unset($model);
        TPL::assign($var_array);
        TPL::display($this->cptpl.'tixian.tpl');
    }
    public function action_ajaxti(){
        $ordernum=$_REQUEST['order_number'];
        $model = parent::model('order', 'am');
        $data = $model->getdata($ordernum);
        $payee_account='15830307910';
        $amount='0.1';
        require_once("source/plugin/alipayti/aop/AopClient.php");
        require_once("source/plugin/alipayti/aop/request/AlipayFundTransToaccountTransferRequest.php");
        require_once("source/plugin/alipayti/aop/SignData.php");
        $remark = '用户余额提现到支付宝';
        $aop = new \AopClient();
        $aop->gatewayUrl =  'https://openapi.alipay.com/gateway.do';//支付宝网关 https://openapi.alipay.com/gateway.do这个是不变的
        $aop->appId = '2016091800542510';//商户appid 在支付宝控制台找
    //    $aop->rsaPrivateKey = "MIIEogIBAAKCAQEAxgKtWXyntAP56CSp92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MIIBCgKCAQEAxgKtWXyntAP56CSp92UD+5wr58MJhpWj81OK1Y55t8bgyWv07oENnKtu8Qb6NMOBjIarWkKp9sDgg3vpiEhFO9EKbp+LOGMVVgnzJ4ulv3KJjFjpi8zViwcKqFJ1Ymn5BNRSHfAxAm1z5z335mQNV5LRIKG3rpxcnDQCE4rxoxIHUN694UvjPOZdJo46qVU0dBMEm91HE1MsM+skz41dfcMoo4lZqVs4pff06303JfytimrVSjhQJv6D8zZymDAhk967tlq4yAArEg5SiPRljEnt0L0c7W2h0XgKE0Usw+5a6itpZXGY8kdB6DbUbcnOpdFqi6VPfgvdgQw+0My0YwIDAQAB";//支付宝公钥 上传应用公钥后 支付宝生成的支付宝公钥
        $aop->alipayrsaPublicKey= "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn7ErgmIvcWCtqxvB9CagdA5fieP/XjtsHoHuvJI5MKl73+eOqd5paEA2y/qfRI3iEe8cr7WIpfz1lOQ5u+S4qpZ/dA0oRhjgkwsZSoK9gWtKepRzXtS2U+lZs3yek+u1yStTkdJX0aB9JAsBULmku94KSNoMT33NQJhZS5j+9WlW7SeXdIwwzOIJ8jSqY1QcDaTkS/Y/9w51ndw9OqAX3YSWXS3ZWtoYd8zby+9rKYQIvG1Eq1HL8y34L8kBuciWyLOB6bJGUS9f2pTNbTEkSrWjircjrIe+kh8Yq1FLRW05Zp87DPanQo0jbvEWfvj3WEEMrfNMMn1flPUdC6y7KwIDAQAB";
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new \AlipayFundTransToaccountTransferRequest();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$ordernum\"," .
            "\"payee_type\":\"ALIPAY_LOGONID\"," .
            "\"payee_account\":\"$payee_account\"," .
            "\"amount\":\"$amount\"," .
            "\"remark\":\"$remark\"" .
            "}");

        $result = $aop->execute ($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;

        if(!empty($resultCode)&&$resultCode == 10000){
            print_r($resultCode);
            //提现成功以后 更新表状态
            //并且记录 流水等等
            return true;
        } else {
            print_r($resultCode);
            //$result->$responseNode->sub_msg 这个参数 是返回的错误信息
            //throw new Exception($result->$responseNode->sub_msg);
        }
        //公共请求参数
//        $pub_params = [
//            'app_id'    => '2016091800542510',
//            'method'    =>  'alipay.fund.trans.toaccount.transfer', //接口名称 应填写固定值alipay.fund.trans.toaccount.transfer
//            'format'    =>  'JSON', //目前仅支持JSON
//            'charset'    =>  'UTF-8',
//            'sign_type'    =>  'RSA2',//签名方式
//            'sign'    =>  '', //签名
//            'timestamp'    => date('Y-m-d H:i:s'), //发送时间 格式0000-00-00 00:00:00
//            'version'    =>  '1.0', //固定为1.0
//            'biz_content'    =>  '', //业务请求参数的集合
//        ];
//
////请求参数
//        $api_params = [
//            'out_biz_no'  => date('YmdHis'),//商户转账订单号
//            'payee_type'  => 'ALIPAY_LOGONID', //收款方账户类型
//            'payee_account'  => $data['payee_account'], //收款方账户
//            'amount'  => $data['amount'], //金额
//        ];
////请求参数转换为json后和业务参数合并
//        $pub_params['biz_content'] = json_encode($api_params,JSON_UNESCAPED_UNICODE);
//        print_r($data);
    }
}
?>
