zhanghua
2025-08-22 89ad0fa748b1773d58822290e4822a4bbbb1d9f9
ruoyi-admin/src/main/java/org/dromara/web/controller/AuthController.java
@@ -3,7 +3,9 @@
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
import com.aliyun.dingtalkcontact_1_0.models.GetUserResponseBody;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
@@ -14,9 +16,7 @@
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;
@@ -33,6 +33,7 @@
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.social.config.properties.SocialLoginConfigProperties;
import org.dromara.common.social.config.properties.SocialProperties;
import org.dromara.common.social.utils.SocialUtils;
@@ -50,12 +51,16 @@
import org.dromara.web.service.IAuthStrategy;
import org.dromara.web.service.SysLoginService;
import org.dromara.web.service.SysRegisterService;
import org.dromara.web.utils.RZTHttpUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.aliyun.teaopenapi.models.Config;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 认证
@@ -79,6 +84,7 @@
    private final ISysClientService clientService;
    private final ISysUserService userService;
    private final SysUserMapper userMapper;
    private final RZTHttpUtils rzthttpUtils;
    /**
@@ -264,7 +270,7 @@
            //获取用户个人token
            String accessToken = getUserTokenResponse.getBody().getAccessToken();
            GetUserResponseBody userResponseBody = getUserinfoNoId(accessToken);
            return JSON.toJSONString(userResponseBody);
            return userResponseBody.mobile;
        }
@@ -315,4 +321,55 @@
        return rsp.getResult();
    }
    final String redisKey = "RZT_TOKEN";
    @RequestMapping(value = "/rztLogin", method = RequestMethod.GET)
    public String getRztAccessToken(@RequestParam(value = "code") String code) {
        try {
            log.info("code值:{}", code);
            String accessToken = getRztToken();
            if (StrUtil.isNotBlank(accessToken)) {
                Map<String, String> hashMap = new HashMap<>();
                hashMap.put("code", code);
                hashMap.put("token", accessToken);
                String userJson = rzthttpUtils.sendGetRequest("/login/info", hashMap);
                log.info("用户信息:{}", userJson);
                JSONObject userObject = JSON.parseObject(userJson);
                if (userObject.getIntValue("errcode") == 0) {
                    return userObject.getString("userid");
                } else {
                    log.error("获取用户信息失败:{}", userObject.getString("errmsg"));
                }
            }
        } catch (Exception e) {
            log.error("获取用户信息异常:{}", e.getMessage());
        }
        return "";
    }
    private String getRztToken() throws Exception {
        String token = RedisUtils.getCacheObject(redisKey);
        if (StrUtil.isEmpty(token)) {
            String corpId = "ww9904fd98c1b0df9e";
            String corpSecret = "mZdTP-ULDWHEPgFCpl62OwudbP3bODgqN9lC-rUtNSA";
            String accessJson = rzthttpUtils.sendGetRequest("/gettoken?corpId=" + corpId + "&corpsecret=" + corpSecret, null);
            JSONObject jsonObject = JSON.parseObject(accessJson);
            if (jsonObject.getIntValue("errcode") == 0) {
                String accessToken = jsonObject.getString("access_token");
                Integer expiresIn = jsonObject.getIntValue("expires_in");
                RedisUtils.setCacheObject(redisKey, accessToken, Duration.ofSeconds(expiresIn));
                return accessToken;
            } else {
                log.error("获取token失败:{},{}", jsonObject.getIntValue("errcode"), jsonObject.getString("errmsg"));
                return null;
            }
        } else {
            return token;
        }
    }
}