ps : 由于前端将userId封装为BigInt类型 , 导致有精度损失, 传入的userId不正确 , 部分功能无法正确实现 , 但是代码已经完善
1.4 更新用户角色信息接口说明
1)原型效果
2)接口说明
功能描述:更新用户角色信息
服务路径:/user/roles
服务方法:Put
请求参数格式:
{"userId": 1247078461865070592,"roleIds": [1237258113002901515,1245949043784421376]
}
响应数据格式:
{"code": 1,"msg": "操作成功"
}
代码实现
1. 表现层
/*** 更新用户角色信息* @param putRoleReqVo* @return*/@PutMapping("/user/roles")public R<String> putRoles(@RequestBody PutRoleReqVo putRoleReqVo , HttpServletRequest request){return UserService.putRoleReqVo(putRoleReqVo,request);}
2. 服务层
R<String> putRoleReqVo(PutRoleReqVo putRoleReqVo, HttpServletRequest request);
@Overridepublic R<String> putRoleReqVo(PutRoleReqVo putRoleReqVo, HttpServletRequest request) {// 获取该用户数据Long userId = Long.valueOf(putRoleReqVo.getUserId());List<Long> roleIds = putRoleReqVo.getRoleIds();// 查询该用户所拥有的角色信息List<Long> userRoleList = sysUserRoleMapper.getRole(userId);// // 获取所有要删除的和要添加的
// for (Long roleId : roleIds) {
// // 如果数据存在
// if (userRoleList.contains(roleId)){
// continue;
// }else {
// delIDs.add(roleId);
// }
// }
//
// for (Long roleID : userRoleList) {
// if (roleIds.contains(roleID)){
// continue;
// }else {
// addIDs.add(roleID);
// }
// }List<Long> delIDs = roleIds.stream().filter(item -> !userRoleList.contains(item)).collect(Collectors.toList());List<Long> addIDs = userRoleList.stream().filter(item -> !roleIds.contains(item)).collect(Collectors.toList());if (delIDs != null){// 执行删除操作for (Long delID : delIDs) {sysUserRoleMapper.deleteByRoleId(userId,delID);}}ArrayList<HashMap<String, Object>> data = new ArrayList<>();if (addIDs != null){// 执行添加操作for (Long addID : addIDs) {// 通过雪花算法创建idlong id = idWorker.nextId();HashMap<String, Object> map = new HashMap<>();map.put("id",id);map.put("user_id" , userId);map.put("role_id",addID);data.add(map);}int res = sysUserRoleMapper.addByRoleId(data);}return null;}
3. Dao层
List<Long> getRole(@Param("id") Long id);
<select id="getRole" resultType="java.lang.Long">select role_id from sys_user_role where user_id = #{id};</select>
1.5 批量删除用户信息接口说明
1)原型效果
2)接口说明
功能描述: 批量删除用户信息,delete请求可通过请求体携带数据
服务路径:/user
服务方法:Delete
请求数据格式:
[1473296822679244800,1473296022544453632]
接口提示:请求参数:@RequestBody List userIds
响应数据格式:
{"code": 1,"msg": "操作成功"
}
代码实现
1. 表现层
/*** 删除用户*/@DeleteMapping("/user")public R<String> delRoles(@PathVariable ArrayList rolesList){return UserService.delRoles(rolesList);}
2. 服务层
R<String> delRoles(ArrayList rolesList);
@Overridepublic R<String> delRoles(ArrayList rolesList) {// 执行删除操作int res = sysRoleMapper.delUser(rolesList);if (res > 0){log.info("删除成功");}else {log.error("删除失败");return R.error(ResponseCode.ERROR.getMessage());}return R.ok("操作成功");}
3. Dao层
int delUser(@Param("rolesList") ArrayList rolesList);
<delete id="delUser">update sys_userset deleted = 0where sys_user.id =<foreach collection="rolesList" item="role" open="(" separator="," close=")">#{role}</foreach></delete>
1.6 根据用户id查询用户信息
1)原型效果
2)接口说明
功能描述: 根据用户id查询用户信息
服务路径:/api/user/info/{userId}
服务方法:Get
请求参数:String id
响应数据格式:
{"code": 1,"data": {"id": "1247515643591397376","username": "admin123","phone": "13699999999","nickName": "admin测试","realName": "admin测试","sex": 1,"status": 1,"email": "admin123@qq.com"}
}
代码实现
1. 表现层
/*** 根据用户id查询用户信息* @param userId* @return*/@GetMapping("/user/info/{userId}")public R<HashMap<String,Object>> getUserMsg(@PathVariable String userId){return UserService.getUserMsg(userId);}
2. 服务层
R<HashMap<String, Object>> getUserMsg(String userId);
@Overridepublic R<HashMap<String, Object>> getUserMsg(String userId) {Long user_id = Long.valueOf(userId);// 查询用户数据HashMap<String,Object> user_data= sysUserMapper.getUserMsg(user_id);return R.ok(user_data);}
3. Dao层
HashMap<String, Object> getUserMsg(@Param("userId") Long userId);
<select id="getUserMsg" resultType="java.util.HashMap">select id, username, phone, real_name as realName, nick_name as nickName, email, status, sexfrom sys_userwhere id = #{userId}</select>
1.7 更新用户信息
1)原型效果
2)接口说明
功能描述: 根据id更新用户基本信息
服务路径:/api/user
服务方法:PUT
请求参数格式:
{id: 123456789username: 'zhangsan'phone: '18811023034'email: '345@163.com'nickName: '老王'realName: '王五'sex: '1'createWhere: '1'status: '1'
}
响应数据格式:
{ "code": 1, "msg": "操作成功"}
代码实现
1. 表现层
/*** 更新用户信息* @param userDataDomain* @return*/@PutMapping("/user")public R<String> updataUser(@RequestBody UserDataDomain userDataDomain){return UserService.updataUser(userDataDomain);}
2. 服务层
R<String> updataUser(UserDataDomain userDataDomain);
@Overridepublic R<String> updataUser(UserDataDomain userDataDomain) {// 更新数据int res = sysUserMapper.updataUser(userDataDomain);if (res > 0){log.info("删除成功");}else {log.error("删除失败");return R.error(ResponseCode.ERROR.getMessage());}return R.ok("操作成功");}
3. Dao层
int updataUser(@Param("userDataDomain") UserDataDomain userDataDomain);
<update id="updataUser">update sys_userset username = #{userDataDomain.username},phone = #{userDataDomain.phone} ,email = #{userDataDomain.email} , nick_name = #{userDataDomain.nickName},real_name = #{userDataDomain.realName}, sex = #{userDataDomain.sex},create_where = #{userDataDomain.createWhere} , status = #{userDataDomain.status}where sys_user.id = #{userDataDomain.id};</update>