121 lines
4.4 KiB
C#
121 lines
4.4 KiB
C#
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Oauth2验证回调
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <param name="state"></param>
|
|
/// <param name="scope"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("callback")]
|
|
public async Task<MessageModel<CasDoorToken>> 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<CasDoorToken>()
|
|
{
|
|
success = false,
|
|
msg = "参数错误",
|
|
response = response
|
|
};
|
|
}
|
|
var userinfo = CasdoorHttpHelper.Http_Post<JWTSSOResult<MinProWxOutPut>>("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()
|
|
{
|
|
IsDelete = false,
|
|
UUID = userinfo.result.UnionId,
|
|
Phone = userinfo.result.Mobile,
|
|
GZOpenId = userinfo.result.OpenId,
|
|
CreateTime = DateTime.UtcNow,
|
|
IsVIP = false,
|
|
Gender = 0,
|
|
Subject = 1,
|
|
NickName = userinfo.result.NickName,
|
|
ModifyTime = DateTime.UtcNow,
|
|
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.UtcNow;
|
|
if (!string.IsNullOrEmpty(userinfo.result.Mobile) && string.IsNullOrEmpty(user.Phone))
|
|
{
|
|
user.Phone = userinfo.result.Mobile;
|
|
}
|
|
await _CustomerInfoServices.Update(user);
|
|
}
|
|
var tokenModel = new SSOTokenModelJwt { UserId = user.Id.ToString(), NickName = user.NickName, LoginMode = LoginModeEnum.APP.ToString() };
|
|
jwtStr = JwtHelper.ssoIssueJwt(tokenModel);
|
|
response.token = jwtStr;
|
|
response.Id = user.Id;
|
|
suc = true;
|
|
}
|
|
|
|
return new MessageModel<CasDoorToken>()
|
|
{
|
|
success = suc,
|
|
msg = suc ? "success" : "fail",
|
|
response = response
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|