|
|
@ -14,18 +14,23 @@ 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.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.validation.Valid; |
|
|
@ -55,6 +60,27 @@ public class WxSvcLoginApi { |
|
|
|
|
|
|
|
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) { |
|
|
|
log.warn(e.getMessage()); |
|
|
|
} |
|
|
|
if(wxUser==null){ |
|
|
|
Assert.throwEx(HttpCode.WX_ERR); |
|
|
|
} |
|
|
|
Map<String, Object> objectMap = genTokenInfo(request, wxUser); |
|
|
|
return R.ok(objectMap); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 小程序登陆(暂测试) |
|
|
@ -65,37 +91,16 @@ public class WxSvcLoginApi { |
|
|
|
@SysLog(title = "小程序登录",biz = BizType.LOGIN,inter = InterType.API) |
|
|
|
@PostMapping("/ma") |
|
|
|
public R<?> loginMicroApp(@RequestBody @Valid WxLoginUser wxLoginUser, HttpServletRequest request){ |
|
|
|
Map<String,Object> objectMap = new HashMap<>(); |
|
|
|
WxUser wxUser = null; |
|
|
|
try { |
|
|
|
// wxUser = wxUserService.login(wxLoginUser);
|
|
|
|
wxUser = wxUserService.login(wxLoginUser,request); |
|
|
|
} catch (Exception e) { |
|
|
|
log.warn(e.getMessage()); |
|
|
|
} |
|
|
|
if(wxUser==null){ |
|
|
|
Assert.throwEx(HttpCode.WX_ERR); |
|
|
|
} |
|
|
|
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); |
|
|
|
Map<String, Object> objectMap = genTokenInfo(request, wxUser); |
|
|
|
return R.ok(objectMap); |
|
|
|
} |
|
|
|
|
|
|
@ -108,27 +113,33 @@ public class WxSvcLoginApi { |
|
|
|
@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); |
|
|
|
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(),"", |
|
|
|
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()); |
|
|
|
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 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); |
|
|
@ -139,13 +150,7 @@ public class WxSvcLoginApi { |
|
|
|
tokenMap.put("client",client); |
|
|
|
//关联
|
|
|
|
objectMap.put("adminTokenInfo",tokenMap); |
|
|
|
//设置
|
|
|
|
// SysUser sysUser = sysUserService.getById(wxUser.getSysUserId());
|
|
|
|
// UserDetails userDetails = userDetailsService.buildLoginUser(sysUser);
|
|
|
|
// UsernamePasswordAuthenticationToken authentication =
|
|
|
|
// new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
|
|
|
// SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
return R.ok(objectMap); |
|
|
|
return objectMap; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|