235 lines
8.2 KiB
Java
235 lines
8.2 KiB
Java
package com.tiesheng.login.controller;
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.URLUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.tiesheng.annotation.operation.OperationIgnore;
|
|
import com.tiesheng.annotation.token.TokenIgnore;
|
|
import com.tiesheng.login.pojos.CodeExtraDTO;
|
|
import com.tiesheng.login.pojos.DoLoginInfo;
|
|
import com.tiesheng.login.pojos.UniqueIndexDTO;
|
|
import com.tiesheng.login.pojos.WxminiLoginVo;
|
|
import com.tiesheng.login.service.CorePlatformUniqueService;
|
|
import com.tiesheng.platform.config.ding.PlatformDingConfig;
|
|
import com.tiesheng.platform.config.ding.bean.DingJsapiSignature;
|
|
import com.tiesheng.platform.config.ding.bean.DingUserInfo;
|
|
import com.tiesheng.platform.config.ding.bean.DingUserSimple;
|
|
import com.tiesheng.platform.config.wxmini.PlatformWxminiConfig;
|
|
import com.tiesheng.platform.config.wxmp.PlatformWxmpConfig;
|
|
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
|
|
import com.tiesheng.platform.config.wxmp.bean.WxJsapiSignature;
|
|
import com.tiesheng.platform.config.wxmp.bean.WxUserInfo;
|
|
import com.tiesheng.util.config.GlobalConfig;
|
|
import com.tiesheng.util.config.TsTokenConfig;
|
|
import com.tiesheng.util.exception.ApiException;
|
|
import com.tiesheng.util.pojos.ApiResp;
|
|
import com.tiesheng.util.pojos.TokenBean;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author hao
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/login")
|
|
@TokenIgnore
|
|
public class LoginController {
|
|
|
|
|
|
@Autowired
|
|
PlatformWxmpConfig platformWxmpConfig;
|
|
@Autowired
|
|
PlatformDingConfig platformDingConfig;
|
|
@Autowired
|
|
PlatformWxminiConfig platformWxminiConfig;
|
|
@Autowired
|
|
CorePlatformUniqueService corePlatformUniqueService;
|
|
@Autowired
|
|
GlobalConfig globalConfig;
|
|
@Autowired
|
|
TsTokenConfig tsTokenConfig;
|
|
|
|
|
|
/**
|
|
* 唯一值登录方式,如学号,手机号等
|
|
*
|
|
* @return
|
|
*/
|
|
@GetMapping("/unique/redirect")
|
|
@OperationIgnore
|
|
public void uniqueIndex(UniqueIndexDTO dto, HttpServletResponse response) {
|
|
if (tsTokenConfig.isValidLoginSign() && !dto.validSign()) {
|
|
corePlatformUniqueService.redirect(null, dto.getTo(), dto.getExtra(), response);
|
|
return;
|
|
}
|
|
|
|
TokenBean tokenBean = corePlatformUniqueService.login(new DoLoginInfo("web_unique_redirect",
|
|
dto.getNo(), dto.getPlatform(), dto.getInfo()));
|
|
corePlatformUniqueService.redirect(tokenBean, dto.getTo(), dto.getExtra(), response);
|
|
}
|
|
|
|
|
|
/**
|
|
* 授权登录
|
|
*
|
|
* @param dto
|
|
* @return
|
|
*/
|
|
@PostMapping("/unique/index")
|
|
@OperationIgnore
|
|
public ApiResp<String> uniqueIndex(@RequestBody UniqueIndexDTO dto) {
|
|
TokenBean tokenBean = corePlatformUniqueService.login(new DoLoginInfo("web_unique_index",
|
|
dto.getNo(), dto.getPlatform(), dto.getInfo()));
|
|
if (!TsTokenConfig.validToken(tokenBean)) {
|
|
throw new ApiException("登录失败");
|
|
}
|
|
return ApiResp.respOK(tokenBean.toToken());
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// 钉钉相关授权
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* 钉钉授权登录
|
|
*
|
|
* @param service
|
|
* @param extra
|
|
* @param response
|
|
*/
|
|
@GetMapping("/ding/index/{service}")
|
|
@OperationIgnore
|
|
public void dingIndex(@PathVariable String service, String extra, HttpServletResponse response) {
|
|
if (StrUtil.isEmpty(extra)) {
|
|
extra = "";
|
|
}
|
|
Map<String, String> map = MapUtil.newHashMap();
|
|
map.put("corpId", platformDingConfig.getConfigBean(service).getCorpId());
|
|
map.put("service", service);
|
|
map.put("extra", extra);
|
|
map.put("context", globalConfig.getContextPath());
|
|
String query = URLUtil.buildQuery(map, Charset.defaultCharset());
|
|
String configUrl = globalConfig.buildPath("/ding/index.html?" + query);
|
|
try {
|
|
response.sendRedirect(configUrl);
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 钉钉授权回调
|
|
*
|
|
* @param service
|
|
*/
|
|
@RequestMapping("/ding/oauth2/{service}")
|
|
@OperationIgnore
|
|
public void dingOauth2(@PathVariable String service, CodeExtraDTO dto, HttpServletResponse response) {
|
|
DingUserSimple userSimple = platformDingConfig.getUserIdByCode(service, dto.getCode());
|
|
DingUserInfo dingUserInfo = platformDingConfig.topapiV2UserGet(service, userSimple.getUserid());
|
|
TokenBean tokenBean = corePlatformUniqueService.login(new DoLoginInfo(dingUserInfo.getAppId(),
|
|
dingUserInfo.getUserid(), "ding", JSON.toJSONString(dingUserInfo)));
|
|
corePlatformUniqueService.redirect(tokenBean, dto.getTo(), dto.getExtra(), response);
|
|
}
|
|
|
|
|
|
/**
|
|
* 钉钉授权jssdk
|
|
*
|
|
* @param url
|
|
* @return
|
|
*/
|
|
@GetMapping("/ding/jssdk/{service}")
|
|
public ApiResp<DingJsapiSignature> dingJssdk(@PathVariable String service, String url) {
|
|
DingJsapiSignature jsapiSignature = platformDingConfig.createJsapiSignature(service, url);
|
|
return ApiResp.respOK(jsapiSignature);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// 微信相关
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
/**
|
|
* 微信授权登录
|
|
*
|
|
* @return
|
|
*/
|
|
@RequestMapping("/wxmp/index/{service}")
|
|
@OperationIgnore
|
|
public void wxmpIndex(@PathVariable String service, String extra, HttpServletResponse response) throws IOException {
|
|
if (StrUtil.isEmpty(extra)) {
|
|
extra = "";
|
|
}
|
|
String configUrl = globalConfig.buildPath("/login/wxmp/oauth2/" + service + "?extra=" + extra);
|
|
// String authorizationUrl = platformWxmpConfig.buildAuthorizationUrl(service, configUrl, "snsapi_base");
|
|
// response.sendRedirect(authorizationUrl);
|
|
response.sendRedirect(configUrl+"&to=mobile&code=200");
|
|
}
|
|
|
|
|
|
/**
|
|
* 微信授权回调
|
|
*/
|
|
@RequestMapping("/wxmp/oauth2/{service}")
|
|
@OperationIgnore
|
|
public void wxmpOauth2(@PathVariable String service, CodeExtraDTO dto, HttpServletResponse response) {
|
|
// WxUserInfo wxUserInfo = platformWxmpConfig.getOAuth2AccessToken(service, dto.getCode());
|
|
// TokenBean tokenBean = corePlatformUniqueService.login(new DoLoginInfo(wxUserInfo.getAppId(),
|
|
// wxUserInfo.getOpenid(), "wxmp", JSON.toJSONString(wxUserInfo)));
|
|
TokenBean tokenBean = new TokenBean();
|
|
tokenBean.setId("testid");
|
|
tokenBean.setRoleId("testroleid");
|
|
tokenBean.setExtra("testextra");
|
|
corePlatformUniqueService.redirect(tokenBean, dto.getTo(), dto.getExtra(), response);
|
|
}
|
|
|
|
|
|
/**
|
|
* 微信授权jssdk
|
|
*
|
|
* @param url
|
|
* @return
|
|
*/
|
|
@GetMapping("/wxmp/jssdk/{service}")
|
|
public ApiResp<WxJsapiSignature> wxmpJssdk(@PathVariable String service, String url) {
|
|
WxJsapiSignature jsapiSignature = platformWxmpConfig.createJsapiSignature(service, url);
|
|
return ApiResp.respOK(jsapiSignature);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// 小程序相关
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* 微信小程序登录
|
|
*
|
|
* @return
|
|
*/
|
|
@RequestMapping("/wxmini/index/{service}")
|
|
@OperationIgnore
|
|
public ApiResp<WxminiLoginVo> wxminiIndex(@PathVariable String service, String code) {
|
|
String openid = platformWxminiConfig.jscode2session(service, code);
|
|
WxConfigBean configBean = platformWxminiConfig.getConfigBean(service);
|
|
TokenBean tokenBean = corePlatformUniqueService.login(new DoLoginInfo(configBean.getAppId(),
|
|
openid, "wxmini", "{}"));
|
|
|
|
WxminiLoginVo loginVo = new WxminiLoginVo();
|
|
loginVo.setOpenid(openid);
|
|
if (TsTokenConfig.validToken(tokenBean)) {
|
|
loginVo.setToken(tokenBean.toToken());
|
|
}
|
|
return ApiResp.respOK(loginVo);
|
|
}
|
|
|
|
|
|
}
|