|
@@ -15,11 +15,15 @@ using TEAMModelOS.SDK.Models;
|
|
|
using HTEXLib.COMM.Helpers;
|
|
|
using TEAMModelOS.Models;
|
|
|
using static TEAMModelOS.SDK.Models.Teacher;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
+using static TEAMModelOS.Controllers.Third.ScController;
|
|
|
|
|
|
namespace TEAMModeBI.Controllers
|
|
|
{
|
|
|
- //[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
- //[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
+ [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
[Route("common/login")]
|
|
|
[ApiController]
|
|
|
public class LoginController : ControllerBase
|
|
@@ -32,13 +36,18 @@ namespace TEAMModeBI.Controllers
|
|
|
//钉钉提示信息
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
|
- public LoginController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, Option option)
|
|
|
+ //隐世登录
|
|
|
+ private readonly CoreAPIHttpService _accountHttpService;
|
|
|
+ string type = "ddteammodel";
|
|
|
+
|
|
|
+ public LoginController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService coreAPIHttpService)
|
|
|
{
|
|
|
_configuration = configuration;
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_azureStorage = azureStorage;
|
|
|
_dingDing = dingDing;
|
|
|
- _option = option;
|
|
|
+ _option = option?.Value;
|
|
|
+ _accountHttpService = coreAPIHttpService;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -118,7 +127,7 @@ namespace TEAMModeBI.Controllers
|
|
|
/// <returns>Json结果</returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
[HttpGet("DingLogin")]
|
|
|
- public async Task<IActionResult> DingLogin(JsonElement jsonElement)
|
|
|
+ public async Task<IActionResult> DingLogin(string LoginTempCode)
|
|
|
{
|
|
|
string temp_mess = null;
|
|
|
//state 是前端传入的,钉钉并不会修改,比如有多种登录方式的时候,一个登录方法判断登录方式可以进行不同的处理。
|
|
@@ -126,14 +135,13 @@ namespace TEAMModeBI.Controllers
|
|
|
{
|
|
|
string str_appKey = _configuration["DingDingAuth:appKey"];
|
|
|
string str_appSecret = _configuration["DingDingAuth:appSecret"];
|
|
|
- string str_agentld = "1290158212";
|
|
|
if (string.IsNullOrWhiteSpace(str_appKey) || string.IsNullOrWhiteSpace(str_appSecret))
|
|
|
{
|
|
|
- throw new Exception("请先配置钉钉扫码登录信息!");
|
|
|
+ return BadRequest("请先配置钉钉扫码登录信息!");
|
|
|
}
|
|
|
//自己传的code
|
|
|
- if (jsonElement.TryGetProperty("tempCode", out JsonElement LoginTempCode)) return BadRequest();
|
|
|
- string accreCode = LoginTempCode.ToString();
|
|
|
+ //if (!jsonElement.TryGetProperty("tempCode", out JsonElement LoginTempCode)) return BadRequest();
|
|
|
+ string loginTempCode = LoginTempCode.ToString();
|
|
|
//判断参数是否为空
|
|
|
if (string.IsNullOrEmpty(LoginTempCode.ToString()))
|
|
|
{
|
|
@@ -149,50 +157,23 @@ namespace TEAMModeBI.Controllers
|
|
|
OapiGettokenResponse tokenResponse = Iclient.Execute(request);
|
|
|
if (tokenResponse.IsError)
|
|
|
{
|
|
|
- return BadRequest();
|
|
|
+ return Ok(new { ddbinds = $"status=-1"});
|
|
|
}
|
|
|
|
|
|
- //temp_mess = tokenResponse.Body;
|
|
|
- //获取引用后台免登录凭证
|
|
|
- DefaultDingTalkClient NoVoucher = new DefaultDingTalkClient("https://oapi.dingtalk.com/sso/gettoken");
|
|
|
- OapiSsoGettokenRequest ssoRequest = new OapiSsoGettokenRequest();
|
|
|
- ssoRequest.Corpid = str_agentld;
|
|
|
- ssoRequest.Corpsecret = str_appSecret;
|
|
|
- ssoRequest.SetHttpMethod("GET");
|
|
|
- OapiSsoGettokenResponse ssoResponse = new OapiSsoGettokenResponse();
|
|
|
- ssoResponse = NoVoucher.Execute(ssoRequest);
|
|
|
-
|
|
|
- //temp_mess += "=====" + ssoResponse.Body;
|
|
|
- ////return Ok(tokenResponse.Body);
|
|
|
- ////自己传的code
|
|
|
- //if (!jsonElement.TryGetProperty("accreCode", out JsonElement jsaccreCode)) return BadRequest();
|
|
|
- //string accreCode = jsaccreCode.ToString();
|
|
|
-
|
|
|
- //自己获取code
|
|
|
- //string accreCode = tokenResponse.AccessToken;
|
|
|
- //temp_mess += "====="+accreCode;
|
|
|
-
|
|
|
+ string access_token = tokenResponse.AccessToken;
|
|
|
+ //获取临时授权码 获取授权用户的个人信息
|
|
|
DefaultDingTalkClient clientinfo = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
|
|
|
- OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest() { TmpAuthCode = accreCode };
|
|
|
- //req.TmpAuthCode = code;
|
|
|
+ OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest() { TmpAuthCode = loginTempCode }; //通过扫描二维码,跳转到指定的Url后,向Url中追加Code临时授权码
|
|
|
OapiSnsGetuserinfoBycodeResponse response = clientinfo.Execute(req, str_appKey, str_appSecret);
|
|
|
- //temp_mess += "====="+ response.Body;
|
|
|
- //return Ok(temp_mess);
|
|
|
- ////return Ok(response.Body); //用户信息代检验;
|
|
|
- ////获取到response后就可以进行自己的登录业务处理了
|
|
|
- ////xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
|
-
|
|
|
if (response.IsError)
|
|
|
{
|
|
|
return BadRequest();
|
|
|
}
|
|
|
|
|
|
- //temp_mess += response.UserInfo;
|
|
|
- //根据unionid获取userid
|
|
|
string unionid = response.UserInfo.Unionid;
|
|
|
IDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid"); //userid地址
|
|
|
OapiUserGetbyunionidRequest byunionidRequest = new OapiUserGetbyunionidRequest() { Unionid = unionid };
|
|
|
- OapiUserGetbyunionidResponse byunionidResponse = client2.Execute(byunionidRequest, accreCode);
|
|
|
+ OapiUserGetbyunionidResponse byunionidResponse = client2.Execute(byunionidRequest, access_token);
|
|
|
if (byunionidResponse.IsError)
|
|
|
{
|
|
|
return BadRequest();
|
|
@@ -206,150 +187,69 @@ namespace TEAMModeBI.Controllers
|
|
|
Language = "zh_CN"
|
|
|
};
|
|
|
v2GetRequest.SetHttpMethod("POST");
|
|
|
- OapiV2UserGetResponse v2GetResponse = client3.Execute(v2GetRequest, accreCode);
|
|
|
+ OapiV2UserGetResponse v2GetResponse = client3.Execute(v2GetRequest, access_token);
|
|
|
if (v2GetResponse.IsError)
|
|
|
{
|
|
|
return BadRequest();
|
|
|
}
|
|
|
|
|
|
var DDbind = v2GetResponse.Result;
|
|
|
-
|
|
|
- DingDingBind dingDingBind = new()
|
|
|
+ //return Ok(new { v2GetResponse.Result ,v2GetResponse.Body});
|
|
|
+ DingDingBind dingDingBind = new DingDingBind
|
|
|
{
|
|
|
- type = "ddteammodel",
|
|
|
- loginid = DDbind.LoginId,
|
|
|
+ type = type,
|
|
|
+ unionid = DDbind.Unionid,
|
|
|
userid = DDbind.Userid,
|
|
|
- userName = DDbind.Name,
|
|
|
- Mobile = DDbind.Mobile,
|
|
|
- email = DDbind.Email,
|
|
|
- sourceid = new HashSet<string> { DDbind.LoginId }
|
|
|
+ name = DDbind.Name,
|
|
|
+ mobile = DDbind.Mobile,
|
|
|
+ title = DDbind.Title,
|
|
|
+ DeptIdList = DDbind.DeptIdList,
|
|
|
+ jobNumber = DDbind.JobNumber,
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
Teacher teacher = null;
|
|
|
- string sql = $"select distinct value(c) c join A1 in c.ddbinds where A1.userid={dingDingBind.userid} AND A1.loginid = {dingDingBind.loginid}";
|
|
|
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
|
|
|
+ string sql = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid='{dingDingBind.userid}' AND A1.unionid ='{dingDingBind.unionid}'";
|
|
|
+ await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
|
|
|
{
|
|
|
teacher = item;
|
|
|
break;
|
|
|
}
|
|
|
if (teacher == null)
|
|
|
{
|
|
|
- return Ok(new { status = 0, msg = "没有绑定!" , dingDingBind });
|
|
|
+ return Ok(new { ddbinds = $"status=0&dingDingBind={dingDingBind.ToJsonString()}" });
|
|
|
}
|
|
|
- else
|
|
|
+ else
|
|
|
{
|
|
|
- var ddbind = teacher.ddbinds.FindAll(x => x.userid.Equals($"{dingDingBind.userid}") && x.loginid.Equals($"{dingDingBind.loginid}"));
|
|
|
- if (ddbind != null)
|
|
|
- {
|
|
|
- return Ok(new { teacher, dingDingBind });
|
|
|
- }
|
|
|
- else
|
|
|
+ var url = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
|
|
|
+ var clientID = _configuration.GetValue<string>("HaBookAuth:clientID");
|
|
|
+ var clientSecret = _configuration.GetValue<string>("HaBookAuth:clientSecret");
|
|
|
+ var logintion = _option.Location;
|
|
|
+ //隐式登录
|
|
|
+ (int code, string content) = await _accountHttpService.Implicit(clientID, clientSecret, logintion, $"{url}/oauth2/implicit",
|
|
|
+ new Dictionary<string, string>()
|
|
|
+ {
|
|
|
+ { "grant_type", "implicit" },
|
|
|
+ { "client_id",clientID},
|
|
|
+ { "account",teacher.id},
|
|
|
+ { "nonce",Guid.NewGuid().ToString()}
|
|
|
+ });
|
|
|
+
|
|
|
+ TmdidImplicit implicit_token = new TmdidImplicit();
|
|
|
+ if (!string.IsNullOrEmpty(content) && code == 200)
|
|
|
{
|
|
|
- teacher.ddbinds.Add(dingDingBind);
|
|
|
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
|
|
|
- return Ok(new { teacher, dingDingBind });
|
|
|
+ implicit_token = content.ToObject<TmdidImplicit>();
|
|
|
+ var ddbind = teacher.ddbinds.Find(x => x.userid.Equals($"{dingDingBind.userid}") && x.unionid.Equals($"{dingDingBind.unionid}"));
|
|
|
+ if (ddbind != null)
|
|
|
+ {
|
|
|
+ return Ok(new { ddbinds = $"status=200$id_token={implicit_token.id_token}&access_token={implicit_token.access_token}&expires_in={implicit_token.expires_in}&token_type={implicit_token.token_type}" });
|
|
|
+ }
|
|
|
}
|
|
|
+ return Ok(new { ddbinds= $"status=1¶m={dingDingBind.ToJsonString()}&type={type}&bindurl=sc/bind" });
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
- return BadRequest(temp_mess + "======" + e.Message);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 钉钉扫码登录返回String
|
|
|
- /// </summary>
|
|
|
- /// <param name="accreCode"></param>
|
|
|
- /// <param name="state"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- [HttpGet("DLogin")]
|
|
|
- public string DLogin(string accreCode, string state)
|
|
|
- {
|
|
|
- //state 是前端传入的,钉钉并不会修改,比如有多种登录方式的时候,一个登录方法判断登录方式可以进行不同的处理。
|
|
|
- OapiSnsGetuserinfoBycodeResponse response = new OapiSnsGetuserinfoBycodeResponse();
|
|
|
- try
|
|
|
- {
|
|
|
- string qrAppId = _configuration["DingDingAuth:appKey"];
|
|
|
- string qrAppSecret = _configuration["DingDingAuth:appSecret"];
|
|
|
- if (string.IsNullOrWhiteSpace(qrAppId) || string.IsNullOrWhiteSpace(qrAppSecret))
|
|
|
- {
|
|
|
- throw new Exception("请先配置钉钉扫码登录信息!");
|
|
|
- }
|
|
|
-
|
|
|
- DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
|
|
|
- OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
|
|
|
- req.TmpAuthCode = accreCode;
|
|
|
- response = client.Execute(req, qrAppId, qrAppSecret);
|
|
|
-
|
|
|
- //获取到response后就可以进行自己的登录业务处理了
|
|
|
- //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
|
-
|
|
|
- if (response.IsError)
|
|
|
- {
|
|
|
- return "unionid读取失败";
|
|
|
- }
|
|
|
-
|
|
|
- //根据unionid获取userid
|
|
|
- string unionid = response.UserInfo.Unionid;
|
|
|
- IDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid"); //userid地址
|
|
|
- OapiUserGetbyunionidRequest byunionidRequest = new OapiUserGetbyunionidRequest() { Unionid = unionid };
|
|
|
- OapiUserGetbyunionidResponse byunionidResponse = client2.Execute(byunionidRequest, accreCode);
|
|
|
- if (byunionidResponse.IsError)
|
|
|
- {
|
|
|
- return "userid读取失败";
|
|
|
- }
|
|
|
- // 根据userId获取用户信息
|
|
|
- string userid = byunionidResponse.Result.Userid;
|
|
|
- IDingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
- OapiV2UserGetRequest v2GetRequest = new OapiV2UserGetRequest()
|
|
|
- {
|
|
|
- Userid = userid,
|
|
|
- Language = "zh_CN"
|
|
|
- };
|
|
|
- v2GetRequest.SetHttpMethod("GET");
|
|
|
- OapiV2UserGetResponse v2GetResponse = client3.Execute(v2GetRequest, accreCode);
|
|
|
- if (v2GetResponse.IsError)
|
|
|
- {
|
|
|
- return "用户信息读取错误";
|
|
|
- }
|
|
|
-
|
|
|
- return response.Body;
|
|
|
-
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- return response.Errmsg = e.Message;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public async Task<IActionResult> TeamModeBILogin(JsonElement jsonElement)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
- if (!jsonElement.TryGetProperty("pw", out JsonElement pw)) return BadRequest();
|
|
|
-
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
-
|
|
|
- var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(id.GetString(), new PartitionKey($"Base"));
|
|
|
-
|
|
|
-
|
|
|
- return Ok(new { });
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- await _dingDing.SendBotMsg($"IES5,{_option.Location},LoginController/TeamModeBILogin\n Error Message{ex.Message} Error sting:{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
- throw;
|
|
|
+ return BadRequest($"{temp_mess }{e.Message}{e.StackTrace} ");
|
|
|
}
|
|
|
|
|
|
}
|