Skip to content

Commit

Permalink
添加 Senparc.Weixin.Sample.MP.Simple
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySu committed Jun 25, 2024
1 parent 9b6f63f commit 56c747d
Show file tree
Hide file tree
Showing 31 changed files with 2,099 additions and 15 deletions.
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;
}
}
}
}
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 });
}
}
}
9 changes: 9 additions & 0 deletions Samples/MP/Senparc.Weixin.Sample.MP.Simple/GlobalUsing.cs
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;
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, "由于长时间未搭理客服,您的客服状态已退出!");
}
}
}
Loading

0 comments on commit 56c747d

Please sign in to comment.