Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Core/Demo.Data.Host/Views/Roles/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model IEnumerable<Demo.Models.AppRole>
@model IList<Demo.Models.AppRole>

@{
ViewData["Title"] = "Index";
Expand Down
15 changes: 10 additions & 5 deletions src/MvcDemo/Controllers/WeixinController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ public class WeixinController : Controller
private readonly IWeixinUserApi _api;
private readonly IWeixinCustomerSupportApi _csApi;
private readonly IWeixinSubscriberStore _subscriberStore;
private readonly IWeixinResponseMessageStore<WeixinResponseMessageEntity> _messageStore;
private readonly IWeixinResponseMessageStore<WeixinResponseMessageEntity> _responseStore;
private readonly IWeixinReceivedMessageStore<WeixinReceivedMessageEntity> _messageStore;
private readonly IWeixinReceivedEventStore<WeixinReceivedEventEntity> _eventStore;

public WeixinController(
ILogger<WeixinController> logger,
IWeixinUserApi api,
IWeixinCustomerSupportApi csApi,
IWeixinSubscriberStore subscriberStore,
IWeixinResponseMessageStore<WeixinResponseMessageEntity> messageStore,
IWeixinResponseMessageStore<WeixinResponseMessageEntity> responseStore,
IWeixinReceivedMessageStore<WeixinReceivedMessageEntity> messageStore,
IWeixinReceivedEventStore<WeixinReceivedEventEntity> eventStore)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_api = api;
_csApi = csApi;
_subscriberStore = subscriberStore ?? throw new ArgumentNullException(nameof(subscriberStore));
_responseStore=responseStore ?? throw new ArgumentNullException(nameof(responseStore));
_messageStore = messageStore ?? throw new ArgumentNullException(nameof(messageStore));
_eventStore = eventStore ?? throw new ArgumentNullException(nameof(eventStore));
}
Expand Down Expand Up @@ -61,6 +64,7 @@ public async Task<IActionResult> Subscribers(int? n)
var subscribers = await _subscriberStore.GetItemsAsync(pageSize, pageIndex);
_logger.LogDebug($"微信订阅者在数据库中共{totalRecords}条记录。");
vm.Item = subscribers;
vm.ReturnUrl=Url.Action(nameof(Subscribers), new { n });

return View(vm);
}
Expand All @@ -81,9 +85,10 @@ public async Task<IActionResult> ReceivedText(int? n)

var pageIndex = n.Value - 1;

var items = await _messageStore.GetItemsAsync(pageSize, pageIndex);
vm.Item = await _messageStore.GetItemsAsync(pageSize, pageIndex);
vm.ReturnUrl = Url.Action(nameof(ReceivedText), new { n });
_logger.LogDebug($"微信文本消息在数据库中共{totalRecords}条记录。");
return View(items);
return View(vm);
}

public async Task<IActionResult> SendWeixin(string openId)
Expand All @@ -95,7 +100,7 @@ public async Task<IActionResult> SendWeixin(string openId)

var vm = new SendWeixinViewModel
{
Received = await _messageStore.Items.Where(x => x.ToUserName == openId).ToListAsync(),
Responsed = await _responseStore.Items.Where(x => x.ToUserName == openId).ToListAsync(),
OpenId = openId
};
return View(vm);
Expand Down
1 change: 0 additions & 1 deletion src/MvcDemo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<PackageReference Include="Myvas.AspNetCore.TencentSms" Version="9.0.*" />
<PackageReference Include="Myvas.AspNetCore.ViewDivert" Version="9.0.*" />
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.6" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.*" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/MvcDemo/HostExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Demo.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Myvas.AspNetCore.Authentication;
using Myvas.AspNetCore.Weixin;

Expand All @@ -14,6 +18,21 @@ public static class HostExtensions
{
public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
{
Debug.WriteLine("Create a temporary logger for me.");
// Build a temporary logger for me.
using var loggerFactory = LoggerFactory.Create(logging =>
{
logging.AddConsole();
#if DEBUG
logging.SetMinimumLevel(LogLevel.Trace);
#else
logging.SetMinimumLevel(LogLevel.Information);
#endif
});
var logger = loggerFactory.CreateLogger<WebApplicationBuilder>();
logger.LogDebug($"{MethodBase.GetCurrentMethod().Name}...");
logger.LogInformation("Environment=" + builder.Environment.EnvironmentName);

var Configuration = builder.Configuration;

builder.Services.AddControllersWithViews();
Expand Down Expand Up @@ -47,6 +66,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
o.AccessDeniedPath = "/Identity/Account/AccessDenied";
});

logger?.LogInformation("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]);
logger?.LogInformation("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]);
logger?.LogInformation("QQConnect:AppId=" + Configuration["QQConnect:AppId"]);

builder.Services.AddAuthentication()
.AddWeixinOpen(o =>
{
Expand Down Expand Up @@ -74,6 +97,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
QQConnectScopes.do_like);
});

logger?.LogInformation("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]);

builder.Services.AddTencentSms(o =>
{
o.SdkAppId = Configuration["TencentSms:SdkAppId"];
Expand All @@ -82,6 +107,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder

builder.Services.AddViewDivert();

logger?.LogInformation("Weixin:AppId=" + Configuration["Weixin:AppId"]);

builder.Services.AddWeixin(o =>
{
o.AppId = Configuration["Weixin:AppId"];
Expand Down Expand Up @@ -111,6 +138,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder

public static WebApplication ConfigurePipeline(this WebApplication app)
{
var logger = app.Logger;
logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}...");
logger.LogInformation($"Environment={app.Environment.EnvironmentName}");

var env = app.Environment;
if (env.IsDevelopment())
{
Expand All @@ -124,6 +155,8 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
}
//app.UseHttpsRedirection();
app.UseStaticFiles();

logger.LogTrace("UseWeixinSite...");
app.UseWeixinSite();

app.UseRouting();
Expand Down
2 changes: 1 addition & 1 deletion src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Demo.Models
{
public class SendWeixinViewModel
{
public IEnumerable<WeixinResponseMessageEntity> Received { get; set; }
public IList<WeixinResponseMessageEntity> Responsed { get; set; }

[Required]
public string OpenId { get; set; }
Expand Down
23 changes: 12 additions & 11 deletions src/MvcDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@
using Demo.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Serilog;
using Microsoft.Extensions.Logging;
using System;
using System.Reflection;

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();

var assembly = typeof(Program).Assembly;
var assemblyName = assembly.GetName().Name;
var assemblyVersion = assembly.GetName().Version?.ToString()
?? assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
Log.Information($"{assemblyName} v{assemblyVersion} starting up...");
Console.WriteLine($"{assemblyName} v{assemblyVersion} starting up...");

try
{
var builder = WebApplication.CreateBuilder(args);

// Set default logging level.
#if DEBUG
builder.Logging.SetMinimumLevel(LogLevel.Trace);
#else
builder.Logging.SetMinimumLevel(LogLevel.Information);
#endif

var app = builder.ConfigureServices().Build()
.MigrateDatabase()
.SeedDatabase("demo", "demo@myvas.com")
Expand All @@ -29,12 +32,10 @@
}
catch (Exception ex)
{
// Any unhandled exception during start-up will be caught and flushed to
// our log file or centralized log server
Log.Fatal(ex, "Host terminated for an unhandled exception occured.");
Console.WriteLine($"{assemblyName} v{assemblyVersion} terminated for an unhandled exception occured.");
Console.WriteLine(ex);
}
finally
{
Log.Information($"{assemblyName} v{assemblyVersion} shutdown.");
Log.CloseAndFlush();
Console.WriteLine($"{assemblyName} v{assemblyVersion} shutdown.");
}
Loading