<?php
// +----------------------------------------------------------------------
// | 系统配置控制器
// +----------------------------------------------------------------------
// | ThinkOAO （Online And Offline）
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.hanchao9999.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xiaogg <xiaogg@sina.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
class ConfigMobileController extends BaseController {

	public function _initialize() {
		parent::_initialize();
		$this->model = D('Config');
	}
	//添加新设置
	public function add() {
	   if(IS_POST){
            $data = $this->model->create();
            if($data){
                if($this->model->add()){
                    S('DB_CONFIG_DATA',null);
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $grouplist=strtoarray(C('CONFIG_GROUP_LIST'));
            $grouplist[0]='隐藏';
            $this->assign('TYPE_LIST',strtoarray(C('CONFIG_TYPE_LIST')));
            $this->assign('GROUP_LIST',$grouplist);
            $this->assign('info',null);           
            $this->display('edit');
        }
	}
    /**
     * 编辑配置
     */
    public function edit($id = 0){
        if(IS_POST){
            $data = $this->model->create();
            if($data){
                if($this->model->save()){
                    S('DB_CONFIG_DATA',null);
                    //记录行为
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $info = array();
            /* 获取数据 */
            $info = $this->model->field(true)->find($id);

            if(false === $info){
                $this->error('获取配置信息错误');
            }
            $grouplist=strtoarray(C('CONFIG_GROUP_LIST'));
            $grouplist[0]='隐藏';
            $this->assign('TYPE_LIST',strtoarray(C('CONFIG_TYPE_LIST')));
            $this->assign('GROUP_LIST',$grouplist);
            $this->assign($info);
            $this->meta_title = '编辑配置';
            $this->display();
        }
    }
    /**
     * 批量保存配置
     */
    public function save($config){
        if($config && is_array($config)){
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                $this->model->where($map)->setField('value', $value);
            }
        }
        if($config['MEMBER_UC'] && $config['MEMBER_UCONFIG']){//更新ucenter配置
        $con = "<?php\n".$config['MEMBER_UCONFIG'].";\n?>";
        oaofile(APP_PATH.'Member/Conf/uc.php', $con);
        }
        S('DB_CONFIG_DATA',null);
        $this->success('保存成功！');
    }
	public function index() {
        /* 查询条件初始化 */
        $map = array();$getdata=I('param.');
        //$map  = array('status' => 1);
        if(!empty($getdata['group']))$map['group']   =   $getdata['group'];
        if(!empty($getdata['name'])){
            $whereor['name']=array('like','%'.$getdata['name'].'%');
            $whereor['title']=array('like','%'.$getdata['name'].'%');
            $whereor['value']=array('like','%'.$getdata['name'].'%');
            $whereor['extra']=array('like','%'.$getdata['name'].'%');
            $whereor['remark']=array('like','%'.$getdata['name'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
        $list = $this->lists('Config', $map,'sort,id');
        $this->assign('group_id',I('get.group',0));
        $this->assign('list', $list);
        $this->assign($getdata);
        $big_menu = array('添加配置'=>U(CONTROLLER_NAME.'/add'));
		$this->assign('big_menu',$big_menu);
		$this->display();

	}
    // 获取某个标签的配置参数
    public function group() {
        $grouplist   =   strtoarray(C('CONFIG_GROUP_MOBILE_LIST'));
        $list   =   $this->model->where(array('status'=>1))->field('id,name,title,group,extra,value,remark,type')->order('sort')->select();
        if($list) {
            $this->assign('list',$list);
        }$this->assign('grouplist',$grouplist);
        if(I('is_content'))$this->assign('is_content',I('is_content'));
        $this->display();
    }
    // 微信绑定
	public function wx_bound(){	  
	   if(C('BOUND_STATUS')==1){
	       $this->redirect('group',array('is_content'=>7));return;
	   }
        $wx = new \Common\Vendor\Weixinopen;
        $res = $wx->wxconfig(C('appid'));
		$data = array();
		$is_author = 0;//是否已经授权(0:否,1:是)		
		if(!empty($res)){
			$is_author = 1;		
			$result=$wx->get_authorizer_info();
			
			$func_info = $result['authorization_info']['func_info'];
			$info_arr = array();
			if($func_info){
				foreach($func_info as $val){
					foreach($val['funcscope_category'] as $k => $v){
						$info_arr[] = $v;
					}
				}
			}
			// 授权方认证类型，-1代表未认证，0代表微信认证，1代表新浪微博认证，2代表腾讯微博认证，3代表已资质认证通过但还未通过名称认证，4代表已资质认证通过、还未通过名称认证，但通过了新浪微博认证，5代表已资质认证通过、还未通过名称认证，但通过了腾讯微博认证
			$data['nick_name'] = $result['authorizer_info']['nick_name'];
			$data['service_type_info'] = $result['authorizer_info']['service_type_info'];
			// $data['qrcode_url'] = $result['authorizer_info']['qrcode_url'];
			// if($result['authorizer_info']['qrcode_url']){
				// print_r($result['authorizer_info']['qrcode_url']);exit;
				// $this->save_pic($result['authorizer_info']['qrcode_url']);
			// }
			$data['qrcode_url'] =$result['authorizer_info']['qrcode_url'];
            /*if(C('WEIXIN_PUBLIC_NUMBER_URL') != $data['qrcode_url']){
                $map = $save = array();
                $map['name'] = 'WEIXIN_PUBLIC_NUMBER_URL';
                $save['value'] = $data['qrcode_url'];
                M('Config')->where($map)->save($save);
            }*/
			$data['business_arr'] = $result['authorizer_info']['business_info'];
			$data['info_arr'] = $info_arr;


			$type = 0;
			if($data['service_type_info']['id'] == 2){
				if($result['authorizer_info']['verify_type_info']['id'] == -1){
					$type = 3;
				}else{
					$type = 4;
				}
			}else{
				if($result['authorizer_info']['verify_type_info']['id'] == -1){
					$type = 1;
				}else{
					$type = 2;
				}
			}
            //绑定开放平台
            $open_openid=C("OPEN_APPID");
            $appid=C('appid');
            if($open_openid){
                $authorizer_appid=$res['authorizer_info']['authorizer_appid'];
                $this->bind_openid($authorizer_appid,$open_openid,0);
            }else{
                $wx->open_bind_wxall($appid);
            }
			if(C('WX_TYPE') != $type){
				$map = $save = array();
				$map['name'] = 'WX_TYPE';
				$save['value'] = $type;
				M('Config')->where($map)->save($save);
			}
		}
		$data['is_author'] = $is_author;
		// echo "<pre>";print_r($data);exit;
		$this->assign($data);
		$this->display();
	}
    public function save_pic($qrcode_url){
		$appid = C('appid');
		$image=array(
			'name'=>"qrcodeurl_".$appid.".png",
			'type'=>'image/png',
			'tmp_name'=>$qrcode_url,
			'error'=>'0',
			'size'=>filesize($qrcode_url),
			'oss'=>1
		);
		echo "<pre>";print_r($image);
		$res=uploadimg($image);
		print_r($res);exit;
		
	}
    public function wx_small_bound(){
        
        $morecode=I('morecode');
        $this->assign('morecode',$morecode);
        $wx = new \Common\Vendor\Weixinopen;
        $siteid=C('appid');
        $cachename_moresmall='config_moresmall.php';
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $appid=$config_morelist[$siteid.'_'.$morecode];
        if(empty($appid)){$is_author = 0;}
        if($appid) {
            $res = $wx->wxconfig($appid, '', 1);
            $data = array();
            $is_author = 0;//是否已经授权(0:否,1:是)
            if (!empty($res)) {
                $is_author = 1;
                $result = $wx->get_authorizer_info($appid);
                $func_info = $result['authorization_info']['func_info'];
                $info_arr = array();
                if ($func_info) {
                    foreach ($func_info as $val) {
                        foreach ($val['funcscope_category'] as $k => $v) {
                            $info_arr[] = $v;
                        }
                    }
                }
                // 授权方认证类型，-1代表未认证，0代表微信认证，1代表新浪微博认证，2代表腾讯微博认证，3代表已资质认证通过但还未通过名称认证，4代表已资质认证通过、还未通过名称认证，但通过了新浪微博认证，5代表已资质认证通过、还未通过名称认证，但通过了腾讯微博认证
                $data['nick_name'] = $result['authorizer_info']['nick_name'];
                $data['service_type_info'] = $result['authorizer_info']['service_type_info'];
                // $data['qrcode_url'] = $result['authorizer_info']['qrcode_url'];
                // if($result['authorizer_info']['qrcode_url']){
                // print_r($result['authorizer_info']['qrcode_url']);exit;
                // $this->save_pic($result['authorizer_info']['qrcode_url']);
                // }
                $data['qrcode_url'] = $result['authorizer_info']['qrcode_url'];
                $data['business_arr'] = $result['authorizer_info']['business_info'];
                $data['info_arr'] = $info_arr;


                $type = 0;
                if ($data['service_type_info']['id'] == 2) {
                    if ($result['authorizer_info']['verify_type_info']['id'] == -1) {
                        $type = 3;
                    } else {
                        $type = 4;
                    }
                } else {
                    if ($result['authorizer_info']['verify_type_info']['id'] == -1) {
                        $type = 1;
                    } else {
                        $type = 2;
                    }
                }
                if (C('WX_TYPE') != $type) {
                    $map = $save = array();
                    $map['name'] = 'WX_TYPE';
                    $save['value'] = $type;
                    M('Config')->where($map)->save($save);
                }
                //绑定开放平台
                $open_openid = C("OPEN_APPID");
                $wxinfo = $wx->wxconfig($appid, '', 1);
                $authorizer_appid = $wxinfo['authorization_info']['authorizer_appid'];
                if ($open_openid) {
                    $this->bind_openid($authorizer_appid, $open_openid, 1);
                } else {
                    $appid = C('appid');
                    $wx->open_bind_wxall($appid);
                }
            }
        }
        $data['is_author'] = $is_author;
        // echo "<pre>";print_r($data);exit;
        $this->assign($data);
        $this->display();
    }
    /**绑定开放 平台
     * @param $appid
     * @param $openid
     */
    public function bind_openid($appid,$openid,$type){
        $siteid=C('APPID');
        $wx = new \Common\Vendor\Weixinopen;
        $old= $wx->wxconfig($appid,'',$type);
        if($old['authorization_info']['wx_open_appid'])return true;
        if(empty($old['authorization_info']['wx_open_appid'])){
            $res=$wx->open_bind($appid,$type,$openid);
            if($res['errcode']==0){return true;
            }else{
                return false;
            }
        }
    }
}
?>