1
zj
2026-05-18 a101bb1fa69e7954516944f7b5d93606e656d672
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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();
    }
}