OtherController.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cn.aiyangniu.api.controller.other;
  2. import cn.aiyangniu.api.common.util.AuthUtil;
  3. import cn.aiyangniu.api.common.util.RedisUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import javax.annotation.Resource;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * 一些常用的业务接口类
  18. *
  19. * @author Henry Hall
  20. * @since 2019-10-22
  21. */
  22. @RestController
  23. @Api(tags="常用的接口")
  24. @RequestMapping("/others")
  25. public class OtherController {
  26. @Resource
  27. private AuthUtil authUtil;
  28. @Resource
  29. private RedisUtil redisUtil;
  30. /**
  31. * getSysTime 获取当前服务器系统时间
  32. */
  33. @ApiOperation(value = "获取当前服务器系统时间")
  34. @RequestMapping(value="/getSysTime", method= RequestMethod.GET)
  35. public Map<String, Object> getSysTime(HttpServletRequest req) {
  36. Map<String, Object> result = new HashMap<>();
  37. SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), sd = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
  38. String type = req.getParameter("type"), dt = ("1".equals(type) ? sdt.format(new Date()) : sd.format(new Date()));
  39. result.put("retCode", "1001");
  40. result.put("retMsg", "获取成功!");
  41. result.put("time", dt);
  42. return result;
  43. }
  44. /**
  45. * getSysRandom 获取随机数返回给前端,同时存储到缓冲数据库中
  46. */
  47. @ApiOperation(value = "获取请求的随机码")
  48. @RequestMapping(value="/getSysRandom", method= RequestMethod.GET)
  49. public Map<String, Object> getSysRandom(HttpServletRequest req) {
  50. Map<String, Object> result = new HashMap<>();
  51. String sysRandom, token, retCode, retMsg, userId;
  52. token = req.getHeader("Authorization");
  53. if(token == null || "null".equals(token)) {
  54. retCode = "1004";
  55. retMsg = "对不起,您没有登录,请重新登录!";
  56. } else {
  57. JSONObject jo = authUtil.getUserSession(token);
  58. if(jo != null) {
  59. userId = jo.getString("userId");
  60. if(redisUtil.exists("sysRandom" + userId)) {
  61. sysRandom = redisUtil.get("sysRandom" + userId);
  62. long expire = redisUtil.getExpire("sysRandom" + userId);
  63. if(expire <= 0) {
  64. sysRandom = String.valueOf(Math.random());
  65. redisUtil.add("sysRandom" + userId, sysRandom, 3600);
  66. }
  67. }else {
  68. sysRandom = String.valueOf(Math.random());
  69. redisUtil.add("sysRandom" + userId, sysRandom, 3600);
  70. }
  71. result.put("sysRandom", sysRandom);
  72. retCode = "1001";
  73. retMsg = "获取成功!";
  74. } else {
  75. retCode = "1006";
  76. retMsg = "获取失败!";
  77. }
  78. }
  79. result.put("retCode", retCode);
  80. result.put("retMsg", retMsg);
  81. return result;
  82. }
  83. }