淘淘商城58-SSO单点登录系统之用户注册接口开发

版权声明:本文为博主原创文章,如有转载请注明出处,谢谢。 https://blog.csdn.net/pdsu161530247/article/details/82181302

目录

1.分析接口文档

2.注册接口实现

2.1服务层

2.1.1dao层

2.1.2service层

2.2表现层

2.2.1引入服务

2.2.2controller

3.测试访问


1.分析接口文档

请求的url:/user/register

参数:表单的数据:username、password、phone、email

返回值:json数据。TaotaoResult

接收参数:使用TbUser对象接收。

请求的方法:post

2.注册接口实现

2.1服务层

2.1.1dao层

查询tb_user表,直接使用逆向工程

2.1.2service层

service接口

在taotao-sso-interface中编写接口

/**
	 * 用户注册
	 * @param tbuser
	 * @return
	 */
	TaotaoResult register(TbUser tbUser);

service实现类

业务逻辑:

  1. 使用TbUser接收提交的请求。
  2. 补全TbUser其他属性。
  3. 密码要进行MD5加密。
  4. 把用户信息插入到数据库中。
  5. 返回TaotaoResult。

在taotao-sso-service编写实现类

/**
	 * 将tbuser插入到tb_user表中
	 */
	@Override
	public TaotaoResult register(TbUser tbUser) {
		//1.校验用户名和密码不能为空
		if(StringUtils.isEmpty(tbUser.getUsername())) {
			return TaotaoResult.build(400, "注册失败. 请校验数据后请再提交数据.");
		}
		if(StringUtils.isEmpty(tbUser.getPassword())) {
			return TaotaoResult.build(400, "注册失败. 请校验数据后请再提交数据.");
		}
		//2.检查数据是否可用
		//2.1校验username是否可用
		TaotaoResult checkUsername = this.checkData(tbUser.getUsername(), 1);
		if(!(boolean) checkUsername.getData()) {
			return TaotaoResult.build(400, "用户名已经被注册!");
		}
		//2.1校验phone是否可用
		if(StringUtils.isNotBlank(tbUser.getPhone())) {
			TaotaoResult checkPhone = this.checkData(tbUser.getPhone(), 2);
			if(!(boolean) checkPhone.getData()) {
				return TaotaoResult.build(400, "手机号已经被注册!");
			}
		}
		//2.1校验email是否可用
		if(StringUtils.isNotBlank(tbUser.getEmail())) {
			TaotaoResult checkEmail = this.checkData(tbUser.getEmail(), 3);
			if(!(boolean) checkEmail.getData()) {
				return TaotaoResult.build(400, "邮箱已经被注册!");
			}
		}
		//3.密码md5加密
		String password = tbUser.getPassword();
		String md5DigestAsHex = DigestUtils.md5DigestAsHex(password.getBytes());
		tbUser.setPassword(md5DigestAsHex);
		//4.补全属性
		tbUser.setCreated(new Date());
		tbUser.setUpdated(tbUser.getCreated());
		//5.插入数据库中
		userMapper.insertSelective(tbUser);
		return TaotaoResult.ok();
	}

配置applicationContext-service.xml发布服务

<dubbo:service interface="com.taotao.sso.service.UserRegisterService" ref="userRegisterServiceImpl" timeout="300000"/>

2.2表现层

2.2.1引入服务


    在springmvc.xml中引入服务

<dubbo:reference interface="com.taotao.sso.service.UserRegisterService" id="userRegisterService" timeout="300000" />
    

2.2.2controller

请求的url:/user/register

参数:表单的数据:username、password、phone、email

返回值:json数据。TaotaoResult

接收参数:使用TbUser对象接收。

请求的方法:post

/**
	 * 用户注册。接收表单参数
	 * @param tbUser
	 * @return
	 */
	@RequestMapping(value="/user/register",method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult register(TbUser tbUser) {
		TaotaoResult result = userRegisterService.register(tbUser);
		return result;
	}

3.测试访问

安装taotao-sso、启动taotao-sso、taotao-sso-web

使用RESETClient工具测试接口

设置url与method

封装body。首先需要修改content-type为表单类型application/x-www-form-urlencoded

然后封装表单

使用一个已存在的username=zhangsan

使用不存在的username、email、phone注册测试

注册成功

在数据库中可以看到新增的用户

猜你喜欢

转载自blog.csdn.net/pdsu161530247/article/details/82181302
今日推荐