This commit is contained in:
j4587698
2020-02-09 19:10:05 +08:00
commit 358617b0c3
414 changed files with 96012 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
using System.Collections.Generic;
using System.Linq;
using DeviceDetectorNET.Parser.Device;
using JXCMS.Core.Extensions;
using Microsoft.AspNetCore.Mvc.Razor;
namespace JXCMS.Core.Themes
{
public class TemplateViewLocationExpander : IViewLocationExpander
{
/// <summary>
/// PC主题不切换主题与自适应主题同样使用此主题
/// </summary>
public static string PcThemeName = "Default";
/// <summary>
/// 手机版主题
/// </summary>
public static string MobileThemeName = "Mobile";
/// <summary>
/// 主题切换方式,默认为不切换
/// </summary>
public static ThemeChangeMode Mode { get; set; } = ThemeChangeMode.None;
/// <summary>
/// 手机版域名
/// </summary>
public static string MobileDomain;
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (!context.AreaName.IsNullOrEmpty())
{
return viewLocations;
}
var themeName = context.Values["template"] ?? PcThemeName;
string[] locations = { "/Views/" + themeName + "/{1}/{0}.cshtml", "/Views/" + themeName + "/{0}.cshtml", "/Views/" + themeName + "/Shared/{0}.cshtml", "/Views/Shared/{0}.cshtml" };
return locations.Union(viewLocations.Where(x => !x.StartsWith("/Views/")));
}
public void PopulateValues(ViewLocationExpanderContext context)
{
switch (Mode)
{
case ThemeChangeMode.None:
case ThemeChangeMode.Adaptive:
context.Values["template"] = PcThemeName;
break;
case ThemeChangeMode.Auto:
MobileParser mobileParser = new MobileParser();
mobileParser.SetUserAgent(context.ActionContext.HttpContext.Request.Headers["User-Agent"]);
var result = mobileParser.Parse();
if ( result.Success)
{
context.Values["template"] = MobileThemeName;
}
else
{
context.Values["template"] = PcThemeName;
}
break;
case ThemeChangeMode.Domain:
if (context.ActionContext.HttpContext.Request.Host.Host == MobileDomain)
{
context.Values["template"] = MobileThemeName;
}
else
{
context.Values["template"] = PcThemeName;
}
break;
}
}
}
}

View File

@@ -0,0 +1,25 @@
namespace JXCMS.Core.Themes
{
/// <summary>
/// 切换主题方式
/// </summary>
public enum ThemeChangeMode
{
/// <summary>
/// 不切换
/// </summary>
None,
/// <summary>
/// 自适应
/// </summary>
Adaptive,
/// <summary>
/// 自动切换主题
/// </summary>
Auto,
/// <summary>
/// 根据域名切换主题
/// </summary>
Domain
}
}

View File

@@ -0,0 +1,15 @@
namespace JXCMS.Core.Themes
{
public class ThemeConfig
{
public string ThemeName { get; set; }
public string FolderName { get; set; }
public string ScreenShot { get; set; }
public int ThemeType { get; set; }
public bool IsUsing { get; set; }
}
}

View File

@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
namespace JXCMS.Core.Themes
{
public static class ThemeUtil
{
/// <summary>
/// PC主题
/// </summary>
public const int PcTheme = 1;
/// <summary>
/// 手机主题
/// </summary>
public const int MobileTheme = 2;
/// <summary>
/// 自适应主题
/// </summary>
public const int AdaptiveTheme = 3;
/// <summary>
/// PC主题字段名称
/// </summary>
public const string PcThemeName = "PcTheme";
/// <summary>
/// 移动主题字段名称
/// </summary>
public const string MobileThemeName = "MobileTheme";
public static List<ThemeConfig> ListThemes(string basePath)
{
var themes = Directory.GetDirectories(basePath);
List<ThemeConfig> themeConfigs = new List<ThemeConfig>();
foreach (var theme in themes)
{
var configPath = Path.Combine(theme, "theme.json");
if (File.Exists(configPath))
{
var themeConfig = JsonConvert.DeserializeObject<ThemeConfig>(File.ReadAllText(configPath));
themeConfig.FolderName = Path.GetFileName(theme);
if (themeConfig.ThemeType == MobileTheme)
{
if (TemplateViewLocationExpander.Mode == ThemeChangeMode.Domain || TemplateViewLocationExpander.Mode == ThemeChangeMode.Auto)
{
themeConfigs.Add(themeConfig);
}
}
else
{
themeConfigs.Add(themeConfig);
}
if (themeConfig.FolderName == TemplateViewLocationExpander.PcThemeName)
{
themeConfig.IsUsing = true;
}
else if (themeConfig.FolderName == TemplateViewLocationExpander.MobileThemeName)
{
themeConfig.IsUsing = true;
}
}
}
return themeConfigs;
}
}
}