package com.terra.system.controller.sys; import com.terra.system.annotation.SysLog; import com.terra.common.controller.all.BaseController; import com.terra.common.entity.all.ResponseMsg; import com.terra.system.entity.all.SettingData; import com.terra.common.entity.all.StaticData; import com.terra.system.entity.sys.TokenEntity; import com.terra.system.entity.sys.UserEntity; import com.terra.system.helper.StringHelper; import com.terra.system.service.sys.TokenService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import javax.annotation.Resource; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.List; /** * 令牌表 * @author WWW * @date 2022-09-28 */ @Tag(name = "运维管理\\令牌管理") @RestController @RequestMapping("/token") public class TokenController extends BaseController { @Resource TokenService tokenService; @SysLog() @Operation(summary = "分页查询并返回记录数") @Parameters({ @Parameter(name = "name", description = "令牌", in = ParameterIn.QUERY, example = "ec101de8-1403-4d8f-ad13-edab8358399b"), @Parameter(name = "type", description = "类型", in = ParameterIn.QUERY, example = "0"), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectByPageAndCount") public ResponseMsg> selectByPageAndCount(String name, Integer type, Integer pageSize, Integer pageIndex) { try { if (pageSize < 1 || pageIndex < 1) { return fail("每页页数或分页数小于1", null); } int count = tokenService.selectCount(name, type); if (count == 0) { return success(0, null); } List rs = tokenService.selectByPage(name, type, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "插入一条") @Parameters({ @Parameter(name = "entity", description = "实体类", example = "") }) @PostMapping(value = "/insertToken", produces = "application/json; charset=UTF-8") public ResponseMsg insertToken(@RequestBody TokenEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setCreateUser(ue.getId()); } int count = tokenService.insertToken(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "创建新令牌") @Parameters({ @Parameter(name = "type", description = "令牌类别:0-临时,1-固定", in = ParameterIn.QUERY, example = "1"), @Parameter(name = "min", description = "分钟数:默认1个月", in = ParameterIn.QUERY, example = "43200") }) @GetMapping(value = "/insertNewToken") public ResponseMsg insertNewToken(Integer type, Integer min, HttpServletRequest req) { try { if (null == type || type > 1) { type = 0; } if (null == min || min < StaticData.I10) { min = SettingData.TOKEN_EXPIRE; } UserEntity ue = tokenService.getCurrentUser(req); TokenEntity te = tokenService.getNewToken(type, min, ue, req); int rows = tokenService.insertToken(te); if (0 == rows) { return fail("创建令牌失败", null); } return success(te); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "删除一条") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/deleteToken") public ResponseMsg deleteToken(int id) { try { int count = tokenService.deleteToken(id); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "删除多条") @Parameters({ @Parameter(name = "ids", description = "ID数组", schema = @Schema(type = "array"), in = ParameterIn.QUERY, example = "1,2") }) @GetMapping(value = "/deleteTokens") public ResponseMsg deleteTokens(@RequestParam List ids) { try { if (ids == null || ids.isEmpty()) { return fail("id数组不能为空", -1); } int count = tokenService.deleteTokens(ids); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "更新一条") @Parameters({ @Parameter(name = "entity", description = "实体类", example = "") }) @ResponseBody @PostMapping(value = "/updateToken", produces = "application/json; charset=UTF-8") public ResponseMsg updateToken(@RequestBody TokenEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setUpdateUser(ue.getId()); } int count = tokenService.updateToken(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "根据ID更新令牌为失效") @Parameters({ @Parameter(name = "id", description = "令牌ID", in = ParameterIn.QUERY, example = "6563") }) @GetMapping(value = "/updateExpireById") public ResponseMsg updateExpireById(Integer id, HttpServletRequest req) { try { if (null == id || id < 0) { return fail("id参数无效", 0); } TokenEntity entity = tokenService.selectOneById(id); if (null == entity) { return fail("该id不存在", 0); } UserEntity ue = tokenService.getCurrentUser(req); entity.setUpdateUser(ue.getId()); int count = tokenService.updateTokenExpire(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "根据Token更新令牌为失效") @Parameters({ @Parameter(name = "tk", description = "令牌", in = ParameterIn.QUERY, example = "2edea2a2-e307-4baa-992c-b477ce0566dd") }) @GetMapping(value = "/updateExpireByToken") public ResponseMsg updateExpireByToken(String tk, HttpServletRequest req) { try { if (StringHelper.isEmpty(tk)) { return fail("token参数无效", 0); } TokenEntity entity = tokenService.selectOneByToken(tk); if (null == entity) { return fail("该token不存在或已失效", 0); } UserEntity ue = tokenService.getCurrentUser(req); entity.setUpdateUser(ue.getId()); int count = tokenService.updateTokenExpire(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "根据ID查询") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "1658") }) @GetMapping(value = "/selectToken") public ResponseMsg selectToken(int id) { try { TokenEntity tokenEntity = tokenService.selectToken(id); return success(tokenEntity); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据ID查询(有效期内的)") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "6563") }) @GetMapping(value = "/selectOneById") public ResponseMsg selectOneById(Integer id) { try { if (null == id || id < 0) { return fail("id参数无效", null); } TokenEntity tokenEntity = tokenService.selectOneById(id); return success(tokenEntity); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据token查询(有效期内的)") @Parameters({ @Parameter(name = "tk", description = "令牌", in = ParameterIn.QUERY, example = "2edea2a2-e307-4baa-992c-b477ce0566dd") }) @GetMapping(value = "/selectOneByToken") public ResponseMsg selectOneByToken(String tk) { try { if (StringHelper.isEmpty(tk)) { return fail("token参数无效", null); } TokenEntity tokenEntity = tokenService.selectOneByToken(tk); return success(tokenEntity); } catch (Exception ex) { return fail(ex, null); } } }