<?php
namespace Mobile\Controller;
use Think\Controller;
use User\Api\SuserApi;

/**
* 首页
*/
class BaseController extends Controller {
    
    public function _empty(){
        
		$this->redirect('Index/index');
        
	}
    
	protected function _initialize(){

        $config = api('Config/lists');

        C($config); 
        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭,请稍后访问~');
        }
				
				if(!C('WAP_DISPLAY_STATUS')) {
					$this->display('Public/404');exit;
				}
				
				
				if(ACTION_NAME !='search_list') {session('search_referer',null);}
				
    }
    
    
	public function __construct() {
		parent::__construct();
		
				if(!C('WAP_DISPLAY_STATUS')) {
					$this->display('Public/404');exit;
				}
				
				
				define('DISPLAY_SITE',2);   /* 游戏中显示站点,即此游戏是否在此站显示 */
				
				
				$member = new SuserApi;
				
        $logininfo = $member->login_info();
				
				define('UID',$logininfo['user_id']);
				
        $user = D('User')->where(array('id'=>$logininfo['user_id']))->field("promote_id")->find();
        if($user && $user['promote_id'] > 0 ){
            define('PROMOTE_ID',1);
        }elseif($user){
            define('PROMOTE_ID',0);
        }else{
            define('PROMOTE_ID',-2);
        }
	}
    
    
    public function islogin() {  
		
				$member = new SuserApi;
				
				return $member->is_login();
				
    }

    /* 退出登录 */
    public function logout() {
			
        $member = new SuserApi;
        if($member->logout()>0) {
            $this->ajaxReturn(array('status' => 1, 'msg' => '退出成功!'));
        } else {
            $this->ajaxReturn(array('status' => 0, 'msg' => '已退出!'));
        }
				
    }
	
    public function lists($model = null, $p = 0,$extend = array(),$flag = false) {
        
        $model || $this->error('模型名标识必须!');
        
        $page = intval($p);
        
        $page = $page ? $page : 1; //默认显示第一页数据
        

        //获取模型信息
        
        $model = M('Model')->getByName($model);
        
        $model || $this->error('模型不存在!');
        
        $prefix = $model['prefix']?$model['prefix']:C("DB_PREFIX");
        
        $dbFields = M($model['name'],$prefix)->getDbFields();
        
        $fields = !empty($extend) && !empty($extend['fields']) ?$extend['fields']:array();
        
        if ($fields) {
            foreach ($fields as $k => $v) {
                if (!array_search($v,$dbFields)) {
                    unset($fields[$k]);
                }
            }
            $fields = array_unique(array_merge($fields,$dbFields));                
        } else {
            $fields = $dbFields;
        }
        
        // 条件搜索
        
        $map = !empty($extend) && !empty($extend['map']) ?$extend['map']:array();
        
        unset($_REQUEST['name']);
        
        foreach($_REQUEST as $name=>$val){
            
            if(in_array($name,$fields)){
                
                $map[$name]	= array('like',	'%'.$val.'%');
                
            }
            
        }
        
        $row    = !empty($extend) && !empty($extend['num']) ? $extend['num']:10;
        
        $name = parse_name(get_table_name($model['id']), true);
        
        $entity = M($name,$prefix);
        
        $data = $entity

            ->field(empty($fields) ? true : $fields)

            ->where($map)
            
            ->order(empty($extend['order'])?"id desc":$extend['order'])
            
            ->page($page, $row)
            
            ->select();
                
        if ($flag) {
            
            $count = $entity->where($map)->count(); 
        
            $total = intval(ceil($count/$row));  
            
            $this->assign('total',$total);
        }
        
        $this->assign('model', $model);

        return $data;
    }
    /**
     *验证签名
     */
    public function validation_sign($encrypt = "", $md5_sign = "")
    {
        $signString = $this->arrSort($encrypt);
        $md5Str = $this->encrypt_md5($signString, $key = "");
        if ($md5Str === $md5_sign) {
            return true;
        } else {
            return false;
        }
    }
    protected function join_data($model){
        $game  = M($model['m_name'],$model['prefix']);
        $map = $model['map'];
        $data  = $game
        ->field($model['field'])
        ->join($model['join'])
        ->limit($model['limit'])
        ->where($map)->group($model['group'])->order($model['order'])->select();
        return $data;
    }
    /**
     *对数据进行排序
     */
    private function arrSort($para)
    {
        ksort($para);
        reset($para);
        return $para;
    }

    /**
     *MD5验签加密
     */
    protected function encrypt_md5($param = "", $key = "")
    {
        #对数组进行排序拼接
        if (is_array($param)) {
            $md5Str = implode($this->arrSort($param));
        } else {
            $md5Str = $param;
        }
        $md5 = md5($md5Str . $key);
        return '' === $param ? 'false' : $md5;
    }
    
    /**
     *消费记录表 参数
     */
    private function spend_param($param = array())
    {
        $user_entity = get_user_entity($param['user_id']);
        $data_spned['user_id'] = $param["user_id"];
        $data_spned['user_account'] = $user_entity["account"];
        $data_spned['user_nickname'] = $user_entity["nickname"];
        $data_spned['game_id'] = $param["game_id"];
        $data_spned['game_appid'] = $param["game_appid"];
        $data_spned['game_name'] = $param["game_name"];
        $data_spned['server_id'] = 0;
        $data_spned['server_name'] = "";
        $data_spned['promote_id'] = $user_entity["promote_id"];
        $data_spned['promote_account'] = $user_entity["promote_account"];
        $data_spned['order_number'] = $param["order_number"];
        $data_spned['pay_order_number'] = $param["pay_order_number"];
        $data_spned['props_name'] = $param["title"];
        $data_spned['pay_amount'] = $param["real_pay_amount"];
        $data_spned['pay_time'] = NOW_TIME;
        $data_spned['pay_status'] = $param["pay_status"];
        $data_spned['pay_game_status'] = 0;
        $data_spned['pay_way'] = $param["pay_way"];
        $data_spned['spend_ip'] = $param["spend_ip"];
        return $data_spned;
    }

    /**
     *平台币充值记录表 参数
     */
    private function deposit_param($param = array())
    {
        $user_entity = get_user_entity($param['user_id']);
        $data_deposit['order_number'] = $param["order_number"];
        $data_deposit['pay_order_number'] = $param["pay_order_number"];
        $data_deposit['user_id'] = $param["user_id"];
        $data_deposit['user_account'] = $user_entity["account"];
        $data_deposit['user_nickname'] = $user_entity["nickname"];
        $data_deposit['promote_id'] = $param["promote_id"];
        $data_deposit['promote_account'] = get_promote_name($param['promote_id']);
        $data_deposit['pay_amount'] = $param["pay_amount"];
        $data_deposit['reality_amount'] = $param["real_pay_amount"];
        $data_deposit['pay_status'] = 0;
        $data_deposit['pay_source'] = 2;
        $data_deposit['pay_way'] = $param["pay_way"];
        $data_deposit['pay_ip'] = $param["spend_ip"];
        $data_deposit['create_time'] = NOW_TIME;
        return $data_deposit;
    }

    /**
     *绑定平台币消费
     */
    private function bind_spend_param($param = array())
    {
        $user_entity = get_user_entity($param['user_id']);
        $data_bind_spned['user_id'] = $param["user_id"];
        $data_bind_spned['user_account'] = $user_entity["account"];
        $data_bind_spned['user_nickname'] = $user_entity["nickname"];
        $data_bind_spned['game_id'] = $param["game_id"];
        $data_bind_spned['game_appid'] = $param["game_appid"];
        $data_bind_spned['game_name'] = $param["game_name"];
        $data_bind_spned['server_id'] = 0;
        $data_bind_spned['server_name'] = "";
        $data_bind_spned['promote_id'] = $user_entity["promote_id"];
        $data_bind_spned['promote_account'] = $user_entity["promote_account"];
        $data_bind_spned['order_number'] = $param["order_number"];
        $data_bind_spned['pay_order_number'] = $param["pay_order_number"];
        $data_bind_spned['props_name'] = $param["title"];
        $data_bind_spned['pay_amount'] = $param["price"];
        $data_bind_spned['pay_time'] = NOW_TIME;
        $data_bind_spned['pay_status'] = $param["pay_status"];
        $data_bind_spned['pay_game_status'] = 0;
        $data_bind_spned['pay_way'] = 1;
        $data_bind_spned['spend_ip'] = $param["spend_ip"];
        return $data_bind_spned;
    }
		
		/**
		 * 添加购买订单
		 */
		protected function add_order($data)
    {
			$user = get_user_entity($data['user_id']);
			$order=M('order','tab_');
			
 			$da = $order->where(['merchandise_id'=>$data['id'],'buyer_id'=>$data['user_id']])->find();
			
			if(is_array($da)) {
				if($da['pay_status']==1) {return false;}
				$data['pay_order_number'] = $da['order_number'];
				return $da['id'];
			}
			
			$order_data['order_number']=$data['pay_order_number'];
			$order_data['buyer_id']=$data['user_id'];
			$order_data['buyer_account']=$user['account'];
			$order_data['seller_id']=$data['seller_id'];
			$order_data['seller_account']=$data['seller_account'];
			$order_data['order_small_id']=$data['small_id'];
			$order_data['order_small_account']=$data['small_account'];
			$order_data['merchandise_id']=$data['id'];
			$order_data['order_price']=$data['real_pay_amount'];			
			$order_data['poundage']=$data['poundage'];
			$order_data['real_money']=$data['real'];		
			$order_data['pay_way'] = $data['pay_way'];
      $order_data['order_time'] = NOW_TIME;
			$order_data['pay_status'] = 0;
			$order_data['game_id'] = $data['game_id'];
			
			$result = $order->add($order_data);
      return $result;
    }

    /**
     *消费表添加数据
     */
    protected function add_spend($data)
    {
        $spend_data = $this->spend_param($data);
        $spend = M("spend", "tab_")->add($spend_data);
        return $spend;
    }

    /*
    *平台币充值记录
    */
    protected function add_deposit($data)
    {
        $deposit_data = $this->deposit_param($data);
        $deposit = M("deposit", "tab_")->add($deposit_data);
        return $deposit;
    }


    /*
    *绑定平台币消费记录
    */
    protected function add_bind_spned($data)
    {
        $data_bind_spned = $this->bind_spend_param($data);
        $bind_spned = M("BindSpend", "tab_")->add($data_bind_spned);
        return $bind_spned;
    }


    /**
     * 增加绑币充值记录
     * @param $param
     * author: xmy 280564871@qq.com
     */
    protected function add_bind_recharge($param){
        $user = get_user_entity($param['user_id']);
        $data['order_number']     = "";
        $data['pay_order_number'] = $param['pay_order_number'];
        $data['game_id']          = $param['game_id'];
        $data['game_appid']       = $param['game_appid'];
        $data['game_name']        = $param['game_name'];
        $data['promote_id']       = $param['promote_id'];
        $data['promote_account']  = $user['promote_account'];
        $data['user_id']          = $param['user_id'];
        $data['user_account']     = $user['account'];
        $data['user_nickname']    = $user['nickname'];
        $data['pay_type']         = $param['pay_type'];
        $data['amount']           = $param['pay_amount'];
        $data['real_amount']      = $param['real_pay_amount'];
        $data['pay_status']       = 0;
        $data['pay_way']          = $param['pay_way'];
        $data['create_time']      = time();
        $data['zhekou']           = $param['discount'];
        $data['recharge_ip']      = get_client_ip();
        return M("bind_recharge","tab_")->add($data);
    }

}