login (mobile email usercode)

手机、邮箱、用户编码登录

package login;


/**
 * 常量类(改造拉到你自己项目里面)
 *
 * @author ZengWenFeng
 * @date 2023.09.17
 * @email [email protected]
 * @mobile 13805029595
 */
public class Constant
{

	// 登录界面的登录方式:手机、邮箱、用户名
	public static final String LOGIN_TYPE_MOBILE = "MOBILE";
	public static final String LOGIN_TYPE_EMAIL = "EMAIL";
	public static final String LOGIN_TYPE_USERNAME = "USERNAME";
	
	
	public Constant()
	{
		
	}

}

package login;

/**
 * 用户实体类
 *
 * @author ZengWenFeng
 * @date 2023.09.17
 * @email [email protected]
 * @mobile 13805029595
 */
public class User
{
	private String id;
	private String usercode;
	private String username;
	private String email;
	private String mobile;
	private String password;

	//手机验证码(放另外一张表)
	//手机验证码生成时间
	//手机验证码结束时间

	public User()
	{

	}

	public User(String id, String usercode, String username, String email, String mobile, String password)
	{
		this.id = id;
		this.usercode = usercode;
		this.username = username;
		this.email = email;
		this.mobile = mobile;
		this.password = password;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getUsername()
	{
		return username;
	}

	public String getUsercode()
	{
		return usercode;
	}

	public void setUsercode(String usercode)
	{
		this.usercode = usercode;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getMobile()
	{
		return mobile;
	}

	public void setMobile(String mobile)
	{
		this.mobile = mobile;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}
}

package login;

import java.util.ArrayList;

/**
 * 模拟服务端登录服务类(改造拉到你自己项目里面)
 *
 * @author ZengWenFeng
 * @date 2023.09.17
 * @email [email protected]
 * @mobile 13805029595
 */
public class LoginService
{	
	public LoginService()
	{
		
	}

	// 模拟用户数据库
	private ArrayList listUser = new ArrayList(); 

	// 注册用户
	public void initUser(User user)
	{
		listUser.add(user);
	}

	// 登录方法
	public User login(String identifier, String password)
	{
		String email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		String moblie = "^[1][34578]\\d{9}$";
		
		//
		User user = null;
		
		// 手机号登录,正常来说,判断手机是没法子用正则,因为手机变化太快了!!
		// 这个地方还是要用界面进来的类型来判断,不然的话
		// 【你如何确定   123 0000 0000 它就一定不是手机号码呢】
		// 【正则表达式有个致命问题,就是一旦变化没法子及时匹配】
		if(identifier.matches(moblie))
		{
			user = getUserByMobile(listUser, identifier);
		}
		// 邮箱登录
		else if(identifier.matches(email))
		{
			user = getUserByEmail(listUser, identifier);
		}
		// 用户名登录
		else
		{
			//如果输入11201234567,识别不到电话号码,就会直接进来
			//这个时候11201234567被当做username去查询肯定为null
			//然而数据库有可能存在User user3 = new User("user3", "[email protected]", "11201234567", "kissme");
			user = getUserByUsercode(listUser, identifier);
			
			//----------------测试的时候可以注释下面
			if (user == null)
			{
				user = getUserByMobile(listUser, identifier);
			}
			//----------------
		}
		
		
		if (user != null && user.getPassword().equals(password))
		{
			return user;
		}
		else
		{
			return null; // 登录失败
		}
	}
	
	
	public User login2(String type, String identifier, String password)
	{
		//
		User user = null;
		
		// 手机号登录,正常来说,判断手机是没法子用正则,因为手机变化太快了!!
		// 这个地方还是要用界面进来的类型来判断,不然的话
		// 【你如何确定   123 0000 0000 它就一定不是手机号码呢】
		if(type.equals(Constant.LOGIN_TYPE_MOBILE))
		{
			user = getUserByMobile(listUser, identifier);
		}
		// 邮箱登录
		else if(type.equals(Constant.LOGIN_TYPE_EMAIL))
		{
			user = getUserByEmail(listUser, identifier);
		}
		// 用户名登录
		else
		{
			user = getUserByUsercode(listUser, identifier);
		}
		
		
		if (user != null && user.getPassword().equals(password))
		{
			return user;
		}
		else
		{
			return null; // 登录失败
		}
	}
	
	public String getIdByMobile(ArrayList listUser, String identifier)
	{
		return null;
	}
	
	public String getIdByEmail(ArrayList listUser, String identifier)
	{
		return null;
	}
	
	public String getIdByUsername(ArrayList listUser, String identifier)
	{
		return null;
	}
	

	public User getUserByMobile(ArrayList listUser, String identifier)
	{
		for (int i = 0; i < listUser.size(); i++)
		{
			User curUser = listUser.get(i);
			
			if (curUser != null && curUser.getMobile() != null && curUser.getMobile().equals(identifier))
			{
				return curUser;
			}
		}
		
		return null;
	}
	
	public User getUserByEmail(ArrayList listUser, String identifier)
	{
		for (int i = 0; i < listUser.size(); i++)
		{
			User curUser = listUser.get(i);
			
			if (curUser != null && curUser.getEmail() != null && curUser.getEmail().equals(identifier))
			{
				return curUser;
			}
		}
		
		return null;
	}
	
	public User getUserByUsercode(ArrayList listUser, String identifier)
	{
		for (int i = 0; i < listUser.size(); i++)
		{
			User curUser = listUser.get(i);
			
			if (curUser != null && curUser.getUsercode() != null && curUser.getUsercode().equals(identifier))
			{
				return curUser;
			}
		}
		
		return null;
	}



}

package login;


/**
 * 模拟用户界面UI登录
 * 
 * 
 * @author ZengWenFeng
 * @date 2023.09.17
 * @email [email protected]
 * @mobile 13805029595
 */
public class LoginTest
{

	public LoginTest()
	{
		
	}

	public static void main(String[] args)
	{
		LoginService loginService = new LoginService();

		// 注册一些用户
		User user1 = new User("005126", "user1", "曾大锋", "[email protected]", "01234567891", "1314");
		User user2 = new User("005127", "user2", "曾二锋", "[email protected]", "12301234567", "iloveu");
		User user3 = new User("005128", "user3", "曾三锋", "[email protected]", "11201234567", "kissme");
		User user4 = new User("005129", "user4", "曾小锋", "[email protected]", "13805029595", "kissme");

		loginService.initUser(user1);
		loginService.initUser(user2);
		loginService.initUser(user3);
		loginService.initUser(user4);

		// -----------------
		// --模拟登录
		// 使用手机号、邮箱或用户名登录
		// -----------------
		//email、mobile、username,因为手机号这个变动太大
		
		//【界面操作 1:选择类型 2:输入手机或用户或邮箱 3:输入密码】
		//【简单的问题: 112-0123-4567 开头的11位字符串  算手机号码 还是用户名呢】
		//【简单的问题: 112-0123-4567 假设移动开出的系列号码】
		//【手机号码长度就一定是11位,不会像身份证从15到18】
		String type = "moble";
		type = Constant.LOGIN_TYPE_MOBILE;
		
		String identifier = "13805029595";
		identifier = "11201234567";
		String password = "kissme";

		User loginUser = loginService.login(identifier, password);
		if (loginUser != null)
		{
			System.out.println("登录成功,用户信息:" + loginUser.getUsername());
		}
		else
		{
			System.out.println("登录失败,用户名或密码不正确");
		}
		
		
		loginUser = loginService.login2(type, identifier, password);
		if (loginUser != null)
		{
			System.out.println("登录成功,用户信息:" + loginUser.getUsername());
		}
		else
		{
			System.out.println("登录失败,用户名或密码不正确");
		}
	}
}

login (mobile email usercode)_第1张图片

login (mobile email usercode)_第2张图片

login (mobile email usercode)_第3张图片

login (mobile email usercode)_第4张图片

最后奖励自己一个【鸡腿】

login (mobile email usercode)_第5张图片

你可能感兴趣的:(java,java)