-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
2,099 additions
and
15 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Samples/MP/Senparc.Weixin.Sample.MP.Simple/Controllers/BaseController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/*---------------------------------------------------------------- | ||
Copyright (C) 2024 Senparc | ||
文件名:BaseController.cs | ||
文件功能描述:Controller基类 | ||
创建标识:Senparc - 20150312 | ||
----------------------------------------------------------------*/ | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Senparc.Weixin.Entities; | ||
|
||
namespace Senparc.Weixin.Sample.MP.Controllers | ||
{ | ||
public class BaseController : Controller | ||
{ | ||
protected string AppId | ||
{ | ||
get | ||
{ | ||
return Config.SenparcWeixinSetting.WeixinAppId;//与微信公众账号后台的AppId设置保持一致,区分大小写。 | ||
} | ||
} | ||
|
||
protected static ISenparcWeixinSettingForMP MpSetting | ||
{ | ||
get | ||
{ | ||
return Config.SenparcWeixinSetting.MpSetting; | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Samples/MP/Senparc.Weixin.Sample.MP.Simple/Controllers/HomeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Senparc.Weixin.Sample.Models; | ||
using System.Diagnostics; | ||
|
||
namespace Senparc.Weixin.Sample.MP.Controllers | ||
{ | ||
public class HomeController : BaseController | ||
{ | ||
private readonly ILogger<HomeController> _logger; | ||
|
||
public HomeController(ILogger<HomeController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
public IActionResult Error() | ||
{ | ||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
global using Senparc.NeuChar.MessageHandlers; | ||
global using Senparc.Weixin.AspNet; | ||
global using Senparc.Weixin.MP; | ||
global using Senparc.Weixin.MP.MessageHandlers.Middleware; | ||
global using Senparc.Weixin.RegisterServices; | ||
global using Senparc.Weixin.Sample.MP; | ||
global using Microsoft.Extensions.Options; | ||
global using Senparc.Weixin.Entities; | ||
global using Microsoft.Extensions.FileProviders; |
53 changes: 53 additions & 0 deletions
53
Samples/MP/Senparc.Weixin.Sample.MP.Simple/MessageHandlers/CustomMessageContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*---------------------------------------------------------------- | ||
Copyright (C) 2024 Senparc | ||
文件名:CustomMessageContext.cs | ||
文件功能描述:微信消息上下文 | ||
创建标识:Senparc - 20150312 | ||
----------------------------------------------------------------*/ | ||
|
||
//DPBMARK_FILE MP | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Senparc.NeuChar.Context; | ||
using Senparc.NeuChar.Entities; | ||
using Senparc.Weixin.MP.MessageContexts; | ||
|
||
namespace Senparc.Weixin.Sample.MP | ||
{ | ||
/* v16.8.0 后,提供分布式缓存,只需要直接使用 DefaultMpMessageContext,即使没有 CustomMessageContext 也没有关系 */ | ||
public class CustomMessageContext : DefaultMpMessageContext //MessageContext<IRequestMessageBase, IResponseMessageBase> | ||
{ | ||
public CustomMessageContext() | ||
{ | ||
base.MessageContextRemoved += CustomMessageContext_MessageContextRemoved; | ||
} | ||
|
||
/// <summary> | ||
/// 当上下文过期,被移除时触发的时间 | ||
/// </summary> | ||
/// <param name="sender"></param> | ||
/// <param name="e"></param> | ||
void CustomMessageContext_MessageContextRemoved(object sender, Senparc.NeuChar.Context.WeixinContextRemovedEventArgs<IRequestMessageBase, IResponseMessageBase> e) | ||
{ | ||
/* 注意,这个事件不是实时触发的(当然你也可以专门写一个线程监控) | ||
* 为了提高效率,根据WeixinContext中的算法,这里的过期消息会在过期后下一条请求执行之前被清除 | ||
*/ | ||
|
||
var messageContext = e.MessageContext as CustomMessageContext; | ||
if (messageContext == null) | ||
{ | ||
return;//如果是正常的调用,messageContext不会为null | ||
} | ||
|
||
//TODO:这里根据需要执行消息过期时候的逻辑,下面的代码仅供参考 | ||
|
||
//Log.InfoFormat("{0}的消息上下文已过期",e.OpenId); | ||
//api.SendMessage(e.OpenId, "由于长时间未搭理客服,您的客服状态已退出!"); | ||
} | ||
} | ||
} |
Oops, something went wrong.