Commit ce770182 authored by liudong1993's avatar liudong1993

1

parent 3085312c
...@@ -43,5 +43,15 @@ namespace Edu.Cache ...@@ -43,5 +43,15 @@ namespace Edu.Cache
/// </summary> /// </summary>
public static string WeChatPay_Callback_Key = "WeChatPay_Callback_Key_"; public static string WeChatPay_Callback_Key = "WeChatPay_Callback_Key_";
/// <summary>
/// 通讯录token
/// </summary>
public static string QYWeChat_TelToken_Key = "TWeChat_Token_Key_";
/// <summary>
/// 外部人员token
/// </summary>
public static string QYWeChat_EmpToken_Key = "EWeChat_Token_Key_";
} }
} }
using Edu.CacheManager.Base;
using Edu.Common.Enum;
using Edu.Model.CacheModel;
using Edu.Repository.User;
using System;
using System.Linq;
namespace Edu.Cache.WeChat
{
/// <summary>
/// redis缓存
/// </summary>
public class WeChatReidsCache
{
/// <summary>
/// 使用redis第几号库
/// </summary>
public static readonly int REDIS_DB3 = 3;
static readonly RedisHelper redis = new RedisHelper(REDIS_DB3);
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="token"></param>
/// <param name="JwtExpirTime"></param>
public static void TokenSet(string cacheKey, string token, int JwtExpirTime)
{
try
{
TimeSpan ts = GetExpirTime(JwtExpirTime);
redis.StringSet(cacheKey, token, ts);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "TokenSet缓存设置失败");
}
}
/// <summary>
/// 获取缓存时长
/// </summary>
/// <param name="JwtExpirTime"></param>
/// <returns></returns>
private static TimeSpan GetExpirTime(int JwtExpirTime)
{
DateTime dt = DateTime.Now;
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt.AddSeconds(JwtExpirTime) - dt2;
return ts;
}
/// <summary>
/// 判断key是否存在
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool Exists(string cacheKey)
{
return redis.KeyExists(cacheKey);
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="Data"></param>
/// <param name="JwtExpirTime"></param>
public static string GetToken(string cacheKey)
{
try
{
return redis.StringGet(cacheKey);
}
catch (Exception)
{
return "";
}
}
}
}
\ No newline at end of file
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
<ProjectReference Include="..\Edu.Module.Course\Edu.Module.Course.csproj" /> <ProjectReference Include="..\Edu.Module.Course\Edu.Module.Course.csproj" />
<ProjectReference Include="..\Edu.Module.Finance\Edu.Module.Finance.csproj" /> <ProjectReference Include="..\Edu.Module.Finance\Edu.Module.Finance.csproj" />
<ProjectReference Include="..\Edu.Module.OKR\Edu.Module.OKR.csproj" /> <ProjectReference Include="..\Edu.Module.OKR\Edu.Module.OKR.csproj" />
<ProjectReference Include="..\Edu.ThirdCore\Edu.ThirdCore.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
using Edu.Common.Enum;
using Edu.Common.Enum.Course;
using System;
using VT.FW.DB;
namespace Edu.Model.Entity.WeChat
{
/// <summary>
/// 企业微信配置实体类
/// </summary>
[Serializable]
[DB(ConnectionName = "DefaultConnection")]
public class RB_WeChat_Config
{
/// <summary>
/// Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 企业名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 企业微信ID
/// </summary>
public string WX_CorpId { get; set; }
/// <summary>
/// 企业代码(企业统一社会信用代码)
/// </summary>
public string Social_Code { get; set; }
/// <summary>
/// 企业通讯录secret
/// </summary>
public string Employee_Secret { get; set; }
/// <summary>
/// 企业外部联系人secret
/// </summary>
public string Contact_Secret { get; set; }
/// <summary>
/// 事件回调地址(通讯录)
/// </summary>
public string TelEvent_CallBack { get; set; }
/// <summary>
/// 回调token
/// </summary>
public string Token { get; set; }
/// <summary>
/// 回调消息加密串
/// </summary>
public string Encoding_Aes_Key { get; set; }
/// <summary>
/// 事件回调地址(外部人员)
/// </summary>
public string EmpEvent_CallBack { get; set; }
/// <summary>
/// 集团编号
/// </summary>
public int Group_Id { get; set; }
/// <summary>
/// 创建人
/// </summary>
public int CreateBy { get; set; }
/// <summary>
/// 创建日期
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
/// 修改人
/// </summary>
public int UpdateBy { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdateTime { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Edu.Common.Enum.Sale;
using Edu.Model.Entity.WeChat;
namespace Edu.Model.ViewModel.WeChat
{
/// <summary>
/// 企业微信配置扩展类
/// </summary>
public class RB_WeChat_Config_ViewModel : RB_WeChat_Config
{
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Edu.Cache\Edu.Cache.csproj" />
<ProjectReference Include="..\Edu.Common\Edu.Common.csproj" />
<ProjectReference Include="..\Edu.Model\Edu.Model.csproj" />
<ProjectReference Include="..\Edu.Repository\Edu.Repository.csproj" />
<ProjectReference Include="..\Edu.ThirdCore\Edu.ThirdCore.csproj" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Cache.WeChat;
using Edu.Common;
using Edu.Common.API;
using Edu.Common.Enum.Finance;
using Edu.Common.Plugin;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.WeChat;
using Edu.Repository.WeChat;
using Edu.ThirdCore.QYWinXin;
using VT.FW.DB;
namespace Edu.Module.QYWeChat
{
/// <summary>
/// 企业微信处理类
/// </summary>
public class QYWeChatModule
{
/// <summary>
/// 微信配置
/// </summary>
private readonly RB_WeChat_ConfigRepository weChat_ConfigRepository = new RB_WeChat_ConfigRepository();
#region 微信配置
/// <summary>
/// 获取企业微信配置
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public RB_WeChat_Config_ViewModel GetWeChatConfigModel(RB_WeChat_Config_ViewModel model)
{
var dmodel = weChat_ConfigRepository.GetList(model).FirstOrDefault();
if (dmodel == null) { dmodel = new RB_WeChat_Config_ViewModel(); }
return dmodel;
}
/// <summary>
/// 新增/修改企业微信配置
/// </summary>
/// <param name="demdoel"></param>
/// <returns></returns>
public string SetWeChatConfig(RB_WeChat_Config_ViewModel demdoel)
{
#region 验证一下
//获取两种的token
#region 通讯录
var TelToken = QYWeiXinHelper.GetToken(demdoel.WX_CorpId, demdoel.Employee_Secret);
//更新token缓存
WeChatReidsCache.TokenSet(Cache.CacheKey.QYWeChat_TelToken_Key + demdoel.Group_Id, TelToken.access_token, TelToken.expires_in - 10);
#endregion
#region 外部人员
#endregion
#endregion
if (demdoel.Id > 0)
{
Dictionary<string, object> keyValues = new Dictionary<string, object>() {
{ nameof(RB_WeChat_Config_ViewModel.Name),demdoel.Name},
{ nameof(RB_WeChat_Config_ViewModel.WX_CorpId),demdoel.WX_CorpId},
{ nameof(RB_WeChat_Config_ViewModel.Employee_Secret),demdoel.Employee_Secret},
{ nameof(RB_WeChat_Config_ViewModel.Contact_Secret),demdoel.Contact_Secret},
{ nameof(RB_WeChat_Config_ViewModel.TelEvent_CallBack),demdoel.TelEvent_CallBack},
{ nameof(RB_WeChat_Config_ViewModel.EmpEvent_CallBack),demdoel.EmpEvent_CallBack},
{ nameof(RB_WeChat_Config_ViewModel.Token),demdoel.Token},
{ nameof(RB_WeChat_Config_ViewModel.Encoding_Aes_Key),demdoel.Encoding_Aes_Key},
{ nameof(RB_WeChat_Config_ViewModel.UpdateBy),demdoel.UpdateBy},
{ nameof(RB_WeChat_Config_ViewModel.UpdateTime),demdoel.UpdateTime},
};
List<WhereHelper> wheres = new List<WhereHelper>() {
new WhereHelper(){
FiledName=nameof(RB_WeChat_Config_ViewModel.Id),
FiledValue=demdoel.Id,
OperatorEnum=OperatorEnum.Equal
}
};
bool flag = weChat_ConfigRepository.Update(keyValues, wheres);
return flag ? "" : "出错了,请联系管理员";
}
else {
bool flag = weChat_ConfigRepository.Insert(demdoel) > 0;
return flag ? "" : "出错了,请联系管理员";
}
}
#endregion
}
}
using Edu.Common.Enum;
using Edu.Model.Entity.WeChat;
using Edu.Model.ViewModel.WeChat;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;
namespace Edu.Repository.WeChat
{
/// <summary>
/// 企业配置仓储层
/// </summary>
public class RB_WeChat_ConfigRepository : BaseRepository<RB_WeChat_Config>
{
/// <summary>
/// 获取列表
/// </summary>
/// <param name="demodel"></param>
/// <returns></returns>
public List<RB_WeChat_Config_ViewModel> GetList(RB_WeChat_Config_ViewModel demodel)
{
string where = $@" 1=1 ";
if (demodel.Group_Id > 0)
{
where += $@" and r.{nameof(RB_WeChat_Config_ViewModel.Group_Id)} ={demodel.Group_Id}";
}
string sql = $@"
SELECT * From RB_WeChat_Config r
WHERE {where}
ORDER BY r.Id DESC ";
return Get<RB_WeChat_Config_ViewModel>(sql).ToList();
}
}
}
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework> <TargetFramework>netcoreapp3.0</TargetFramework>
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
<PackageReference Include="Aliyun.Net.SDK.Core" Version="1.0.3" /> <PackageReference Include="Aliyun.Net.SDK.Core" Version="1.0.3" />
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.10.0" /> <PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Senparc.Weixin.Work" Version="3.9.600" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.Work;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.Containers;
namespace Edu.ThirdCore.QYWinXin
{
/// <summary>
/// 企业微信处理
/// </summary>
public class QYWeiXinHelper
{
/// <summary>
/// 获取token
/// </summary>
/// <returns></returns>
public static Senparc.Weixin.Work.Entities.AccessTokenResult GetToken(string corpId, string corpSecret)
{
return Senparc.Weixin.Work.CommonAPIs.CommonApi.GetToken(corpId, corpSecret);
}
/// <summary>
/// 获取部门列表
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static object GetDepartmentList(string token)
{
return Senparc.Weixin.Work.AdvancedAPIs.MailListApi.GetDepartmentList(token);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Finance;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.WeChat;
using Edu.Module.QYWeChat;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace Edu.WebApi.Controllers.Finance
{
/// <summary>
/// 企业微信接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiExceptionFilter]
[ApiController]
[EnableCors("AllowCors")]
public class QYWeChatController : BaseController
{
/// <summary>
/// 企业微信处理类对象
/// </summary>
private readonly QYWeChatModule weChatModule = new QYWeChatModule();
#region 微信配置
/// <summary>
/// 获取活动配置
/// </summary>
/// <returns></returns>
[HttpPost]
public ApiResult GetWeChatConfigInfo() {
var userInfo = base.UserInfo;
var model = weChatModule.GetWeChatConfigModel(new RB_WeChat_Config_ViewModel() { Group_Id = userInfo.Group_Id });
return ApiResult.Success("", new
{
model.Id,
model.Name,
model.WX_CorpId,
model.Social_Code,
model.Employee_Secret,
model.Contact_Secret,
model.TelEvent_CallBack,
model.Token,
model.Encoding_Aes_Key,
model.EmpEvent_CallBack,
model.UpdateBy,
UpdateByName = UserReidsCache.GetUserLoginInfo(model.UpdateBy)?.AccountName ?? "",
UpdateTime = model.UpdateTime.ToString("yyyy-MM-dd HH:mm")
});
}
/// <summary>
/// 新增/修改 活动配置
/// </summary>
/// <returns></returns>
[HttpPost]
public ApiResult SetWeChatConfig() {
var userInfo = base.UserInfo;
RB_WeChat_Config_ViewModel demdoel = JsonHelper.DeserializeObject<RB_WeChat_Config_ViewModel>(RequestParm.Msg.ToString());
demdoel.Group_Id = userInfo.Group_Id;
demdoel.CreateBy = userInfo.Id;
demdoel.CreateTime = DateTime.Now;
demdoel.UpdateBy = userInfo.Id;
demdoel.UpdateTime = DateTime.Now;
string msg = weChatModule.SetWeChatConfig(demdoel);
if (msg == "")
{
return ApiResult.Success();
}
else {
return ApiResult.Failed(msg);
}
}
#endregion
}
}
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
<ProjectReference Include="..\Edu.Module.OKR\Edu.Module.OKR.csproj" /> <ProjectReference Include="..\Edu.Module.OKR\Edu.Module.OKR.csproj" />
<ProjectReference Include="..\Edu.Module.Public\Edu.Module.Public.csproj" /> <ProjectReference Include="..\Edu.Module.Public\Edu.Module.Public.csproj" />
<ProjectReference Include="..\Edu.Module.Question\Edu.Module.Question.csproj" /> <ProjectReference Include="..\Edu.Module.Question\Edu.Module.Question.csproj" />
<ProjectReference Include="..\Edu.Module.QYWeChat\Edu.Module.QYWeChat.csproj" />
<ProjectReference Include="..\Edu.Module.StudyAbroad\Edu.Module.StudyAbroad.csproj" /> <ProjectReference Include="..\Edu.Module.StudyAbroad\Edu.Module.StudyAbroad.csproj" />
<ProjectReference Include="..\Edu.Module.System\Edu.Module.System.csproj" /> <ProjectReference Include="..\Edu.Module.System\Edu.Module.System.csproj" />
<ProjectReference Include="..\Edu.Module.User\Edu.Module.User.csproj" /> <ProjectReference Include="..\Edu.Module.User\Edu.Module.User.csproj" />
......
...@@ -10,6 +10,11 @@ using Microsoft.AspNetCore.Http; ...@@ -10,6 +10,11 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Senparc.CO2NET;
using Senparc.Weixin.Entities;
using Senparc.Weixin.RegisterServices;
using Senparc.Weixin.Work;
namespace Edu.WebApi namespace Edu.WebApi
{ {
...@@ -62,6 +67,8 @@ namespace Edu.WebApi ...@@ -62,6 +67,8 @@ namespace Edu.WebApi
"https://mall.oytour.com" "https://mall.oytour.com"
}; };
services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(corsArray.ToArray()))); services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(corsArray.ToArray())));
services.AddSenparcWeixinServices(Configuration);//注册全局微信服务
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment