package cn.aiyangniu.api.controller.other; import cn.aiyangniu.api.common.util.AuthUtil; import cn.aiyangniu.api.common.util.RedisUtil; import com.alibaba.fastjson.JSONObject; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * 一些常用的业务接口类 * * @author Henry Hall * @since 2019-10-22 */ @RestController @Api(tags="常用的接口") @RequestMapping("/others") public class OtherController { @Resource private AuthUtil authUtil; @Resource private RedisUtil redisUtil; /** * getSysTime 获取当前服务器系统时间 */ @ApiOperation(value = "获取当前服务器系统时间") @RequestMapping(value="/getSysTime", method= RequestMethod.GET) public Map getSysTime(HttpServletRequest req) { Map result = new HashMap<>(); SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), sd = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式 String type = req.getParameter("type"), dt = ("1".equals(type) ? sdt.format(new Date()) : sd.format(new Date())); result.put("retCode", "1001"); result.put("retMsg", "获取成功!"); result.put("time", dt); return result; } /** * getSysRandom 获取随机数返回给前端,同时存储到缓冲数据库中 */ @ApiOperation(value = "获取请求的随机码") @RequestMapping(value="/getSysRandom", method= RequestMethod.GET) public Map getSysRandom(HttpServletRequest req) { Map result = new HashMap<>(); String sysRandom, token, retCode, retMsg, userId; token = req.getHeader("Authorization"); if(token == null || "null".equals(token)) { retCode = "1004"; retMsg = "对不起,您没有登录,请重新登录!"; } else { JSONObject jo = authUtil.getUserSession(token); if(jo != null) { userId = jo.getString("userId"); if(redisUtil.exists("sysRandom" + userId)) { sysRandom = redisUtil.get("sysRandom" + userId); long expire = redisUtil.getExpire("sysRandom" + userId); if(expire <= 0) { sysRandom = String.valueOf(Math.random()); redisUtil.add("sysRandom" + userId, sysRandom, 3600); } }else { sysRandom = String.valueOf(Math.random()); redisUtil.add("sysRandom" + userId, sysRandom, 3600); } result.put("sysRandom", sysRandom); retCode = "1001"; retMsg = "获取成功!"; } else { retCode = "1006"; retMsg = "获取失败!"; } } result.put("retCode", retCode); result.put("retMsg", retMsg); return result; } }