<?php
/** App base控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

/** 注意事项
 * 1.注意客户端与服务端控制器与方法名称的大小写（控制器首字母大写）记录在method_array方法中
 * 2.返回值统一放在$return['data']中,报错信息放在$return['msg']中
 * 3.返回值$return['status']中 1代表正常数据 2代表获取的数据为空 3代表信息错误 
 */
namespace Wxsmall\Controller;
use Common\Controller\HanchaoController;
class BaseController extends HanchaoController {
	public function _initialize(){
        GLOBAL $PurchaseCategory;
        if(empty($PurchaseCategory))$PurchaseCategory=getcache('PurchaseCategory');
        parent::_initialize ();
		$this->pagesize = C('WEB_DEFAULT_PAGE') > 0 ? C('WEB_DEFAULT_PAGE') :10;//网站默认分页
        if(I('pagesize'))$this->pagesize=I('pagesize');
        $this->ControllerName = CONTROLLER_NAME; //控制器			
		$this->ActionName = ACTION_NAME; //方法
        $this->AppKey=$_SERVER['HTTP_X_APPKEY'];
        $this->check();
    }
    //
    //允许执行的控制器与方法
    private function method_array($key=''){
        $method_array = array(
            //添加控制器和方法
            'Index'=>array('index','get_category','get_vindex','get_shop_name','get_fight_group','address','geocoding'),
            'Ajax'=>array('upload','sendsms'),
            'Article'=>array('article_list','article_cont'),
            'Login'=>array('doregister','re_site','get_industy','dologin','setshopid','get_member_info'),
            'Member'=>array('index','balance','integral','mycoupon_list','myorder_list','myorder_info','address','address_list','mymoney','saveuser','get_paypwd','money_change_dec','mycoupon_cont','get_card_sn','save_card_code','to_sign','my_collect','del_collect','comment_list','distance','getmymoney','getrecharge','my_sign','get_bestgoods_list','sign_log','sign_rule','get_order_group','save_user_mobile','save_user_mobile_input','get_invite_mobile','doexpress'),
            'Coupon'=>array('index','countop_count','get_coupon_wx','save_coupon'),
            'Wxsmallpay'=>array('pay_done','pay_moneydone'),
            'Takeout'=>array('index','buy_nows','cont','cont_detial','collect_goods','add_cart','add_carts','get_goods_price','sub_cart','cont_tgercode'),
            'Selfgo'=>array('index','get_goods_price','add_cart','add_carts','buy_order','getcatchs','self_acarts','submit_order','count_total','get_orderinfo','get_ordercode','complete_order','get_myorders','scan_get_goods'),
            'Live'=>array('live_anchor_info','liver_info','get_search_goods','ajaxrun','get_live_goods','get_live_list','liveindex','get_live_info','config','anchor_info','collect_live','myfollow_list','live_history','delete_collect','add_goodsattr','getPushURL','get_attrs','get_live_playurl','fans_list','add_cart','get_live_pushurl','get_live_fetch','get_shops_list','sub_anchor_info','get_small_appid','live_divided','is_livehis','set_live_num','del_anchor_goods','get_link','stop_live','get_link_url','get_live_cate','get_live_praise','jim_init'),

            'LittleShop'=>array('index','choice','my_goods_up','go_isshow','update_myshop','sub_shop','order_info'),
            'Nearby'=>array('nearby'),
            'Cart'=>array('index','del_cart','change_pro_goods','add_pro_goods','sub_cart','add_cart'),
            'Activity'=>array('index','activity_list','promat_sale'),
            'Order'=>array('index','count_total','sub_order','order_success','cancel_order','continu_pay','continue_payhtml','continue_count_total','sub_continue_order','confirm_order','order_invoice','sub_invoice'),
            'Dp'=>array('index','add_dp'),
            'Search'=>array('index'),
            'FightGroup'=>array('cont','group_join'),
            'Extension'=>array('index','extension_list','extension','member_poster','rechange','sub_recharge','withdrawals','sub_ti'),
            'ProLevel'=>array('index','info'),
            'ExtensionMatter'=>array('posters','save_matter','save_xcx_matter'),
            //end
        );
        return empty($key)?$method_array:$method_array[$key];
    }
    //检测请求是否合法
	private function check(){
	    $return['status']=3;
        //请求日志
        //file_put_contents('applog/'.$ControllerName.'/'.$ActionName.'-'.date("YmdHis").'.txt',var_export($_REQUEST,true));
		if(!in_array( $this->ControllerName, array_keys($this->method_array()))){		  
            $return['msg']='请求的控制器('. $this->ControllerName.')不存在';
            $this->ajaxReturn($return);
		}
        if(!in_array( $this->ActionName, $this->method_array($this->ControllerName))){		  
            $return['msg']='请求的方法('.$this->ActionName.')不存在';
            $this->ajaxReturn($return);
        }
        return true;
        if(empty($this->AppKey)){
            $return['msg']='APPKEY不能为空';
		    $this->ajaxReturn($return);
        }
        $appkeyarr=explode('.',$this->AppKey);
        $sign=$appkeyarr[0];//签名
        $timestamp=$appkeyarr[1];
        $method = 'c='.$this->ControllerName.'&a='.$this->ActionName;
        $my_sign=SHA1($method.$timestamp.C('HANCHAO_KEY'));	
		if($my_sign!=$sign){
		  $return['msg']='签名不正确';
		  $this->ajaxReturn($return);
		}
        return true;	
	}
    public function checkuserauth(){
        $return = array('status'=>3,'msg'=>'登录信息错误，请重新获取！');
        $paramdata=I('param.');
        if(empty($paramdata['HCtoken'])){
		    $this->ajaxReturn($return);
        }
        $result = D('Wxsmall/Login')->check_wxsmall_code($paramdata);
        if($result['status']==1){
            $userinfo=idtoname($result['uid'],'Member','*','id',false);
            $userinfo['HCshop']['bind_shop_id'] = $result['bind_shop_id']?$result['bind_shop_id']:0;
            $userinfo['HCshop']['code'] = $result['code']?$result['code']:'';
            $userinfo['HCshop']['send'] = $result['send']?$result['send']:0;
            if(empty($userinfo['HCshop']['bind_shop_id'])&&isset($paramdata['HCshopid'])&&$paramdata['HCshopid']>0){
                $userinfo['HCshop']['bind_shop_id'] = $paramdata['HCshopid'];
            }
            return $userinfo;
        }else{
            $return['msg'] = $result['msg'];
            $return['code'] = $result['code'];
        }
        // echo(json_encode($return));
        exit;
    }
    public function format_content($content){
        if(empty($content))return false;
        $content=h($content);
        $baseurl='http://'.$_SERVER['HTTP_HOST'].__ROOT__;
        $contentarr = $content;
        preg_match_all("/<img(.*)(src=\"[^\"]+\")[^>]+>/isU", $contentarr, $arr);
        $images = array();
        foreach($arr[0] as $key=>$val){
            $match = array();
            preg_match('/<img.*?src="(.*?)".*?>/is',$val,$match);
            if(str_exists($match[1],'http')||str_exists($match[1],'https')){
                $images['images']['images'.$key] = $match[1];
            }else{
                $images['images']['images'.$key] = $baseurl.$match[1];
            }
            
            $contentarr = str_replace($val,'<img v-lazy="this.xqimages.images'.$key.'">',$contentarr);
        }
        $images['content'] = $contentarr;
        return $images;
    }

}
?>