<?php
/*
===========================注册模块控制器
*/
class cls_tab_sys_verify extends cls_base_table
{
	private $perms;
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db;
		$this->obj_db=$obj_db;
		$this->perms["state"]=array("待审"=>0,"通过"=>1);
		$this->perms["type"]=array("邮箱"=>1,"身份证"=>2,"毕业证书"=>4,"营业执照"=>8);
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if(isset($this->perms["$name"])){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	function get_typelist($msgtype=0){
		//0:个人,1:企业
		$arr_type=$this->perms["type"];
		if($msgtype==0){
			unset($arr_type["营业执照"]);
		}else{
			unset($arr_type["毕业证书"]);
		}
		return $arr_type;
	}
	//保存
	function on_save($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["verify_id"]))? $arr_fields["verify_id"]=0:$arr_fields["verify_id"]=intval($arr_fields["verify_id"]);
		//保存操作
		$lng_id=$arr_fields["verify_id"];
		unset($arr_fields["verify_id"]);//释放id
		$arr_fields["verify_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["verify_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="verify_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		if(isset($arr_fields["verify_user_id"]) && $arr_return["err"]=="") self::me_refresh($arr_fields["verify_user_id"]);
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_fields["verify_addtime"]=TIME;
		$arr_return=parent::on_insert(DB_PRE."sys_verify",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update(DB_PRE."sys_verify",$arr_fields,$msg_where);
		return $arr_return;
	}
	//删除
	function on_del($arr_id)
	{
		global $obj_db;
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_user_id=array();
		$obj_result=$obj_db->query("select verify_user_id from ".DB_PRE."sys_verify where verify_id in(".$str_id.")");
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_user_id[]=$obj_rs["verify_user_id"];
		}
		$arr_return=parent::on_del(DB_PRE."sys_verify","verify_id in(".$str_id.")");
		$arr_user_id=array_unique($arr_user_id);
		foreach($arr_user_id as $item){
			self::me_refresh($item);
		}
		return $arr_return;
	}
	function on_state($arr_id,$msg_val){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		global $obj_db;
		$arr_uid=array();
		$obj_result=$obj_db->query("select verify_user_id from ".DB_PRE."sys_verify where verify_id in(".$str_id.")");
		while($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_uid[]=$obj_rs["verify_user_id"];
		}
		$arr_return["err"]=$obj_db->execute("update ".DB_PRE."sys_verify set verify_state=".intval($msg_val)." where verify_id in(".$str_id.")");
		foreach($arr_uid as $item){
			self::me_refresh($item);
		}
		return $arr_return;
	}
	function chk_verify($msg_uid,$msg_key){
		global $obj_db;
		$arr_return=array("err"=>"");
		$str_sql="select * from ".DB_PRE."sys_findpwd where findpwd_user_id=".$msg_uid." and findpwd_key='".$msg_key."' and findpwd_state=0";
		$obj_result = $obj_db->query($str_sql);
		if($obj_rs = $obj_db->fetch_array($obj_result)){
			$obj_db->execute("update ".DB_PRE."sys_findpwd set findpwd_state=1 where findpwd_user_id=".$msg_uid." and findpwd_key='".$msg_key."'");
		}else{
			$arr_return["err"]="访问已过期";
			return $arr_return;
		}
		$str_msg=$obj_db->execute("update ".DB_PRE."sys_verify set verify_state=1 where verify_user_id=".$msg_uid." and verify_type=1");
		if($str_msg!=""){
			$arr_return["err"]=$str_msg;
			return $arr_return;
		}else{
			self::me_refresh($msg_uid);
		}
	}
	function get_verify($msg_id){
		global $obj_db;
		$obj_rs=$obj_db->get_one("select sum(verify_type) as val from ".DB_PRE."sys_verify where verify_user_id=".$msg_id." and verify_state>0");
		return $obj_rs["val"];
	}
	function me_refresh($msg_uid){
		global $obj_db;
		$lng_val=self::get_verify($msg_uid);
		$obj_db->execute("update ".DB_PRE."sys_user set user_verify='".$lng_val."' where user_id=".$msg_uid);
	}
}
?>
