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; } /// /// 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; if (string.IsNullOrWhiteSpace(code)) { return new MessageModel() { success = false, msg = "参数错误", response = response }; } var userinfo = CasdoorHttpHelper.Http_Post>("api/syswechat/user_info", null, new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject( new CasdoorRequest() { code = code, client_id = CasdoorConfig.ClientId, client_secret = CasdoorConfig.ClientSecret }), Encoding.UTF8, "application/json"));// if (userinfo != null && userinfo.code == 200 && userinfo.type == "success") { var user = (await _CustomerInfoServices.Query(q => q.OpenId == userinfo.result.OpenId)).FirstOrDefault();//判断移动端是否已完成unionid替换 if (user == null) { //没有用户则注册一个新用户 user = new Model.Models.V_CustomerInfo() { Id = userinfo.result.Id, IsDelete = false, UUID = userinfo.result.UnionId, Phone = userinfo.result.Mobile, GZOpenId = userinfo.result.OpenId, OpenId = userinfo.result.OpenId, CreateTime = DateTime.Now, IsVIP = true,//默认不收费 Gender = 0, Subject = 1, NickName = userinfo.result.NickName, ModifyTime = DateTime.Now, TenantId = userinfo.result.TenantId, AvatarUrl = userinfo.result.Avatar, CustomerType = CustomerTypeEnum.General, Datainit = false, }; user.Id = await _CustomerInfoServices.Add(user); } else { user.UUID = userinfo.result?.UnionId; // user.GZOpenId = userinfo.result?.OpenId; user.ModifyTime = DateTime.Now; if (!string.IsNullOrEmpty(userinfo.result.Mobile) && string.IsNullOrEmpty(user.Phone)) { user.Phone = userinfo.result.Mobile; } await _CustomerInfoServices.Update(user); } if (string.IsNullOrEmpty(user.NickName)) { user.NickName = "学霸" + RadomHelper.RandNum(4); } //var tokenModel = new SSOTokenModelJwt { UserId = user.Id.ToString(), NickName = user.NickName, LoginMode = LoginModeEnum.APP.ToString() }; //jwtStr = JwtHelper.ssoIssueJwt(tokenModel); response.token = userinfo.result.accessToken; response.Id = user.Id; suc = true; } return new MessageModel() { success = suc, msg = suc ? "success" : "fail", response = response }; } } }