|
@@ -6,10 +6,12 @@ using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
|
+using System.Linq.Expressions;
|
|
using System.Security.Claims;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using TEAMModelOS.SDK.Context.Configuration;
|
|
using TEAMModelOS.SDK.Context.Configuration;
|
|
using TEAMModelOS.SDK.Extension.JwtAuth.Models;
|
|
using TEAMModelOS.SDK.Extension.JwtAuth.Models;
|
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
using TEAMModelOS.SDK.Helper.Common.JsonHelper;
|
|
using TEAMModelOS.SDK.Helper.Common.JsonHelper;
|
|
using TEAMModelOS.SDK.Helper.Network.HttpHelper;
|
|
using TEAMModelOS.SDK.Helper.Network.HttpHelper;
|
|
using JwtSetting = HiTeachCE.Extension.JwtSetting;
|
|
using JwtSetting = HiTeachCE.Extension.JwtSetting;
|
|
@@ -18,6 +20,11 @@ namespace HiTeachCE.Services
|
|
{
|
|
{
|
|
public class WeChatService : IBusinessService
|
|
public class WeChatService : IBusinessService
|
|
{
|
|
{
|
|
|
|
+ public WeChatUserService weChatUserService;
|
|
|
|
+ public WeChatService(WeChatUserService _weChatUserService) {
|
|
|
|
+ weChatUserService = _weChatUserService;
|
|
|
|
+ }
|
|
|
|
+
|
|
public async Task<object> VerifyWeChat(Dictionary<string, object> dict)
|
|
public async Task<object> VerifyWeChat(Dictionary<string, object> dict)
|
|
{
|
|
{
|
|
if (dict.TryGetValue("code", out object code))
|
|
if (dict.TryGetValue("code", out object code))
|
|
@@ -143,7 +150,7 @@ namespace HiTeachCE.Services
|
|
/// <param name="unionid"></param>
|
|
/// <param name="unionid"></param>
|
|
/// <param name="access_token"></param>
|
|
/// <param name="access_token"></param>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- public async Task<WeChatUser> GetWeChatUserInfo(string session_key, Dictionary<string, object> dict)
|
|
|
|
|
|
+ public WeChatUser GetWeChatUserInfo(string session_key, Dictionary<string, object> dict)
|
|
{
|
|
{
|
|
string enData = "";
|
|
string enData = "";
|
|
if (dict.TryGetValue("encryptedData", out object encryptedData) && dict.TryGetValue("iv", out object iv))
|
|
if (dict.TryGetValue("encryptedData", out object encryptedData) && dict.TryGetValue("iv", out object iv))
|
|
@@ -175,20 +182,43 @@ namespace HiTeachCE.Services
|
|
MiniAPPUserInfo miniAPPUser = enData.FromApiJson<MiniAPPUserInfo>();
|
|
MiniAPPUserInfo miniAPPUser = enData.FromApiJson<MiniAPPUserInfo>();
|
|
if (miniAPPUser != null)
|
|
if (miniAPPUser != null)
|
|
{
|
|
{
|
|
- WeChatUser weChatUser = new WeChatUser();
|
|
|
|
- weChatUser.id = miniAPPUser.unionId;
|
|
|
|
- // weChatUser.PartitionKey = miniAPPUser.country;
|
|
|
|
- weChatUser.nickName = miniAPPUser.nickName;
|
|
|
|
- weChatUser.gender = miniAPPUser.gender;
|
|
|
|
- weChatUser.avatarUrl = miniAPPUser.avatarUrl;
|
|
|
|
- weChatUser.country = miniAPPUser.country;
|
|
|
|
- weChatUser.city = miniAPPUser.city;
|
|
|
|
- weChatUser.province = miniAPPUser.province;
|
|
|
|
- weChatUser.openId = miniAPPUser.openId;
|
|
|
|
- weChatUser.unionId = miniAPPUser.unionId;
|
|
|
|
- weChatUser.appid = miniAPPUser.watermark.appid;
|
|
|
|
- // await SaveOrUpdate<WeChatUser>(weChatUser);
|
|
|
|
- return weChatUser;
|
|
|
|
|
|
+
|
|
|
|
+ Expression<Func<WeChatUser, bool>> linq = null;
|
|
|
|
+ linq = l => l.unionId == miniAPPUser.unionId;
|
|
|
|
+ var weChatUsers = weChatUserService.GetList(linq);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // await SaveOrUpdate<WeChatUser>(weChatUser);
|
|
|
|
+
|
|
|
|
+ if (weChatUsers.IsNotEmpty())
|
|
|
|
+ {
|
|
|
|
+ var weChatUser = weChatUsers[0];
|
|
|
|
+ weChatUser.nickName = miniAPPUser.nickName;
|
|
|
|
+ weChatUser.gender = miniAPPUser.gender;
|
|
|
|
+ weChatUser.avatarUrl = miniAPPUser.avatarUrl;
|
|
|
|
+ weChatUser.country = miniAPPUser.country;
|
|
|
|
+ weChatUser.city = miniAPPUser.city;
|
|
|
|
+ weChatUser.province = miniAPPUser.province;
|
|
|
|
+ weChatUser.openId = miniAPPUser.openId;
|
|
|
|
+ weChatUser.unionId = miniAPPUser.unionId;
|
|
|
|
+ weChatUserService.Update(weChatUser);
|
|
|
|
+ return weChatUser;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ var weChatUser = new WeChatUser();
|
|
|
|
+ weChatUser.id = Guid.NewGuid().ToString();
|
|
|
|
+ weChatUser.nickName = miniAPPUser.nickName;
|
|
|
|
+ weChatUser.gender = miniAPPUser.gender;
|
|
|
|
+ weChatUser.avatarUrl = miniAPPUser.avatarUrl;
|
|
|
|
+ weChatUser.country = miniAPPUser.country;
|
|
|
|
+ weChatUser.city = miniAPPUser.city;
|
|
|
|
+ weChatUser.province = miniAPPUser.province;
|
|
|
|
+ weChatUser.openId = miniAPPUser.openId;
|
|
|
|
+ weChatUser.unionId = miniAPPUser.unionId;
|
|
|
|
+ weChatUser.registerTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
|
|
|
|
+ weChatUserService.Insert(weChatUser);
|
|
|
|
+ return weChatUser;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -198,7 +228,7 @@ namespace HiTeachCE.Services
|
|
public async Task<JwtResponse> MiniAPPLogin(Dictionary<string, object> dict)
|
|
public async Task<JwtResponse> MiniAPPLogin(Dictionary<string, object> dict)
|
|
{
|
|
{
|
|
MiniAPPLoginInfo loginInfo = await VerifyMiniAPP(dict);
|
|
MiniAPPLoginInfo loginInfo = await VerifyMiniAPP(dict);
|
|
- WeChatUser weChatUser = await GetWeChatUserInfo(loginInfo.session_key, dict);
|
|
|
|
|
|
+ WeChatUser weChatUser = GetWeChatUserInfo(loginInfo.session_key, dict);
|
|
JwtResponse jwt = CreateJwtToken(weChatUser, loginInfo.session_key);
|
|
JwtResponse jwt = CreateJwtToken(weChatUser, loginInfo.session_key);
|
|
LoginInfo login = new LoginInfo();
|
|
LoginInfo login = new LoginInfo();
|
|
login.id = Guid.NewGuid().ToString();
|
|
login.id = Guid.NewGuid().ToString();
|
|
@@ -219,8 +249,8 @@ namespace HiTeachCE.Services
|
|
model.Claims.Add(new Claim(JwtClaimTypes.SessionId, sessionKey));
|
|
model.Claims.Add(new Claim(JwtClaimTypes.SessionId, sessionKey));
|
|
model.Claims.Add(new Claim(JwtClaimTypes.Name, user.nickName));
|
|
model.Claims.Add(new Claim(JwtClaimTypes.Name, user.nickName));
|
|
model.Claims.Add(new Claim(JwtClaimTypes.Id, user.unionId));
|
|
model.Claims.Add(new Claim(JwtClaimTypes.Id, user.unionId));
|
|
- model.Claims.AddRange("Student".Split(',').Select(s => new Claim(JwtClaimTypes.Role, s)));
|
|
|
|
- model.Roles.Add("Student");
|
|
|
|
|
|
+ model.Claims.AddRange("learner".Split(',').Select(s => new Claim(JwtClaimTypes.Role, s)));
|
|
|
|
+ model.Roles.Add("learner");
|
|
JwtResponse jwtResponse = JwtHelper.IssueJWT(model);
|
|
JwtResponse jwtResponse = JwtHelper.IssueJWT(model);
|
|
return jwtResponse;
|
|
return jwtResponse;
|
|
}
|
|
}
|
|
@@ -229,27 +259,5 @@ namespace HiTeachCE.Services
|
|
throw new Exception("Validation failure,user is null !");
|
|
throw new Exception("Validation failure,user is null !");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- public JwtResponse CreateJwtToken(LoginInfo login, string tiket)
|
|
|
|
- {
|
|
|
|
- ClaimModel model = new ClaimModel
|
|
|
|
- {
|
|
|
|
- Scope = "WebAPP"
|
|
|
|
- };
|
|
|
|
- if (login != null)
|
|
|
|
- {
|
|
|
|
- model.Claims.Add(new Claim(JwtClaimTypes.SessionId, tiket));
|
|
|
|
- model.Claims.Add(new Claim(JwtClaimTypes.Name, login.Name));
|
|
|
|
- model.Claims.Add(new Claim(JwtClaimTypes.Id, login.TeamModelId));
|
|
|
|
- model.Claims.AddRange("Teacher".Split(',').Select(s => new Claim(JwtClaimTypes.Role, s)));
|
|
|
|
- model.Roles.Add("Teacher");
|
|
|
|
- JwtResponse jwtResponse = JwtHelper.IssueJWT(model);
|
|
|
|
- return jwtResponse;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- throw new Exception("Validation failure,user is null !");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|