Identity Card Verification – Backend

Time:2019-9-11

Thinkphp5 + layui

Code:

public function add()
{
	if($this->request->isAjax())
	{
		$request = new Request();     
		$params = $this->request->param();   
		$title = $params['title']; 
		$identity = $params['identity'];
		if(empty($identity))
		{
			This - > error ('Please enter your ID card number or passport number! ).
		} 
		if( strlen($identity ) != 18 ) 
		{
			This - > error ('The ID number you entered is incorrect! ).
		}
		$crad_id_base = substr($identity , 0, 17);
		$crad_id_verify = substr($identity , -1);
		// Weighting factor 
		$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3);
		// Corresponding Value of Check Code 
		$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
		$checksum = 0;
		for( $i = 0; $i < strlen($crad_id_base); $i++ ) 
		{
			$checksum += substr($crad_id_base, $i, 1) * $factor[$i % 10];
		}
		$mod = $checksum % 11;
		if( $crad_id_verify == $verify_number_list[$mod] ) 
		{
			$result = Db::name('volunteers')->where(['identity'=>$identity])->find();
			if($result)
			{
				This - > error ('The volunteer ID number already exists, please do not add it again! ).
			}else
			{
				$data = [
					'title'          => isset($params['title']) ? $params['title'] : '',
					'volun_no'       => isset($params['volun_no']) ? $params['volun_no'] : '',
					'userid'         => isset($params['userid']) ? $params['userid'] : 0,
					'createtime'     => time(),
					'updatetime'     => time(),
					'identity'       => $params['identity'],
					'identity_type'  => isset($params['identity_type']) ? $params['identity_type'] : 1,
				]; 
				if($result = Db::name('volunteers')->insert($data))
				{
					Return $this - > success ('Volunteers add success! ).
				}else{
					Return $this - > error ('Volunteer add failed! ).
				} 
				return;
			} 
			return;	
		}                                                       
	}      
	return $this->fetch();
}