<?php

	/** 
		+---------------------------------------------------------------
		| 开单大师（专业的房产ERP管理系统）
		+---------------------------------------------------------------
		| © 2018 http://fs.ytsofts.cn All rights reserved.
		+---------------------------------------------------------------
		| 本系统由淮南市银泰软件科技有限公司提供技术支持
		+---------------------------------------------------------------
		| QQ号：984784483
		+---------------------------------------------------------------
	*/



	/*-----------------------------------权限函数开始-------------------------------------*/

		function bumen_option($selected,$app = MODULE_NAME,$modules = CONTROLLER_NAME,$action = ACTION_NAME){
			$x = $node_id ? get_node_range_str($app,$modules,$action) : "null";
			$tree = new \Org\Util\Tree();
			$parentid = $selected;
			$result = M('bumen')->where(array("gongsiid"=>session("gongsiid")))->order(array("sort" => "ASC"))->select();
			foreach ($result as $key => $r) {
				$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
				if ($x && $x != "personal" && $x != "null") {
					$b = explode(",",$x);
					if (in_array($r['id'],$b)) {
						$array[] = $r;
					}
				}elseif($x == 'personal' || $x == false){
					$array = "";
				}elseif($x == 'null'){
					$array[] = $r;
				}
			}
			$a = 0;
			$l = 999;
			if ($x && $x != "personal" && $x != "null") {
				foreach ($array as $key => $value) {
					if ($value['level'] < $l) {
						$a = $value['parentid'];
						$l = $value['level'];
					}
				}
			}
			$str = "<option value='\$id' title='\$bmming' \$selected>\$spacer \$bmming</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree($a, $str);
			return $select_categorys;
		}

		function bumen_idsz($node_id){
			if ($node_id) {
				$bumen = _rbac_bumen_show($node_id,0);
				if (!$bumen) {
					$bumen = -1;
				}
				$arr_string = get_bm_sz($bumen,1);
				return $arr_string;
			}
		}

		/**
			* 二维数组排序
			* @param $arr
			* @param $keys
			* @param string $type
			* @return array
		*/

		function array_sort($arr, $keys, $type = 'desc') {
			$key_value = $new_array = array();
			foreach ($arr as $k => $v) {
				$key_value[$k] = $v[$keys];
			}
			if ($type == 'asc') {
				asort($key_value);
			} else {
				arsort($key_value);
			}
			reset($key_value);
			foreach ($key_value as $k => $v) {
				$new_array[$k] = $arr[$k];
			}
			return $new_array;
		}

		

		function _rbac_bumen_show($node_id,$zhuangtai){
			$fanwei = _rbac_range($node_id,$zhuangtai);
			if (!$fanwei) {
				return false;
			}elseif ($fanwei=='all') {
				return 'all';
			}elseif ($fanwei=='me') {
				return 'me';
			}
			$arr =_rbac_bumen_top_id($fanwei);
			if (!$arr) {
				return false;
			}
			return $arr["id"];
		}

		function _rbac_bumen_top_id($val){
			$b_id = M('yonghu')->where(array("gongsiid"=>session("gongsiid"),'id'=>session('uid')))->getField('bumen');
			$find = M('bumen')->where(array("gongsiid"=>session("gongsiid"),'id'=>$b_id))->find();
			$i = $find['level'];
			if ($find['level']<$val) {
				return false;
			}elseif ($find['level']==$val) {
				return $find;
			}elseif ($find['level']>$val) {
				$u = $find['level']-$val;
				for ($i=0; $i < $u; $i++) { 
					$find = M('bumen')->where(array("gongsiid"=>session("gongsiid"),'id'=>$find['parentid']))->find();
				}
				return $find;
			}
		}

		function _rbac_bumen_array($array,$id=0){
			$arr = array();
			foreach ($array as $v) {
				if ($v['parentid']==$id) {
					$arr[] = $v['id'];
					$a = _rbac_bumen_array($array,$v['id']);
					$arr = array_merge($arr, $a); 
				}
			}
			return $arr;
		}

		//获取相应节点和状态的 范围值
		function _rbac_range($node_id,$zhuangtai){
			if(session('username') == C('RBAC_SUPERADMIN')){
				return 'all';
			}
			$where['gongsiid'] = session("gongsiid");
			$where['node_id'] = $node_id;
			$where['role_id'] = get_yh_role(session('uid'),1);
			$where['zhuangtai'] = $zhuangtai;
			$fanwei = M('access')->where($where)->find();
			if (!$fanwei) {
				return false;
			}
			if (!$fanwei['fanwei']){
				return 'all';
			}
			$me_level = _rbac_me_level();
			if ($fanwei['fanwei']==$me_level) {
				return 'me';
			}
			return $fanwei['fanwei'];
		}

		//获取范围生成逗号分隔的字符串
		function _rbac_in_bumen_string($node_id,$zhuangtai){
			$fanwei = _rbac_range($node_id,$zhuangtai);
			if (!$fanwei) {
				return false;
			}elseif ($fanwei=='all') {
				return 'all';
			}elseif ($fanwei=='me') {
				return 'me';
			}
			$arr =_rbac_bumen_top_id($fanwei);
			if (!$arr) {
				return false;
			}
			$string = get_bm_sz($arr['id'],2);
			return $string;
		}

		//根据节点状态和对应的人物 查看是否有权限
		function _rbac_btn_show($node_id,$zhuangtai,$uid){
			$string = _rbac_in_bumen_string($node_id,$zhuangtai);
			if (!$string) {
				return false;
			}elseif ($string=='all') {
				return true;
			}elseif ($string=='me') {
				if ($uid==session('uid')) {
					return true;
				} else {
					return false;
				}
			}
			$map['gongsiid'] = session("gongsiid");
			$map['bumen'] = array('in',$string);
			$map['id'] = $uid;
			$find = M('yonghu')->where($map)->find();
			if ($find) {
				return true;
			}else{
				return false;
			}
		}

		function _rbac_node_show($node_id){
			if(session('username') == C('RBAC_SUPERADMIN')){
				return true;
			}
			$where['gongsiid'] = session("gongsiid");
			$where['node_id'] = $node_id;
			$where['role_id'] = get_yh_role(session('uid'),1);
			$find = M('access')->where($where)->find();
			if ($find) {
				return true;
			}else{
				return false;
			}
		}

		function _rbac_me_level(){
			$count = count(get_options('bumen_options','bumen_array'));
			return $count+1;
		}

		function _rbac_lists_where($node_id,$zhuangtai){
			$string = _rbac_in_bumen_string($node_id,$zhuangtai);
			if (!$string) {
				return false;
			}elseif ($string=='all') {
				return 'all';
			}elseif ($string=='me') {
				return 'me';
			}
			return $string;
		}

	/*-----------------------------------权限函数结束-------------------------------------*/

	// 分析枚举类型配置值 格式 a:名称1,b:名称2
	function parse_config_attr($string) {
		$array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
		if(strpos($string,':')){
			$value = array();
			foreach ($array as $val) {
				list($k, $v) = explode(':', $val);
				$value[$k] = $v;
			}
		}else{
			$value = $array;
		}
		return $value;
	}

	/**
		* 获取数据库中的配置列表
		* @return array 配置数组
	*/

	function lists(){
		$data = M('Configure')->where(array('status'=>1))->field('type,name,value')->select();
		$config = array();
		if($data && is_array($data)){
			foreach ($data as $value) {
				$config[$value['name']] =parse($value['type'], $value['value']);
			}
		}
		return $config;
	}

	/**
		* 根据配置类型解析配置
		* @param integer $type 配置类型
		* @param string $value 配置值
	*/

	function parse($type, $value){
		switch ($type) {
			//解析数组
			case 3:
				$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
				if(strpos($value,':')){
					$value = array();
					foreach ($array as $val) {
						list($k, $v) = explode(':', $val);
						$value[$k] = $v;
					}
				}else{
					$value = $array;
				}
				break;
		}
		return $value;
	}

	/**
		* 格式化字节大小
		* @param number $size 字节数
		* @param string $delimiter 数字和单位分隔符
		* @return string 格式化后的带单位的大小
	*/

	function format_bytes($size, $delimiter = '') {
		$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
		for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
		return round($size, 2) . $delimiter . $units[$i];
	}

	//获取ip地址物理位置
	function findCityByIp($ip){
		$data = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
		$dizhi = json_decode($data,$assoc=true);
		$country = $dizhi['data']['country'];
		if (($country == "台湾") || ($country == "澳门") || ($country == "香港")) {
			$country = "中国";
		}
		return $country." ".$dizhi['data']['region']." ".$dizhi['data']['city']." ".$dizhi['data']['isp'];
	}



	//获取部门及下级部门的数组
	function get_bm_sz($bmid,$lx){
		$result = M('bumen')->where(array("gongsiid"=>session("gongsiid")))->order(array("sort" => "ASC"))->select();
		if ($bmid) {
			$arr_string = _rbac_bumen_array($result,$bmid);
			$arr_string[] = $bmid;
			if ($lx == 1) {
				$bm=$arr_string;
			} elseif ($lx == 2) {
				$bm = implode(',',$arr_string);
			}
			return $bm;
		}
	}

	//获取用户部门
	function yhbm($id,$lx){
		$bm=M('yonghu')->where(array("gongsiid"=>session("gongsiid"),'id'=>$id))->getField('bumen');
		if ($lx == 1) {
			$yhbm=$bm;
		} elseif ($lx == 2) {
			$yhbm=zym($bm,7);
		} elseif ($lx == 3) {
			$yhbm=zym($bm,6);
		}
		return $yhbm;
	}

	//获取用户角色（岗位）
	function get_yh_role($id,$lx){
		$role_id=M('role_user')->where(array("gongsiid"=>session("gongsiid"),'user_id'=>$id))->getField('role_id');
		if ($lx == 1) {
			return $role_id;
		} elseif ($lx == 2) {
			$remark=zym($role_id,8);
			return $remark;
		}
	}

	//获取用户可选角色（岗位）
	function yhkxrole($id,$lx){
		if (!$id) {
			$id=get_yh_role(session('uid'),1);
		}
		if ($lx == 1) {
			if (session('admin') == 1) {
				$role=M('role')->where(array('gongsiid'=>session('gongsiid')))->order('rolefl asc,xsshunxu asc')->select();
			} else {
				$kxrole=M('role')->where(array('gongsiid'=>session('gongsiid'),'id'=>$id))->getField('kxrole');
				if (!$kxrole) {
					$kxrole="0";
				}
				$role=M('role')->where(array('gongsiid'=>session('gongsiid'),'id'=>array('in',$kxrole)))->order('rolefl asc,xsshunxu asc')->select();
			}
		} elseif ($lx == 2) {
			$roleids=explode(",",$kxrole);
			foreach ($roleids as $key => $value) {
				$role[$value]=$value;
			}
		}
		return $role;
	}


	//判断资源是否有图片
	function zysfytp($zyid,$tplx){
		$img=M('photos')->where(array('data_id'=>$zyid,'data_table'=>$tplx))->getField('img_name');
		return $img;
	}


	//获取二手房源图片
	function get_s_img($id){
		$img_urlf=M('photo')->where(array("gongsiid"=>session("gongsiid"),'fybh'=>$id,'fengmian'=>1))->getField('image');
		$img_url=M('photo')->where(array("gongsiid"=>session("gongsiid"),'fybh'=>$id))->order('create_time desc')->limit(1)->getField('image');
		if ($img_urlf) {
			return $img_urlf;
		} else {
			return $img_url;
		}
	}

	//获取小区图片
	function get_x_img($id){
		$img_urlf=M('xqphoto')->where(array('xqid'=>$id,'fengmian'=>1))->getField('image');
		$img_url=M('xqphoto')->where(array('xqid'=>$id))->order('create_time desc')->limit(1)->getField('image');
		if ($img_urlf) {
			return $img_urlf;
		}else{
			return $img_url;
		}
	}

	//获取一手房源图片
	function get_y_img($id){
		$img_urlf=M('ysphoto')->where(array("gongsiid"=>session("gongsiid"),'ysbh'=>$id,'fengmian'=>1))->getField('image');
		$img_url=M('ysphoto')->where(array("gongsiid"=>session("gongsiid"),'ysbh'=>$id))->order('create_time desc')->limit(1)->getField('image');
		if ($img_urlf) {
			return $img_urlf;
		}else{
			return $img_url;
		}
	}




	//获取区间信息
	function get_qujian($cxz,$lx){
		if ($lx == 1) {
			$qujian = M('qujian_config')->where(array('pid'=>0))->select();
		} elseif ($lx == 2) {
			$qujian = M('qujian_config')->where(array("gongsiid"=>session("gongsiid"),'id'=>$cxz))->find();
		} elseif ($lx == 3) {
			$qujian = M('qujian_config')->where(array("gongsiid"=>session("gongsiid"),'sign'=>$cxz))->getField('id');
		} elseif ($lx == 4) {
			$qujian = M('qujian_config')->where(array("gongsiid"=>session("gongsiid"),'pid'=>$cxz))->order('value1 asc')->select();
		}
		return $qujian;
	}






	/**
		* 获取客户端浏览器类型
		* @return string 浏览器类型返回字符串 unknown为未知浏览器类型
	*/

	function getBrowser(){
		$agent=$_SERVER["HTTP_USER_AGENT"];
		if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
			return "ie";
		else if(strpos($agent,'Firefox')!==false)
			return "firefox";
		else if(strpos($agent,'Chrome')!==false)
			return "chrome";
		else if(strpos($agent,'Opera')!==false)
			return 'opera';
		else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
			return 'safari';
		else
			return 'unknown';
	}

	//判断是否是移动端
	function ismobile() {
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
			return true;
		//此条摘自TPM智能切换模板引擎，适合TPM开发
		if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
			return true;
		//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
		if (isset ($_SERVER['HTTP_VIA']))
		//找不到为flase,否则为true
			return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
		//判断手机发送的客户端标志,兼容性有待提高
		if (isset ($_SERVER['HTTP_USER_AGENT'])) {
			$clientkeywords = array(
				'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
			);
			//从HTTP_USER_AGENT中查找手机浏览器的关键字
			if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
				return true;
			}
		}
		//协议法，因为有可能不准确，放到最后判断
		if (isset ($_SERVER['HTTP_ACCEPT'])) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
				return true;
			}
		}
		return false;
	}

	/**
		* 上传文件类型控制 此方法仅限ajax上传使用
		* @param string $path 字符串 保存文件路径示例： /Upload/image/
		* @param string $format 文件格式限制
		* @param integer $maxSize 允许的上传文件最大值 52428800
		* @return booler 返回ajax的json格式数据
	*/

	//上传二手房源图片
	function ajax_upload($path='file',$format='image',$maxSize='52428800'){
		ini_set('max_execution_time', '0');
		// 去除两边的/
		$path=trim($path,'/');
		// 添加Upload根目录
		$path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;
		// 上传文件类型控制
		$ext_arr= array(
			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
			'photo' => array('jpg', 'jpeg', 'png'),
			'flash' => array('swf', 'flv'),
			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')
		);
		if(!empty($_FILES)){
			// 上传文件配置
			$config=array(
				'maxSize' => $maxSize, // 上传文件最大为50M
				'rootPath' => './', // 文件上传保存的根路径
				'savePath' => './'.$path.'/', // 文件上传的保存路径（相对于根路径）
				'saveName' => array('uniqid',''), // 上传文件的保存规则，支持数组和字符串方式定义
				'autoSub' => false, // 自动使用子目录保存上传文件 默认为true
				'exts' => '' //isset($ext_arr[$format])?$ext_arr[$format]:'',
			);
			// 实例化上传
			$upload=new \Think\Upload($config);
			// 调用上传方法
			$info=$upload->upload();
			$data=array();
			$ImgDb = M('photoss');
			if(!$info){
				// 返回错误信息
				$error=$upload->getError();
				$data['error_info']=$error;
				echo json_encode($data);
			}else{
				// 返回成功信息
				foreach($info as $file){
					//上传的图片同步保存到数据库表
					$add_data['gongsiid'] = session('gongsiid');
					$add_data['addUid'] = session('uid');
					$add_data['imgName'] = $file['savename'];
					$add_data['fybh'] = $_POST['fybh'];
					$add_data['addTime'] = time();
					$ImgDb->add($add_data);
					
					//删除原图
					unlink('./Upload/'.session('gongsiid').'/'.$_POST['fybh'].'/'.$file['savename']);
				}
			}
		}
	}

	//上传小区图片
	function ajax_upload_xq($path='file',$format='image',$maxSize='52428800'){
		ini_set('max_execution_time', '0');
		// 去除两边的/
		$path=trim($path,'/');
		// 添加Upload根目录
		$path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;
		// 上传文件类型控制
		$ext_arr= array(
			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
			'photo' => array('jpg', 'jpeg', 'png'),
			'flash' => array('swf', 'flv'),
			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')
		);
		if(!empty($_FILES)){
			// 上传文件配置
			$config=array(
				'maxSize' => $maxSize, // 上传文件最大为50M
				'rootPath' => './', // 文件上传保存的根路径
				'savePath' => './'.$path.'/', // 文件上传的保存路径（相对于根路径）
				'saveName' => array('uniqid',''), // 上传文件的保存规则，支持数组和字符串方式定义
				'autoSub' => false, // 自动使用子目录保存上传文件 默认为true
				'exts' => '' //isset($ext_arr[$format])?$ext_arr[$format]:'',
			);
			// 实例化上传
			$upload=new \Think\Upload($config);
			// 调用上传方法
			$info=$upload->upload();
			$data=array();
			$Img = D('xqphoto');
			if(!$info){
				// 返回错误信息
				$error=$upload->getError();
				$data['error_info']=$error;
				echo json_encode($data);
			}else{
				// 返回成功信息
				foreach($info as $file){
					//上传的图片同步保存到数据库表
					$d['uid'] = session('uid');
					$d['image'] = $file['savename'];
					$d['xqid'] = $_POST['xqid'];
					$d['create_time'] = time();
					$Img->add($d);
					//把fangyuan表tupian字段更新为 1
					M('Xiaoqu')->where(array('id'=>$_POST['xqid']))->setField('tupian',1);
					//为上传成功的图片生成缩略图
					$timg = new \Think\Image();
					$timg->open(trim($file['savepath'].$file['savename']));
					$newpathbig = trim($file['savepath']).'b_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$newpath = trim($file['savepath']).'t_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$timg->thumb(900,600)->save($newpathbig); 
					$timg->thumb(450,300)->save($newpath);
					if (C('IS_PHOTO_JSY')==1) {
						// 给原图添加水印
						$timg->open($newpathbig)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpathbig);
						$timg->open($newpath)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpath);
					}
					//删除原图
					unlink('./Upload/xiaoqu/'.$_POST['xqid'].'/'.$file['savename']);
					rename($newpathbig,'./Upload/xiaoqu/'.$_POST['xqid'].'/'.$file['savename']);
					//返回给AJAX
					$data['name']=trim($file['savepath'].$file['savename'],'.');
					echo json_encode($data);
				}
			}
		}
	}

	//上传一手房源图片
	function ajax_upload_ys($path='file',$format='image',$maxSize='52428800'){
		ini_set('max_execution_time', '0');
		// 去除两边的/
		$path=trim($path,'/');
		// 添加Upload根目录
		$path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;
		// 上传文件类型控制
		$ext_arr= array(
			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
			'photo' => array('jpg', 'jpeg', 'png'),
			'flash' => array('swf', 'flv'),
			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')
		);
		if(!empty($_FILES)){
			// 上传文件配置
			$config=array(
				'maxSize' => $maxSize, // 上传文件最大为50M
				'rootPath' => './', // 文件上传保存的根路径
				'savePath' => './'.$path.'/', // 文件上传的保存路径（相对于根路径）
				'saveName' => array('uniqid',''), // 上传文件的保存规则，支持数组和字符串方式定义
				'autoSub' => false, // 自动使用子目录保存上传文件 默认为true
				'exts' => '' //isset($ext_arr[$format])?$ext_arr[$format]:'',
			);
			// 实例化上传
			$upload=new \Think\Upload($config);
			// 调用上传方法
			$info=$upload->upload();
			$data=array();
			$Img = D('ysphoto');
			if(!$info){
				// 返回错误信息
				$error=$upload->getError();
				$data['error_info']=$error;
				echo json_encode($data);
			}else{
				// 返回成功信息
				foreach($info as $file){
					//上传的图片同步保存到数据库表
					$d['gongsiid'] = session('gongsiid');
					$d['uid'] = session('uid');
					$d['image'] = $file['savename'];
					$d['ysbh'] = $_POST['ysbh'];
					$d['create_time'] = time();
					$Img->add($d);
					//把fangyuan表tupian字段更新为 1
					M('ysfangyuan')->where(array("gongsiid"=>session("gongsiid"),'ysbianhao'=>$_POST['ysbh']))->setField('tupian',1);
					//为上传成功的图片生成缩略图
					$timg = new \Think\Image();
					$timg->open(trim($file['savepath'].$file['savename']));
					$newpathbig = trim($file['savepath']).'b_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$newpath = trim($file['savepath']).'t_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$timg->thumb(900,600)->save($newpathbig);
					$timg->thumb(450,300)->save($newpath);
					if (C('IS_PHOTO_JSY')==1) {
					// 给原图添加水印
					$timg->open($newpathbig)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpathbig);
					$timg->open($newpath)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpath);
					}
					//删除原图
					unlink('./Upload/'.session('gongsiid').'/ysfang/'.$_POST['ysbh'].'/'.$file['savename']);
					rename($newpathbig,'./Upload/'.session('gongsiid').'/ysfang/'.$_POST['ysbh'].'/'.$file['savename']);
					//返回给AJAX
					$data['name']=trim($file['savepath'].$file['savename'],'.');
					echo json_encode($data);
				}
			}
		}
	}

	//上传房间图片
	function ajax_upload_fj($path='file',$format='image',$maxSize='52428800'){
		ini_set('max_execution_time', '0');
		// 去除两边的/
		$path=trim($path,'/');
		// 添加Upload根目录
		$path=strtolower(substr($path, 0,6))==='upload' ? ucfirst($path) : 'Upload/'.$path;
		// 上传文件类型控制
		$ext_arr= array(
			'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
			'photo' => array('jpg', 'jpeg', 'png'),
			'flash' => array('swf', 'flv'),
			'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
			'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2','pdf')
		);
		if(!empty($_FILES)){
			// 上传文件配置
			$config=array(
				'maxSize' => $maxSize, // 上传文件最大为50M
				'rootPath' => './', // 文件上传保存的根路径
				'savePath' => './'.$path.'/', // 文件上传的保存路径（相对于根路径）
				'saveName' => array('uniqid',''), // 上传文件的保存规则，支持数组和字符串方式定义
				'autoSub' => false, // 自动使用子目录保存上传文件 默认为true
				'exts' => '' //isset($ext_arr[$format])?$ext_arr[$format]:'',
			);
			// 实例化上传
			$upload=new \Think\Upload($config);
			// 调用上传方法
			$info=$upload->upload();
			$data=array();
			$Img = D('fjphoto');
			if(!$info){
				// 返回错误信息
				$error=$upload->getError();
				$data['error_info']=$error;
				echo json_encode($data);
			}else{
				// 返回成功信息
				$xqid=M('fjxinxi')->where(array('id'=>$_POST['fjid']))->getField('xqid');
				$zdid=M('fjxinxi')->where(array('id'=>$_POST['fjid']))->getField('zdid');
				$dyid=M('fjxinxi')->where(array('id'=>$_POST['fjid']))->getField('dyid');
				$lcid=M('fjxinxi')->where(array('id'=>$_POST['fjid']))->getField('lcid');
				foreach($info as $file){
					//上传的图片同步保存到数据库表
					$d['image'] = $file['savename'];
					$d['xqid'] = $xqid;
					$d['zdid'] = $zdid;
					$d['dyid'] = $dyid;
					$d['lcid'] = $lcid;
					$d['fjid'] = $_POST['fjid'];
					$d['create_time'] = time();
					$d['uid'] = session('uid');
					$Img->add($d);
					//把fangyuan表tupian字段更新为 1
					M('fjxinxi')->where(array('id'=>$_POST['fjid']))->setField('tupian',1);
					//为上传成功的图片生成缩略图
					$timg = new \Think\Image();
					$timg->open(trim($file['savepath'].$file['savename']));
					$newpathbig = trim($file['savepath']).'b_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$newpath = trim($file['savepath']).'t_'.substr($file['savename'],0,strrpos($file['savename'], '.')).'.'.$file['ext'];
					$timg->thumb(900,600)->save($newpathbig); 
					$timg->thumb(450,300)->save($newpath);
					if (C('IS_PHOTO_JSY')==1) {
						// 给原图添加水印
						$timg->open($newpathbig)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpathbig);
						$timg->open($newpath)->water('./Upload/shuiyin/shuiyin1.png',\Think\Image::IMAGE_WATER_CENTER,C('IS_PHOTO_TOUMING'))->save($newpath);
					}
					//删除原图
					unlink('./Upload/xiaoqu/fangjian/'.$_POST['fjid'].'/'.$file['savename']);
					rename($newpathbig,'./Upload/xiaoqu/fangjian/'.$_POST['fjid'].'/'.$file['savename']);
					//返回给AJAX
					$data['name']=trim($file['savepath'].$file['savename'],'.');
					echo json_encode($data);
				}
			}
		}
	}

	//获取配置变动信息
	function get_xiugai($xiugaiqian,$xiugaihou,$pzming){
		$xgqian=get_peizhi($pzming,$xiugaiqian,3);
		$xghou=get_peizhi($pzming,$xiugaihou,3);
		$zhushi=get_pzmzs($pzming,4);
		return $zhushi."：[".$xgqian."]=>[".$xghou."]；";
	}

	//获取部门变动信息
	function get_xgbm($xiugaiqian,$xiugaihou,$bmming){
		$xgqian=M('bumen')->where(array("gongsiid"=>session("gongsiid"),'id'=>$xiugaiqian))->getField('bmming');
		$xghou=M('bumen')->where(array("gongsiid"=>session("gongsiid"),'id'=>$xiugaihou))->getField('bmming');
		return "部门：[".$xgqian."]=>[".$xghou."]；";
	}

	//获取岗位变动信息
	function get_xgjs($xiugaiqian,$xiugaihou,$remark){
		$xgqian=M('role')->where(array("gongsiid"=>session("gongsiid"),'id'=>$xiugaiqian))->getField('remark');
		$xghou=M('role')->where(array("gongsiid"=>session("gongsiid"),'id'=>$xiugaihou))->getField('remark');
		return "岗位：[".$xgqian."]=>[".$xghou."]；";
	}

	//获取提成
	function ticheng($ticheng_fa,$fcyj1,$fcyj2,$fcyj3,$fcyj4){
		$ticheng_arr = M('ticheng_config')->where(array("gongsiid"=>session("gongsiid"),'pid'=>$ticheng_fa))->select();
		foreach ($ticheng_arr as $key => $value) {
			$pieces = explode(",", $value['yj_type']);
			$yeji = 0;
			foreach ($pieces as $k => $v) {
				$varname = "fcyj".$v;
				$yeji = $yeji + $$varname; 
			}
			$arr = M('ticheng_config_s')->where(array("gongsiid"=>session("gongsiid"),'pid'=>$value['id']))->order('value asc')->select();
			$obj = 0;
			if ($value['jisuanfs']==1) {
				foreach ($arr as $k1 => $v1) {
					if ($yeji<=$v1['value']) {
						$obj = $yeji*$v1['bili']/100;
						break;
					}
				}
			} elseif ($value['jisuanfs']==2) {
				foreach ($arr as $k2 => $v2) {
					if ($yeji > $v2['value']) {
						$obj = $obj+($v2['value']-$arr[$k2-1]['value'])*$v2['bili']/100;
					}else{
						$obj = $obj+($yeji-$arr[$k2-1]['value'])*$v2['bili']/100;
						break;
					}
				}
			}
			$sum = $sum + $obj;
		}
		return $sum;
	}