using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using New_College.AuthHelper.OverWrite; using New_College.Common; using New_College.Common.Helper; using New_College.IServices; using New_College.Model; using New_College.Model.ViewModels; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace New_College.Controllers { [AllowAnonymous] [Route("api/oauth")] public class OauthController : Controller { readonly IV_CustomerInfoServices _CustomerInfoServices; public OauthController(IV_CustomerInfoServices v_CustomerInfoServices) { _CustomerInfoServices = v_CustomerInfoServices; } /// ///三方登录 /// /// /// //[Route("login")] //public async Task> Login([FromQuery] ThridLoginRequestDto dto) //{ // string token = string.Empty; // if (dto == null) // { // return new MessageModel() // { // success = false, // msg = "参数错误", // }; // } // if (ThridConfig.secretKey != dto.secretKey) // { // return new MessageModel() // { // success = false, // msg = "密钥错误", // }; // } // //首先判断是否存在账户| // //不存在则创建新的账户| // //存在则直接登录| // return new MessageModel() // { // msg = "ok", // success = true, // response = token // }; //} /// /// Oauth2验证回调 /// /// /// /// /// [HttpGet] [Route("callback")] public async Task> Callback(string code, string state, string scope) { var response = new CasDoorToken(); string jwtStr = string.Empty; bool suc = false; var getaccesstoken = CasdoorHttpHelper.Http_Post("api/login/oauth/access_token", null, new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new CasdoorRequest() { code = code, grant_type = "authorization_code", client_id = CasdoorConfig.ClientId, client_secret = CasdoorConfig.ClientSecret }), Encoding.UTF8, "application/json"));//获取access_token var headers = new System.Collections.Generic.Dictionary { { "Authorization", string.Format("Bearer {0}", getaccesstoken.access_token) } }; if (getaccesstoken.access_token == null) { return new MessageModel() { msg = "code已失效", success = false }; } response.servicetoken = getaccesstoken.access_token; var userinfo = CasdoorHttpHelper.Http_Get("/api/userinfo", headers, new Dictionary()); var user = (await _CustomerInfoServices.Query(q => q.UUID == userinfo.sub)).FirstOrDefault(); if (user == null) { if (!string.IsNullOrWhiteSpace(userinfo.phone)) { var baseuser = (await _CustomerInfoServices.Query(q => q.Phone == userinfo.phone)).FirstOrDefault(); if (baseuser != null) { if (string.IsNullOrEmpty(baseuser.NickName)) { if (!string.IsNullOrWhiteSpace(baseuser.Phone)) { baseuser.NickName = baseuser.Phone; } } baseuser.UUID = userinfo.sub; await _CustomerInfoServices.Update(baseuser); } } else { return new MessageModel() { success = false, msg = "该账户不存在或已注销" }; } } else { if (string.IsNullOrEmpty(user.NickName)) { var single = await _CustomerInfoServices.QueryById(user.Id); if (!string.IsNullOrWhiteSpace(user.Phone)) { single.NickName = user.Phone; } if (!string.IsNullOrWhiteSpace(user.UserCode)) { single.NickName = user.UserCode; } await _CustomerInfoServices.Update(single); } } TokenModelJwt tokenModel = new TokenModelJwt { Uid = user.Id, Role = "users" }; jwtStr = JwtHelper.IssueJwt(tokenModel); response.token = jwtStr; response.Id = user.Id; suc = true; return new MessageModel() { success = suc, msg = suc ? "success" : "fail", response = response }; } } }