一.电商管理后台 API 接口
1.1API V1 接口说明
- 接口基准地址:
http://127.0.0.1:8888/api/private/v1/ - 服务端已开启 CORS 跨域支持
- API V1 认证统一使用 Token 认证
- 需要授权的 API ,必须在请求头中使用
Authorization 字段提供 token 令牌 - 使用 HTTP Status Code 标识状态
- 数据返回格式统一使用 JSON
1.2 支持的请求方法
- GET(SELECT):从服务器取出资源(一项或多项)。
- POST(CREATE):在服务器新建一个资源。
- PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。
- PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)。
- DELETE(DELETE):从服务器删除资源。
- HEAD:获取资源的元数据。
- OPTIONS:获取信息,关于资源的哪些属性是客户端可以改变的。
1.3 通用返回状态说明
状态码 | 含义 | 说明 |
---|
200 | OK | 请求成功 | 201 | CREATED | 创建成功 | 204 | DELETED | 删除成功 | 400 | BAD REQUEST | 请求的地址不存在或者包含不支持的参数 | 401 | UNAUTHORIZED | 未授权 | 403 | FORBIDDEN | 被禁止访问 | 404 | NOT FOUND | 请求的资源不存在 | 422 | Unprocesable entity | [POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误 | 500 | INTERNAL SERVER ERROR | 内部错误 | | | |
二.登录
2.1登录验证接口
参数名 | 参数说明 | 备注 |
---|
username | 用户名 | 不能为空 | password | 密码 | 不能为空 |
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | | rid | 用户角色 ID | | username | 用户名 | | mobile | 手机号 | | email | 邮箱 | | token | 令牌 | 基于 jwt 的令牌 |
{
"data": {
"id": 500,
"rid": 0,
"username": "admin",
"mobile": "123",
"email": "123@qq.com",
"token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1MTI1NDQyOTksImV4cCI6MTUxMjYzMDY5OX0.eGrsrvwHm-tPsO9r_pxHIQ5i5L1kX9RX444uwnRGaIM"
},
"meta": {
"msg": "登录成功",
"status": 200
}
}
后端实现
数据返回实体类封装
Meta.java
package com.vueshop.manager.controller.http.response.base;
import lombok.Data;
@Data
public class Meta {
private String msg;
private Integer status;
}
HttpResponse.java
package com.vueshop.manager.controller.http.response.base;
import lombok.Data;
@Data
public class HttpResponse<T> {
private T data;
private Meta meta;
public HttpResponse() {
}
public HttpResponse(Meta meta) {
this.meta = meta;
}
}
用户请求实体类和用户响应实体类
UserInfoResponse.java
package com.vueshop.manager.controller.http.response;
import lombok.Data;
@Data
public class UserInfoResponse {
private Long id;
private Long rid;
private String username;
private String mobile;
private String email;
private String token;
private String password;
private String createtime;
private String updatetime;
}
UserInfoRequest.java
package com.vueshop.manager.controller.http.request;
import lombok.Data;
@Data
public class UserInfoRequest {
private Long id;
private String username;
private String password;
private Integer status;
private String email;
private String mobile;
private Integer rid;
}
LoginController.java
@RestController
@RequestMapping("api/private/v1/")
@Slf4j
public class LoginController {
@Autowired
private LoginService loginService;
@PostMapping("login")
public HttpResponse<UserInfoResponse> login(HttpServletRequest request,
@RequestBody UserInfoRequest userInfoRequest){
HttpResponse<UserInfoResponse> httpResponse=new HttpResponse<>(new Meta());
try {
UserInfoResponse userInfoResponse = loginService.queryUserInfo(userInfoRequest);
if (userInfoResponse == null || !userInfoResponse.getPassword().equals(userInfoRequest.getPassword())) {
httpResponse.getMeta().setMsg("登录失败");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("登录成功");
httpResponse.getMeta().setStatus(200);
} catch (Exception e) {
log.error(e.getMessage());
httpResponse.getMeta().setMsg("登录失败");
httpResponse.getMeta().setStatus(400);
}
return httpResponse;
}
LoginServiceImpl.java
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
@Autowired
private UserInfoDao userInfoDao;
@Autowired
private Map<String, AuthInfoDto> redisCache;
@Autowired
private MenuInfoDao menuInfoDao;
@Override
public UserInfoResponse queryUserInfo(UserInfoRequest userInfoRequest) {
UserInfo userInfo = userInfoDao.queryUserInfo(userInfoRequest.getUsername());
if(userInfo==null){
return null;
}
List<MenuInfo> menuInfos = menuInfoDao.queryMenuInfoByRoleId(userInfo.getRid());
UserInfoResponse userInfoResponse=new UserInfoResponse();
BeanUtils.copyProperties(userInfo,userInfoResponse);
String token= UUID.randomUUID().toString().replace("-","");
userInfoResponse.setToken(token);
AuthInfoDto authInfoDto=new AuthInfoDto();
authInfoDto.setUserInfo(userInfo);
authInfoDto.setMenuInfos(menuInfos);
redisCache.put(token,authInfoDto);
return userInfoResponse;
}
}
UserInfoDaoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vueshop.manager.dao.mapper.UserInfoDao">
<resultMap id="UserInfoMap" type="com.vueshop.manager.dao.model.UserInfo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="rid" property="rid" jdbcType="BIGINT"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="mobile" property="mobile" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
<result column="token" property="token" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="role_name" property="roleName" jdbcType="VARCHAR"/>
<result column="createtime" property="createtime" jdbcType="TIMESTAMP"/>
<result column="updatetime" property="updatetime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="allColumn">
id,
rid,
username,
mobile,
email,
token,
password,
status,
createtime,
updatetime
</sql>
<select id="queryUserInfo" resultMap="UserInfoMap">
select <include refid="allColumn"/>
from user_info
where username= #{username}
AND status=1
</select>
</mapper>
MenuInfoDaoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vueshop.manager.dao.mapper.MenuInfoDao">
<resultMap id="MenuInfoMap" type="com.vueshop.manager.dao.model.MenuInfo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="auth_name" property="authName" jdbcType="VARCHAR"/>
<result column="path" property="path" jdbcType="VARCHAR"/>
<result column="pid" property="pid" jdbcType="BIGINT"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="display" property="display" jdbcType="TINYINT"/>
<result column="level" property="level" jdbcType="TINYINT"/>
<result column="createtime" property="createtime" jdbcType="TIMESTAMP"/>
<result column="updatetime" property="updatetime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="allColumn">
id,
auth_name,
path,
pid,
status,
display,
level,
createtime,
updatetime
</sql>
<select id="queryMenuInfoByRoleId" resultMap="MenuInfoMap" >
SELECT
um.id,
um.auth_name,
um.path,
um.pid,
um.status,
um.display,
um.level,
um.createtime,
um.updatetime
FROM user_menus um
RIGHT JOIN user_role_menu urm ON um.id = urm.menu_id
WHERE STATUS = 1
AND urm.role_id =#{roleId}
ORDER BY LEVEL ASC
</select>
</mapper>
拦截器 AuthInterceptor.java
@Slf4j
@Component
public class AuthInterceptor implements HandlerInterceptor {
@Autowired
protected Map<String, AuthInfoDto> redisCache;
private static final List<String> menuWhiteList = Arrays.asList("home", "menus", "login");
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
AuthInfoDto authInfoDto = loginAuthorizationCheck(request);
if (authInfoDto.isNotAuth()) {
HttpResponse httpResponse = new HttpResponse();
Meta meta = new Meta();
meta.setMsg(authInfoDto.getErrMsg());
meta.setStatus(400);
httpResponse.setMeta(meta);
response.setHeader("Content-type", "text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(JSON.toJSONString(httpResponse));
return false;
}
return true;
}
public AuthInfoDto loginAuthorizationCheck(HttpServletRequest request) {
AuthInfoDto authInfoDto = redisCache.get(request.getHeader("Authorization"));
if (authInfoDto == null) {
authInfoDto = new AuthInfoDto();
authInfoDto.setErrMsg("未登录");
authInfoDto.setNotAuth(true);
return authInfoDto;
}
boolean isAccept = false;
String requestURI = request.getRequestURI();
for (MenuInfo menuInfo : authInfoDto.getMenuInfos()) {
if (menuInfo.getPath() == null) {
continue;
}
if (requestURI.contains(menuInfo.getPath())) {
isAccept = true;
break;
}
}
for (String uri : menuWhiteList) {
if (requestURI.contains(uri)) {
isAccept = true;
break;
}
}
if (!isAccept) {
authInfoDto.setErrMsg("没有权限访问该路径");
authInfoDto.setNotAuth(true);
}
return authInfoDto;
}
}
InterceptorConfig.java
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
AuthInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.addPathPatterns("/api/private/v1/**")
.excludePathPatterns("/api/private/v1/login")
.excludePathPatterns("/api/private/v1/historyBrief/**")
.excludePathPatterns("/api/private/v1/historyBriefEvent/**");
}
}
三.用户管理
3.1用户数据列表
参数名 | 参数说明 | 备注 |
---|
query | 查询参数 | 可以为空 | pagenum | 当前页码 | 不能为空 | pagesize | 每页显示条数 | 不能为空 |
参数名 | 参数说明 | 备注 |
---|
totalpage | 总记录数 | | pagenum | 当前页码 | | users | 用户数据集合 | |
{
"data": {
"totalpage": 5,
"pagenum": 4,
"users": [
{
"id": 25,
"username": "tige117",
"mobile": "18616358651",
"type": 1,
"email": "tige112@163.com",
"create_time": "2017-11-09T20:36:26.000Z",
"mg_state": true,
"role_name": "炒鸡管理员"
}
]
},
"meta": {
"msg": "获取成功",
"status": 200
}
}
后端实现
分页实体类 PageResponse.java
@Data
public class PageResponse {
private Integer total;
private Integer pagenum;
}
用户查询响应实体类继承分页实体类UserInfoQueryResponse.java
@Data
public class UserInfoQueryResponse extends PageResponse {
private List<UserInfo> users;
}
用户分页查询封装实体类PageRequest.java
@Data
public class PageRequest<T> {
private Integer pagenum;
private Integer pagesize;
private Integer pagestart;
private T query;
public PageRequest(Integer pagenum, Integer pagesize) {
this.pagenum = pagenum;
this.pagesize = pagesize;
if(pagenum != null && pagesize != null){
this.pagestart = (pagenum - 1) * pagesize;
}
}
public PageRequest() {
}
}
@RestController
@RequestMapping("api/private/v1/")
@Slf4j
public class UsersController {
@Autowired
private UsersService usersService;
@GetMapping("users")
public HttpResponse<UserInfoQueryResponse> queryUserPage(HttpServletRequest request) {
HttpResponse<UserInfoQueryResponse> httpResponse = new HttpResponse<>(new Meta());
String pagenum = request.getParameter("pagenum");
String pagesize = request.getParameter("pagesize");
String query = request.getParameter("query");
PageRequest<UserInfoQueryRequest> pageRequest = new PageRequest(Integer.parseInt(pagenum),
Integer.parseInt(pagesize));
UserInfoQueryRequest userInfoQueryRequest = new UserInfoQueryRequest();
userInfoQueryRequest.setUsername(query);
pageRequest.setQuery(userInfoQueryRequest);
UserInfoQueryResponse userInfoPageResponse = usersService.queryUserPage(pageRequest);
httpResponse.setData(userInfoPageResponse);
httpResponse.getMeta().setMsg("获取菜单列表成功");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
}
UsersServiceImpl.java
@Service
@Slf4j
public class UsersServiceImpl implements UsersService {
@Autowired
private UserInfoDao userInfoDao;
@Override
public UserInfoQueryResponse queryUserPage(PageRequest<UserInfoQueryRequest> pageRequest) {
UserInfoQueryResponse res = new UserInfoQueryResponse();
List<UserInfo> userInfos = userInfoDao.queryUserPage(pageRequest.getQuery(), pageRequest.getPagestart(), pageRequest.getPagesize());
Integer count = userInfoDao.queryUserPageCount(pageRequest.getQuery());
for (UserInfo userInfo : userInfos) {
userInfo.setMg_state(userInfo.getStatus() == 1 ? true : false);
}
res.setTotal(count);
res.setPagenum(pageRequest.getPagenum());
res.setUsers(userInfos);
return res;
}
}
UserInfoDaoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vueshop.manager.dao.mapper.UserInfoDao">
<resultMap id="UserInfoMap" type="com.vueshop.manager.dao.model.UserInfo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="rid" property="rid" jdbcType="BIGINT"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="mobile" property="mobile" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
<result column="token" property="token" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="role_name" property="roleName" jdbcType="VARCHAR"/>
<result column="createtime" property="createtime" jdbcType="TIMESTAMP"/>
<result column="updatetime" property="updatetime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="allColumn">
id,
rid,
username,
mobile,
email,
token,
password,
status,
createtime,
updatetime
</sql>
<select id="queryUserPage" resultMap="UserInfoMap">
SELECT
ur.role_name,
ui.id,
ui.rid,
ui.username,
ui.mobile,
ui.email,
ui.token,
ui.password,
ui.status,
ui.createtime,
ui.updatetime
FROM user_info ui
LEFT JOIN user_role ur
ON ui.rid = ur.id
<where>
<if test=" query.username != null and query.username != ''">
AND ui.username =#{query.username}
</if>
</where>
ORDER BY ui.id ASC
LIMIT #{pageNo} , #{pageSize}
</select>
</mapper>
3.2. 添加用户
参数名 | 参数说明 | 备注 |
---|
username | 用户名称 | 不能为空 | password | 用户密码 | 不能为空 | email | 邮箱 | 可以为空 | mobile | 手机号 | 可以为空 |
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | | rid | 用户角色 ID | | username | 用户名 | | mobile | 手机号 | | email | 邮箱 | |
{
"data": {
"id": 28,
"username": "tige1200",
"mobile": "test",
"type": 1,
"openid": "",
"email": "test@test.com",
"create_time": "2017-11-10T03:47:13.533Z",
"modify_time": null,
"is_delete": false,
"is_active": false
},
"meta": {
"msg": "用户创建成功",
"status": 201
}
}
后端实现
用户添加实体类UserInfoRequest.java
@Data
public class UserInfoRequest {
private Long id;
private String username;
private String password;
private Integer status;
private String email;
private String mobile;
private Integer rid;
}
UsersController.java
@PostMapping("users")
public HttpResponse<UserInfoResponse> addUser(HttpServletRequest request,
@RequestBody UserInfoRequest userInfoRequest) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
UserInfoResponse userInfoResponse = usersService.insertUser(userInfoRequest);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("用户创建成功");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("用户创建失败");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse insertUser(UserInfoRequest userInfoRequest) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
try {
userInfoRequest.setStatus(1);
userInfoDao.insertUserInfo(userInfoRequest);
} catch (Exception e) {
log.error("添加失败", e.getMessage());
return null;
}
BeanUtils.copyProperties(userInfoRequest, userInfoResponse);
return userInfoResponse;
}
UserInfoDaoMapper.xml
<insert id="insertUserInfo" parameterType="com.vueshop.manager.controller.http.request.UserInfoRequest">
INSERT INTO user_info (rid,username,mobile,email,password,status,createtime,updatetime)
VALUES (#{userInfo.rid},#{userInfo.username},#{userInfo.mobile},#{userInfo.email},#{userInfo.password},#{userInfo.status},NOW(),NOW())
</insert>
3.3. 修改用户状态
- 请求路径:users/:uId/state/:type
- 请求方法:put
- 请求参数
参数名 | 参数说明 | 备注 |
---|
uId | 用户 ID | 不能为空携带在url中 | type | 用户状态 | 不能为空携带在url中 ,值为 true 或者 false |
{
"data": {
"id": 566,
"rid": 30,
"username": "admin",
"mobile": "123456",
"email": "bb@itcast.com",
"mg_state": 0
},
"meta": {
"msg": "设置状态成功",
"status": 200
}
}
后端实现
UsersController.java
@PutMapping("users/{id}/state/{type}")
public HttpResponse<UserInfoResponse> updateUserStateById(HttpServletRequest request, @PathVariable long id,
@PathVariable boolean type) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
UserInfoRequest userInfoRequest = new UserInfoRequest();
userInfoRequest.setId(id);
userInfoRequest.setStatus(type ? 1 : 0);
UserInfoResponse userInfoResponse = usersService.updateUserInfoById(userInfoRequest);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("设置失败");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("设置成功");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse updateUserInfoById(UserInfoRequest userInfoRequest) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
try {
userInfoDao.updateUserInfo(userInfoRequest);
} catch (Exception e) {
log.error("更新失败", e.getMessage());
return null;
}
BeanUtils.copyProperties(userInfoRequest, userInfoResponse);
return userInfoResponse;
}
UserInfoDaoMapper.xml
<update id="updateUserInfo" parameterType="com.vueshop.manager.controller.http.request.UserInfoRequest">
UPDATE user_info
<set>
<if test=" userInfo.rid != null">
rid =#{userInfo.rid},
</if>
<if test=" userInfo.mobile != null and userInfo.mobile != ''">
mobile =#{userInfo.mobile},
</if>
<if test=" userInfo.email != null and userInfo.email != ''">
email =#{userInfo.email},
</if>
<if test=" userInfo.password != null and userInfo.password != ''">
password =#{userInfo.password},
</if>
<if test=" userInfo.status != null">
status =#{userInfo.status},
</if>
updatetime = now()
</set>
WHERE id =#{userInfo.id}
</update>
3.4. 根据 ID 查询用户信息
- 请求路径:users/:id
- 请求方法:get
- 请求参数
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | 不能为空携带在url中 |
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | | role_id | 角色 ID | | mobile | 手机号 | | email | 邮箱 | |
{
"data": {
"id": 503,
"username": "admin3",
"role_id": 0,
"mobile": "00000",
"email": "new@new.com"
},
"meta": {
"msg": "查询成功",
"status": 200
}
}
后端实现
UsersController.java
@GetMapping("users/{id}")
@ResponseBody
public HttpResponse<UserInfoResponse> queryUserById(HttpServletRequest request, @PathVariable long id) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
UserInfoResponse userInfoResponse = usersService.queryUserInfoById(id);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("查询失败");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("查询成功");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse queryUserInfoById(Long id) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
UserInfo userInfo = userInfoDao.queryUserInfoById(id);
BeanUtils.copyProperties(userInfo, userInfoResponse);
return userInfoResponse;
}
UserInfoDaoMapper.xml
<select id="queryUserInfoById" resultType="com.vueshop.manager.dao.model.UserInfo">
SELECT
<include refid="allColumn"/>
FROM user_info
WHERE id = #{id}
</select>
3.5. 编辑用户提交
- 请求路径:users/:id
- 请求方法:put
- 请求参数
参数名 | 参数说明 | 备注 |
---|
id | 用户 id | 不能为空 参数是url参数:id | email | 邮箱 | 可以为空 | mobile | 手机号 | 可以为空 |
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | | role_id | 角色 ID | | mobile | 手机号 | | email | 邮箱 | |
{
"data": {
"id": 503,
"username": "admin3",
"role_id": 0,
"mobile": "111",
"email": "123@123.com"
},
"meta": {
"msg": "更新成功",
"status": 200
}
}
后端实现
UsersController.java
@PutMapping("users/{id}")
@ResponseBody
public HttpResponse<UserInfoResponse> updateUserInfoById(HttpServletRequest request, @PathVariable long id,
@RequestBody UserInfoRequest userInfoRequest) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
userInfoRequest.setId(id);
if (userInfoRequest.getStatus() == null) {
userInfoRequest.setStatus(0);
}
UserInfoResponse userInfoResponse = usersService.updateUserInfoById(userInfoRequest);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("更新失败");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("更新成功");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse updateUserInfoById(UserInfoRequest userInfoRequest) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
try {
userInfoDao.updateUserInfo(userInfoRequest);
} catch (Exception e) {
log.error("更新失败", e.getMessage());
return null;
}
BeanUtils.copyProperties(userInfoRequest, userInfoResponse);
return userInfoResponse;
}
UserInfoDaoMapper.xml
<update id="updateUserInfo" parameterType="com.vueshop.manager.controller.http.request.UserInfoRequest">
UPDATE user_info
<set>
<if test=" userInfo.rid != null">
rid =#{userInfo.rid},
</if>
<if test=" userInfo.mobile != null and userInfo.mobile != ''">
mobile =#{userInfo.mobile},
</if>
<if test=" userInfo.email != null and userInfo.email != ''">
email =#{userInfo.email},
</if>
<if test=" userInfo.password != null and userInfo.password != ''">
password =#{userInfo.password},
</if>
<if test=" userInfo.status != null">
status =#{userInfo.status},
</if>
updatetime = now()
</set>
WHERE id =#{userInfo.id}
</update>
3.6. 删除单个用户
- 请求路径:users/:id
- 请求方法:delete
- 请求参数
参数名 | 参数说明 | 备注 |
---|
id | 用户 id | 不能为空参数是url参数:id |
{
"data": null,
"meta": {
"msg": "删除成功",
"status": 200
}
}
后端实现
UsersController.java
@DeleteMapping("users/{id}")
@ResponseBody
public HttpResponse<UserInfoResponse> deleteUserById(HttpServletRequest request, @PathVariable long id) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
UserInfoResponse userInfoResponse = usersService.deleteUserById(id);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("删除失败");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("删除成功");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse deleteUserById(Long id) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
try {
userInfoDao.deleteUserById(id);
} catch (Exception e) {
log.error("删除失败", e.getMessage());
return null;
}
return userInfoResponse;
}
UserInfoDaoMapper.xml
<delete id="deleteUserById" parameterType="java.lang.Long">
DELETE FROM user_info WHERE id = #{id}
</delete>
3.7. 分配用户角色
- 请求路径:users/:id/role
- 请求方法:put
- 请求参数
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | 不能为空参数是url参数:id | rid | 角色 id | 不能为空参数body参数 |
参数名 | 参数说明 | 备注 |
---|
id | 用户 ID | | role_id | 角色 ID | | mobile | 手机号 | | email | 邮箱 | |
{
"data": {
"id": 508,
"rid": "30",
"username": "asdf1",
"mobile": "123123",
"email": "adfsa@qq.com"
},
"meta": {
"msg": "设置角色成功",
"status": 200
}
}
后端实现
UsersController.java
@PutMapping("users/{id}/role")
@ResponseBody
public HttpResponse<UserInfoResponse> updateUserStateById(HttpServletRequest request, @PathVariable long id, @RequestBody AssignRoleRequest assignRoleRequest) {
HttpResponse<UserInfoResponse> httpResponse = new HttpResponse<>(new Meta());
String rid = assignRoleRequest.getRid();
UserInfoRequest userInfoRequest = new UserInfoRequest();
userInfoRequest.setId(id);
userInfoRequest.setRid(Integer.parseInt(rid));
UserInfoResponse userInfoResponse = usersService.updateUserInfoById(userInfoRequest);
if (userInfoResponse == null) {
httpResponse.getMeta().setMsg("设置角色成功");
httpResponse.getMeta().setStatus(400);
return httpResponse;
}
httpResponse.setData(userInfoResponse);
httpResponse.getMeta().setMsg("设置角色失败");
httpResponse.getMeta().setStatus(200);
return httpResponse;
}
UsersServiceImpl.java
@Override
public UserInfoResponse updateUserInfoById(UserInfoRequest userInfoRequest) {
UserInfoResponse userInfoResponse = new UserInfoResponse();
try {
userInfoDao.updateUserInfo(userInfoRequest);
} catch (Exception e) {
log.error("更新失败", e.getMessage());
return null;
}
BeanUtils.copyProperties(userInfoRequest, userInfoResponse);
return userInfoResponse;
}
UserInfoDaoMapper.xml
<update id="updateUserInfo" parameterType="com.vueshop.manager.controller.http.request.UserInfoRequest">
UPDATE user_info
<set>
<if test=" userInfo.rid != null">
rid =#{userInfo.rid},
</if>
<if test=" userInfo.mobile != null and userInfo.mobile != ''">
mobile =#{userInfo.mobile},
</if>
<if test=" userInfo.email != null and userInfo.email != ''">
email =#{userInfo.email},
</if>
<if test=" userInfo.password != null and userInfo.password != ''">
password =#{userInfo.password},
</if>
<if test=" userInfo.status != null">
status =#{userInfo.status},
</if>
updatetime = now()
</set>
WHERE id =#{userInfo.id}
</update>
|