You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.7 KiB
124 lines
4.7 KiB
package com.qs.serve.controller;
|
|
|
|
import com.qs.serve.common.framework.redis.RedisService;
|
|
import com.qs.serve.common.framework.security.model.LoginUser;
|
|
import com.qs.serve.common.framework.security.model.LoginUserType;
|
|
import com.qs.serve.common.model.annotation.SysLog;
|
|
import com.qs.serve.common.model.consts.RedisCacheKeys;
|
|
import com.qs.serve.common.model.dto.R;
|
|
import com.qs.serve.common.model.enums.BizType;
|
|
import com.qs.serve.common.model.enums.HttpCode;
|
|
import com.qs.serve.common.model.enums.InterType;
|
|
import com.qs.serve.common.util.*;
|
|
import com.qs.serve.modules.sys.mapper.SysTenantMapper;
|
|
import com.qs.serve.modules.wx.entity.WxUser;
|
|
import com.qs.serve.modules.wx.entity.dto.WxLoginUser;
|
|
import com.qs.serve.modules.wx.service.WxUserService;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.validation.Valid;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/**
|
|
* 门户:微信登录
|
|
* @author YenHex
|
|
* @since 2022/3/7
|
|
*/
|
|
@Slf4j
|
|
@AllArgsConstructor
|
|
@RestController
|
|
@RequestMapping("/api/wx/login")
|
|
public class WxSvcLoginApi {
|
|
|
|
private final WxUserService wxUserService;
|
|
|
|
private final RedisService redisService;
|
|
|
|
private final SysTenantMapper sysTenantMapper;
|
|
|
|
|
|
/**
|
|
* 公众号登录
|
|
* @param wxLoginUser
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
@SysLog(title = "公众号登录",biz = BizType.LOGIN,inter = InterType.API)
|
|
@PostMapping("/mp")
|
|
public R<?> login(@RequestBody @Valid WxLoginUser wxLoginUser, HttpServletRequest request){
|
|
Map<String,Object> objectMap = new HashMap<>();
|
|
WxUser wxUser = null;
|
|
try {
|
|
wxUser = wxUserService.login(wxLoginUser);
|
|
} catch (Exception e) {
|
|
log.warn(e.getMessage());
|
|
}
|
|
if(wxUser==null){
|
|
Assert.throwEx(HttpCode.WX_ERR);
|
|
}
|
|
String token = IdUtil.fastSimpleUUID();
|
|
String key = RedisCacheKeys.LOGIN_KEY_WX + token;
|
|
LoginUser loginUser = new LoginUser(wxUser.getId(),wxUser.getEmpName(),"",
|
|
ServletUtils.getIp(request), LoginUserType.APP_USER,new ArrayList<>(),null,AuthContextUtils.getTenant());
|
|
redisService.set(key, JsonUtil.objectToJson(loginUser.loginUserDTO()),2, TimeUnit.DAYS);
|
|
objectMap.put("token",token);
|
|
|
|
//后台管理员信息
|
|
Map<String,Object> tokenMap = new HashMap<>(10);
|
|
String client = "wx_app";
|
|
String redisKey = StringUtils.format(RedisCacheKeys.LOGIN_KEY_APP,client,wxUser.getSysUserId());
|
|
String pctoken = JwtUtils.generateToken(wxUser.getSysUserId(),loginUser.getTypeFlag(),client);
|
|
redisService.set(redisKey,pctoken);
|
|
tokenMap.put("token", pctoken);
|
|
tokenMap.put("userId", wxUser.getSysUserId());
|
|
tokenMap.put("IP", loginUser.getLoginIp());
|
|
tokenMap.put("tenant", sysTenantMapper.selectById(loginUser.getTenant()));
|
|
tokenMap.put("loginType",client);
|
|
tokenMap.put("client",client);
|
|
//关联
|
|
objectMap.put("adminTokenInfo",tokenMap);
|
|
return R.ok(objectMap);
|
|
}
|
|
|
|
/**
|
|
* 公众号测试登录
|
|
*/
|
|
@PostMapping("/mptest")
|
|
public R<?> login(HttpServletRequest request){
|
|
Map<String,Object> objectMap = new HashMap<>();
|
|
String token = TokenUtil.desEncrypt(IdUtil.timeStampId());
|
|
String key = RedisCacheKeys.LOGIN_KEY_WX + token;
|
|
LoginUser loginUser = new LoginUser("1","微信测试用户","",
|
|
ServletUtils.getIp(request), LoginUserType.APP_USER,new ArrayList<>(),null,AuthContextUtils.getTenant());
|
|
redisService.set(key, JsonUtil.objectToJson(loginUser.loginUserDTO()),2, TimeUnit.DAYS);
|
|
objectMap.put("token",token);
|
|
|
|
Map<String,Object> tokenMap = new HashMap<>(10);
|
|
String client = "wx_app";
|
|
String redisKey = StringUtils.format(RedisCacheKeys.LOGIN_KEY_APP,client,loginUser.getUserId());
|
|
String pctoken = JwtUtils.generateToken(loginUser.getUserId(),loginUser.getTypeFlag(),client);
|
|
redisService.set(redisKey,pctoken);
|
|
tokenMap.put("token", pctoken);
|
|
tokenMap.put("userId", loginUser.getUserId());
|
|
tokenMap.put("IP", loginUser.getLoginIp());
|
|
tokenMap.put("tenant", sysTenantMapper.selectById(loginUser.getTenant()));
|
|
tokenMap.put("loginType",client);
|
|
tokenMap.put("client",client);
|
|
//关联
|
|
objectMap.put("adminTokenInfo",tokenMap);
|
|
|
|
return R.ok(objectMap);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|