| | |
| | | import cn.dev33.satoken.annotation.SaIgnore; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders; |
| | | import com.aliyun.dingtalkcontact_1_0.models.GetUserResponseBody; |
| | | import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest; |
| | | import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse; |
| | | import com.aliyun.teautil.models.RuntimeOptions; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.dingtalk.api.DefaultDingTalkClient; |
| | | import com.dingtalk.api.DingTalkClient; |
| | | import com.dingtalk.api.request.*; |
| | | import com.dingtalk.api.response.OapiUserGetbyunionidResponse; |
| | | import com.dingtalk.api.response.OapiUserGetuserinfoResponse; |
| | | import com.dingtalk.api.response.OapiV2UserGetResponse; |
| | | import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse; |
| | | import com.taobao.api.ApiException; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.dromara.common.social.utils.SocialUtils; |
| | | import org.dromara.common.tenant.helper.TenantHelper; |
| | | import org.dromara.system.domain.SysClient; |
| | | import org.dromara.system.domain.SysUser; |
| | | import org.dromara.system.domain.bo.SysTenantBo; |
| | | import org.dromara.system.domain.vo.SysTenantVo; |
| | | import org.dromara.system.service.ISysClientService; |
| | | import org.dromara.system.service.ISysConfigService; |
| | | import org.dromara.system.service.ISysSocialService; |
| | | import org.dromara.system.service.ISysTenantService; |
| | | import org.dromara.system.mapper.SysUserMapper; |
| | | import org.dromara.system.service.*; |
| | | import org.dromara.system.uitil.AutoLoginUtil; |
| | | import org.dromara.web.domain.vo.LoginTenantVo; |
| | | import org.dromara.web.domain.vo.LoginVo; |
| | | import org.dromara.web.domain.vo.TenantListVo; |
| | |
| | | import org.dromara.web.service.SysRegisterService; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import com.aliyun.teaopenapi.models.Config; |
| | | |
| | | import java.net.URL; |
| | | import java.util.List; |
| | |
| | | private final ISysTenantService tenantService; |
| | | private final ISysSocialService socialUserService; |
| | | private final ISysClientService clientService; |
| | | private final ISysUserService userService; |
| | | private final SysUserMapper userMapper; |
| | | |
| | | |
| | | /** |
| | |
| | | return R.ok(vo); |
| | | } |
| | | |
| | | /** |
| | | * 自动注册并登录 |
| | | */ |
| | | // @SaCheckPermission("system:user:auto") |
| | | @GetMapping(value = "auto") |
| | | public R<LoginVo> auto(RegisterBody user) { |
| | | AutoLoginUtil.padding(user); |
| | | String clientId = "e5cd7e4891bf95d1d19206ce24a7b32e"; |
| | | //pc |
| | | SysClient client = clientService.queryByClientId(clientId); |
| | | //检验是否存在账号 |
| | | boolean exists = userMapper.exists(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getUserName, user.getUsername()) |
| | | .eq(SysUser::getStatus, "0") |
| | | .eq(SysUser::getDelFlag, "0")); |
| | | if (!exists) { |
| | | registerService.register(user); |
| | | } |
| | | |
| | | LoginBody loginBody = AutoLoginUtil.create(user, clientId); |
| | | // 登录 |
| | | return R.ok(IAuthStrategy.login(loginBody, client)); |
| | | } |
| | | |
| | | |
| | | public static com.aliyun.dingtalkoauth2_1_0.Client authClient() throws Exception { |
| | | Config config = new Config(); |
| | | config.protocol = "https"; |
| | | config.regionId = "central"; |
| | | return new com.aliyun.dingtalkoauth2_1_0.Client(config); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户token |
| | | * |
| | | * @param authCode |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | //接口地址:注意/auth与钉钉登录与分享的回调域名地址一致 |
| | | @RequestMapping(value = "/dingdingLogin", method = RequestMethod.GET) |
| | | public String getAccessToken(@RequestParam(value = "authCode") String authCode, @RequestParam(value = "code") String code) throws Exception { |
| | | |
| | | com.aliyun.dingtalkoauth2_1_0.Client client = authClient(); |
| | | GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest() |
| | | |
| | | //应用基础信息-应用信息的AppKey,请务必替换为开发的应用AppKey |
| | | .setClientId("dingl5dxahaj3uzfug66") |
| | | |
| | | //应用基础信息-应用信息的AppSecret,,请务必替换为开发的应用AppSecret |
| | | .setClientSecret("AGDu7NfzkverlMXq8CUDiy6EXx5jSL4v2p-Odz1mpXV5e4_K3kB1Acat0RftzBXC") |
| | | .setCode(authCode) |
| | | .setGrantType("authorization_code"); |
| | | GetUserTokenResponse getUserTokenResponse = client.getUserToken(getUserTokenRequest); |
| | | System.out.println(getUserTokenResponse.getBody()); |
| | | if ("dingf4816bf92d85d84435c2f4657eb6378f".equals(getUserTokenResponse.getBody().getCorpId())) { |
| | | |
| | | //获取用户个人token |
| | | String accessToken = getUserTokenResponse.getBody().getAccessToken(); |
| | | GetUserResponseBody userResponseBody = getUserinfoNoId(accessToken); |
| | | return userResponseBody.mobile; |
| | | } |
| | | |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | public static com.aliyun.dingtalkcontact_1_0.Client contactClient() throws Exception { |
| | | Config config = new Config(); |
| | | config.protocol = "https"; |
| | | config.regionId = "central"; |
| | | return new com.aliyun.dingtalkcontact_1_0.Client(config); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户个人信息 |
| | | * |
| | | * @param accessToken |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public GetUserResponseBody getUserinfoNoId(String accessToken) throws Exception { |
| | | com.aliyun.dingtalkcontact_1_0.Client client = contactClient(); |
| | | GetUserHeaders getUserHeaders = new GetUserHeaders(); |
| | | getUserHeaders.xAcsDingtalkAccessToken = accessToken; |
| | | //获取用户个人信息,如需获取当前授权人的信息,unionId参数必须传me |
| | | GetUserResponseBody userResponse = client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody(); |
| | | |
| | | return userResponse; |
| | | } |
| | | |
| | | public String getUserId(String accessToken, String unionId) throws ApiException { |
| | | DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid"); |
| | | OapiUserGetbyunionidRequest req = new OapiUserGetbyunionidRequest(); |
| | | req.setUnionid(unionId); |
| | | OapiUserGetbyunionidResponse rsp = client.execute(req, accessToken); |
| | | if (rsp.isSuccess()) { |
| | | return rsp.getResult().getUserid(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public OapiV2UserGetResponse.UserGetResponse getUserinfo(String accessToken, String userId) throws Exception { |
| | | DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get"); |
| | | OapiV2UserGetRequest req = new OapiV2UserGetRequest(); |
| | | req.setUserid(userId); |
| | | req.setLanguage("en_US"); |
| | | OapiV2UserGetResponse rsp = client.execute(req, accessToken); |
| | | return rsp.getResult(); |
| | | } |
| | | |
| | | } |