package com.nq.controller; import com.nq.common.ServerResponse; import com.nq.dao.AgentUserMapper; import com.nq.pojo.AgentUser; import com.nq.pojo.GoogleAuthDto; import com.nq.pojo.UserStockSubscribe; import com.nq.service.IAgentUserService; import com.nq.service.IUserStockSubscribeService; import com.nq.service.impl.GoogleAuthenticator; import com.nq.utils.PropertiesUtil; import com.nq.utils.redis.CookieUtils; import com.nq.utils.redis.JsonUtil; import com.nq.utils.redis.RedisConst; import com.nq.utils.redis.RedisShardedPoolUtils; import com.nq.vo.agent.AgentLoginResultVO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping({"/api/agent/"}) public class AgentApiController { private static final Logger log = LoggerFactory.getLogger(AgentApiController.class); @Autowired IAgentUserService iAgentUserService; @Autowired IUserStockSubscribeService iUserStockSubscribeService; @Autowired AgentUserMapper agentUserMapper; //代理后台登录 @RequestMapping({"login.do"}) @ResponseBody public ServerResponse login(@RequestParam("agentPhone") String agentPhone, @RequestParam("agentPwd") String agentPwd, @RequestParam(value = "verifyCode", required = false, defaultValue = "") String verifyCode, @RequestParam(value = "googleAuthCode", required = false) Integer googleAuthCode, HttpSession httpSession, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { ServerResponse serverResponse = this.iAgentUserService.login(agentPhone, agentPwd, verifyCode, googleAuthCode, httpServletRequest); String token = RedisConst.getAgentRedisKey(httpSession.getId()); if (serverResponse.isSuccess()) { String redisSetExResult = RedisShardedPoolUtils.setEx(token, JsonUtil.obj2String(serverResponse.getData()), 999999); log.info("redis setex agent result : {}", redisSetExResult); AgentLoginResultVO resultVO = new AgentLoginResultVO(); resultVO.setToken(token); return ServerResponse.createBySuccess("登陆成功", resultVO); } return serverResponse; } //代理后台退出登录 @RequestMapping({"logout.do"}) @ResponseBody public ServerResponse logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { String cookie_name = PropertiesUtil.getProperty("agent.cookie.name"); String logintoken = CookieUtils.readLoginToken(httpServletRequest, cookie_name); log.info("代理 token = {} ,退出登陆", logintoken); RedisShardedPoolUtils.del(logintoken); CookieUtils.delLoginToken(httpServletRequest, httpServletResponse, cookie_name); return ServerResponse.createBySuccess(); } //申购信息列表查询 @RequestMapping({"getStockSubscribeList.do"}) @ResponseBody public ServerResponse getStockSubscribeList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "12") int pageSize, @RequestParam(value = "keyword", defaultValue = "") String keyword, @RequestParam(value = "agentId", required = false) String agentId, HttpServletRequest request) { return this.iUserStockSubscribeService.getList(pageNum, pageSize, keyword,agentId, request); } //申购信息-添加 修改 @RequestMapping({"saveStockSubscribe.do"}) @ResponseBody public ServerResponse saveStockSubscribe(UserStockSubscribe model, HttpServletRequest request) throws Exception { return this.iUserStockSubscribeService.save(model, request); } //新股申购-删除 @RequestMapping({"delStockSubscribe.do"}) @ResponseBody public ServerResponse delStockSubscribe(@RequestParam("id") int id, HttpServletRequest request) { return this.iUserStockSubscribeService.del(id, request); } @RequestMapping({"getLoginGoogleAuthSecret"}) @ResponseBody public ServerResponse getLoginGoogleAuthSecret(HttpServletRequest request) { GoogleAuthDto dto = iAgentUserService.getGoogleAuth(request); if (dto == null) { return ServerResponse.createByErrorMsg("请先登录"); } return ServerResponse.createBySuccess(dto); } @RequestMapping({"bindGoogleAuth"}) @ResponseBody public ServerResponse bindGoogleAuth(@RequestParam("googleAuthCode") String googleAuthCode, @RequestParam("secret") String secret, HttpServletRequest request) { AgentUser currentAgent = iAgentUserService.getCurrentAgent(request); if (currentAgent == null) { return ServerResponse.createByErrorMsg("请先登录"); } AgentUser agentUser = agentUserMapper.selectByPrimaryKey(currentAgent.getId()); if (agentUser == null) { return ServerResponse.createByErrorMsg("当前用户未找到"); } if (Boolean.TRUE.equals(agentUser.getGoogleAuthBind())) { return ServerResponse.createByErrorMsg("谷歌验证码已绑定"); } if (StringUtils.isBlank(secret) || StringUtils.isBlank(googleAuthCode)) { return ServerResponse.createByErrorMsg("参数不能为空"); } long t = System.currentTimeMillis(); GoogleAuthenticator ga = new GoogleAuthenticator(); ga.setWindowSize(5); boolean userFlag = ga.check_code(secret, Long.valueOf(googleAuthCode), t); if (!userFlag) { return ServerResponse.createByErrorMsg("谷歌验证码错误"); } agentUser.setGoogleAuthBind(true); agentUser.setGoogleAuthSecret(secret); agentUserMapper.updateByPrimaryKeySelective(agentUser); return ServerResponse.createBySuccess(); } @RequestMapping({"unbindingGoogleAuth"}) @ResponseBody public ServerResponse unbindingGoogleAuth(@RequestParam("rootGoogleAuthCode") String rootGoogleAuthCode, HttpServletRequest request) { AgentUser currentAgent = iAgentUserService.getCurrentAgent(request); if (currentAgent == null) { return ServerResponse.createByErrorMsg("请先登录"); } AgentUser agentUser = agentUserMapper.selectByPrimaryKey(currentAgent.getId()); if (agentUser == null) { return ServerResponse.createByErrorMsg("当前用户未找到"); } if (!Boolean.TRUE.equals(agentUser.getGoogleAuthBind())) { return ServerResponse.createByErrorMsg("谷歌验证码未绑定,无需解绑!"); } long t = System.currentTimeMillis(); GoogleAuthenticator ga = new GoogleAuthenticator(); ga.setWindowSize(5); boolean flag = ga.check_code(agentUser.getGoogleAuthSecret(), Long.valueOf(rootGoogleAuthCode), t); if (!flag) { return ServerResponse.createByErrorMsg("谷歌验证码错误"); } agentUser.setGoogleAuthBind(false); agentUser.setGoogleAuthSecret(""); agentUserMapper.updateByPrimaryKeySelective(agentUser); return ServerResponse.createBySuccess(); } }