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.
193 lines
7.1 KiB
193 lines
7.1 KiB
package com.qs.serve.controller;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
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.framework.security.service.SysUserDetailsServiceImpl;
|
|
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.entity.SysUser;
|
|
import com.qs.serve.modules.sys.mapper.SysTenantMapper;
|
|
import com.qs.serve.modules.sys.service.SysUserService;
|
|
import com.qs.serve.modules.wx.common.conf.WxCpConfig;
|
|
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 me.chanjar.weixin.common.error.WxErrorException;
|
|
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
|
|
import me.chanjar.weixin.cp.api.WxCpService;
|
|
import me.chanjar.weixin.cp.api.WxCpUserService;
|
|
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
|
|
import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
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 SysUserDetailsServiceImpl userDetailsService;
|
|
|
|
private final SysTenantMapper sysTenantMapper;
|
|
|
|
private SysUserService sysUserService;
|
|
|
|
/**
|
|
* 企业微信登录
|
|
* @param wxLoginUser
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@SysLog(title = "企业微信登录",biz = BizType.LOGIN,inter = InterType.API)
|
|
@PostMapping("/cp")
|
|
public R<?> loginCompanyApp(@RequestBody @Valid WxLoginUser wxLoginUser, HttpServletRequest request){
|
|
WxUser wxUser = null;
|
|
try {
|
|
wxUser = wxUserService.login(wxLoginUser,request);
|
|
} catch (Exception e) {
|
|
Assert.throwEx(e.getMessage());
|
|
}
|
|
if(wxUser==null){
|
|
Assert.throwEx(HttpCode.WX_ERR);
|
|
}
|
|
|
|
|
|
Map<String, Object> objectMap = genTokenInfo(request, wxUser);
|
|
|
|
return R.ok(objectMap);
|
|
}
|
|
|
|
/**
|
|
* 小程序登陆(暂测试)
|
|
* @param wxLoginUser
|
|
* @param request
|
|
* @return
|
|
*/
|
|
@SysLog(title = "小程序登录",biz = BizType.LOGIN,inter = InterType.API)
|
|
@PostMapping("/ma")
|
|
public R<?> loginMicroApp(@RequestBody @Valid WxLoginUser wxLoginUser, HttpServletRequest request){
|
|
WxUser wxUser = null;
|
|
try {
|
|
wxUser = wxUserService.login(wxLoginUser,request);
|
|
} catch (Exception e) {
|
|
log.warn(e.getMessage());
|
|
}
|
|
if(wxUser==null){
|
|
Assert.throwEx(HttpCode.WX_ERR);
|
|
}
|
|
Map<String, Object> objectMap = genTokenInfo(request, wxUser);
|
|
return R.ok(objectMap);
|
|
}
|
|
|
|
/**
|
|
* 公众号登录
|
|
* @param wxLoginUser
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
@SysLog(title = "公众号登录",biz = BizType.LOGIN,inter = InterType.API)
|
|
@PostMapping("/mp")
|
|
public R<?> login(@RequestBody @Valid WxLoginUser wxLoginUser, HttpServletRequest request){
|
|
WxUser wxUser = null;
|
|
try {
|
|
wxUser = wxUserService.login(wxLoginUser,request);
|
|
} catch (Exception e) {
|
|
log.warn(e.getMessage());
|
|
}
|
|
if(wxUser==null){
|
|
Assert.throwEx(HttpCode.WX_ERR);
|
|
}
|
|
Map<String, Object> objectMap = genTokenInfo(request, wxUser);
|
|
return R.ok(objectMap);
|
|
}
|
|
|
|
@NotNull
|
|
private Map<String, Object> genTokenInfo(HttpServletRequest request, WxUser wxUser) {
|
|
Map<String,Object> objectMap = new HashMap<>();
|
|
String token = IdUtil.fastSimpleUUID();
|
|
LoginUser loginUser = new LoginUser(wxUser.getId(), wxUser.getEmpName(),"",
|
|
ServletUtils.getIp(request), LoginUserType.APP_USER,new ArrayList<>(),null,AuthContextUtils.getTenant());
|
|
objectMap.put("token",token);
|
|
//微信登录ID
|
|
String wxUserKey = StringUtils.format(RedisCacheKeys.WX_KEY_USER,token);
|
|
redisService.set(wxUserKey, wxUser.getId());
|
|
//后台管理员信息
|
|
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 objectMap;
|
|
}
|
|
|
|
/**
|
|
* 公众号测试登录
|
|
*/
|
|
@PostMapping("/mptest")
|
|
public R<?> login(HttpServletRequest request){
|
|
Map<String,Object> objectMap = new HashMap<>();
|
|
String token = IdUtil.fastSimpleUUID();
|
|
//微信登录ID
|
|
String wxUserKey = StringUtils.format(RedisCacheKeys.WX_KEY_USER,token);
|
|
redisService.set(wxUserKey,"1");
|
|
|
|
LoginUser loginUser = new LoginUser("1","微信测试用户","",
|
|
ServletUtils.getIp(request), LoginUserType.APP_USER,new ArrayList<>(),null,AuthContextUtils.getTenant());
|
|
objectMap.put("token",token);
|
|
String sysUserId = "1";
|
|
Map<String,Object> tokenMap = new HashMap<>(10);
|
|
String client = "wx_app";
|
|
String redisKey = StringUtils.format(RedisCacheKeys.LOGIN_KEY_APP,client,sysUserId);
|
|
String pctoken = JwtUtils.generateToken(sysUserId,loginUser.getTypeFlag(),client);
|
|
redisService.set(redisKey,pctoken);
|
|
tokenMap.put("token", pctoken);
|
|
tokenMap.put("userId", sysUserId);
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|