diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java b/mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java index 429baf53c..a93617438 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java @@ -1,5 +1,9 @@ package com.iformall.controller; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.iformall.common.ErrorCode; import com.iformall.common.ResultData; import com.iformall.domain.po.MallUserInfo; import com.iformall.domain.vo.MallUserInfoVo; @@ -7,6 +11,8 @@ import com.iformall.service.MallUserInfoService; import com.iformall.service.MallUserRoleService; import com.iformall.shiro.UserSession; import com.iformall.shiro.UseriFormallToken; +import com.iformall.utils.RedisUtils; +import com.iformall.utils.TOTP; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; @@ -32,6 +38,8 @@ import java.util.List; @RequestMapping("/wechat") @Slf4j public class WechatLoginController extends BaseController { + private String WECHAT_PREV = "OPEN-"; + @Autowired private WxMpService wxMpService; @@ -64,7 +72,7 @@ public class WechatLoginController extends BaseController { response.addCookie(openIdCookie); // check user openid 是否是已授权用户 - List userList = mallUserInfoService.getByWebOpenId(accessToken.getOpenId()); + List userList = mallUserInfoService.getUsersByWebOpenId(accessToken.getOpenId()); if(userList.size() > 0) { if(userList.size() == 1) { // 唯一用户 @@ -92,8 +100,11 @@ public class WechatLoginController extends BaseController { } } else { // 跳转登录选择页面 + String key = TOTP.generateWechatOpen(accessToken.getOpenId()); + RedisUtils redisUtils = new RedisUtils(); + redisUtils.set(WECHAT_PREV + key, JSON.toJSONString(userList)); try { - response.sendRedirect("https://" + host + "/#/loginselect?openId=" + accessToken.getOpenId()); + response.sendRedirect("https://" + host + "/#/loginselect?key=" + key); } catch (Exception e) { log.error(e.getMessage()); } @@ -115,45 +126,66 @@ public class WechatLoginController extends BaseController { @ApiOperation(value = "微信登录用户信息列表") @GetMapping("getMallUserList") @ApiImplicitParams({ - @ApiImplicitParam(name = "openId", value = "openId", dataType = "String", paramType = "query", required = true)}) - public ResultData getMallUserList(String openId) { + @ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "query", required = true)}) + public ResultData getMallUserList(String key) { log.debug("[" + getIpAddr() + "] MallUserInfoController::getMallUserList"); - List users = mallUserInfoService.getUsersByWebOpenId(openId); - // TODO 添加 加密串 - - return new ResultData(users); + String openKey = WECHAT_PREV + key; + RedisUtils redisUtils = new RedisUtils(); + if(redisUtils.hasKey(openKey)) { + String userList = redisUtils.get(openKey); + return new ResultData(userList); + } + return new ResultData(ErrorCode.WECHAT_LOGIN_KEY_OVERTIME); } @ApiOperation(value = "微信用户登录") @GetMapping("weChatUserLogin") @ApiImplicitParams({ - @ApiImplicitParam(name = "openId", value = "openId", dataType = "String", paramType = "query", required = true), - @ApiImplicitParam(name = "uname", value = "uname", dataType = "String", paramType = "query", required = true)}) - public void weChatUserLogin(String uname, String openId, HttpServletRequest request, HttpServletResponse response) { + @ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "query", required = true), + @ApiImplicitParam(name = "username", value = "username", dataType = "String", paramType = "query", required = true)}) + public void weChatUserLogin(String username, String key, HttpServletRequest request, HttpServletResponse response) { log.debug("[" + getIpAddr() + "] MallUserInfoController::weChatUserLogin"); String host = request.getHeader("host"); - MallUserInfo userInfo = mallUserInfoService.getByUsernameWebOpenId(uname, openId); - try { - Subject subject = SecurityUtils.getSubject(); - UseriFormallToken token = new UseriFormallToken(userInfo.getUsername()); - subject.login(token); - MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo); - info.setPassword("保密"); - info.setBopenId("保密"); - info.setWebOpenId("保密"); - info.setWithWechat(true); - String menus = mallUserRoleService.getPermissionsByUser(info); - if(menus != null) { - info.setMenus(menus); + + String openKey = WECHAT_PREV + key; + RedisUtils redisUtils = new RedisUtils(); + if(redisUtils.hasKey(openKey)) { + String userList = redisUtils.get(openKey); + // 删除缓存 + redisUtils.delete(openKey); + JSONArray userArr = JSON.parseArray(userList); + for(int i=0;i