重构代码:重新设计网站皮肤逻辑,移除客户端请求api/css接口

This commit is contained in:
Argo-MacBookPro 2018-08-10 16:05:24 +08:00
parent 9f7e0ab0ba
commit 00e0b0b5e8
13 changed files with 63 additions and 79 deletions

View File

@ -89,7 +89,7 @@ namespace Bootstrap.Admin.Controllers
/// <returns></returns>
public ActionResult Settings()
{
return View(new NavigatorBarModel(this));
return View(new ThemeModel(this));
}
/// <summary>

View File

@ -1,33 +0,0 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
[Route("api/[controller]")]
public class CssController : Controller
{
/// <summary>
/// 获得当前样式接口
/// </summary>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public JsonResult Get()
{
return new JsonResult(DictHelper.RetrieveActiveCss().FirstOrDefault().Code);
}
/// <summary>
/// 获得网站所有样式表
/// </summary>
/// <returns></returns>
[HttpPost]
public IEnumerable<BootstrapDict> Post()
{
return DictHelper.RetrieveWebCss();
}
}
}

View File

@ -14,6 +14,7 @@ namespace Bootstrap.Admin.Models
{
Title = DictHelper.RetrieveWebTitle();
Footer = DictHelper.RetrieveWebFooter();
Theme = DictHelper.RetrieveActiveTheme();
}
/// <summary>
///
@ -23,5 +24,9 @@ namespace Bootstrap.Admin.Models
///
/// </summary>
public string Footer { get; private set; }
/// <summary>
///
/// </summary>
public string Theme { get; private set; }
}
}

View File

@ -10,17 +10,13 @@ namespace Bootstrap.Admin.Models
/// <summary>
///
/// </summary>
public class ProfilesModel : NavigatorBarModel
public class ProfilesModel : ThemeModel
{
/// <summary>
/// 获得/设置 头像文件大小
/// </summary>
public long Size { get; }
/// <summary>
/// 获得 系统配置的所有样式表
/// </summary>
public IEnumerable<BootstrapDict> Csss { get; }
/// <summary>
///
/// </summary>
/// <param name="controller"></param>
@ -32,8 +28,6 @@ namespace Bootstrap.Admin.Models
{
Size = new FileInfo(fileName).Length;
}
Csss = DictHelper.RetrieveWebCss();
}
}
}

View File

@ -0,0 +1,26 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace Bootstrap.Admin.Models
{
/// <summary>
///
/// </summary>
public class ThemeModel : NavigatorBarModel
{
/// <summary>
///
/// </summary>
/// <param name="controller"></param>
public ThemeModel(ControllerBase controller) : base(controller)
{
Themes = DictHelper.RetrieveThemes();
}
/// <summary>
/// 获得 系统配置的所有样式表
/// </summary>
public IEnumerable<BootstrapDict> Themes { get; }
}
}

View File

@ -84,7 +84,7 @@
<label class="control-label" for="userName">样式</label>
<select id="css" class="select form-control">
<option value="">默认样式</option>
@foreach (var css in Model.Csss)
@foreach (var css in Model.Themes)
{
if (css.Code == Model.Css)
{

View File

@ -1,4 +1,4 @@
@model Bootstrap.Admin.Models.NavigatorBarModel
@model Bootstrap.Admin.Models.ThemeModel
@{
ViewBag.Title = "网站设置";
Layout = "~/Views/Shared/_Admin.cshtml";
@ -48,9 +48,21 @@
<form id="CssDataForm" class="form-inline">
<div class="form-group">
<div class="dropdown">
<button id="dictCssDefine" class="btn btn-success dropdown-select dropdown-toggle" data-toggle="dropdown" data-default-val="">默认样式</button>
<button id="dictCssDefine" class="btn btn-success dropdown-select dropdown-toggle" data-toggle="dropdown" data-default-val="" value="@Model.Theme">默认样式</button>
<ul class="dropdown-menu" role="menu" id="cssContainer">
<li><a href="#" data-val="">默认样式</a></li>
@foreach (var css in Model.Themes)
{
if (css.Code == Model.Css)
{
<li><a href="#" data-val="@css.Code">@css.Name</a></li>
}
else
{
<li><a href="#" data-val="@css.Code">@css.Name</a></li>
}
}
</ul>
</div>
<button class="btn btn-secondary" type="button" data-method="css">保存</button>

View File

@ -1,4 +1,4 @@
@model Bootstrap.Admin.Models.HeaderBarModel
@model ModelBase
<footer class="site-footer">
<div class="text-center">
<span id="websiteFooter">@Model.Footer</span>

View File

@ -1,4 +1,4 @@
@model Bootstrap.Admin.Models.HeaderBarModel
@model HeaderBarModel
<header class="header">
<div class="bg"></div>
<div class="d-flex align-items-center">

View File

@ -1,4 +1,5 @@
@{
@model ModelBase
@{
Layout = "~/Views/Shared/_Root.cshtml";
}
@section css {
@ -16,6 +17,10 @@
<link href="~/css/theme-responsive.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/css/site-responsive.css" rel="stylesheet" />
@if (!string.IsNullOrEmpty(Model.Theme))
{
<link href="~/css/@Model.Theme" rel="stylesheet" />
}
}
@section javascript {
<environment include="Development">

View File

@ -257,8 +257,7 @@
// Dicts
Dicts = {
url: 'api/Dicts/',
css: 'api/Css/',
url: 'api/Dicts/'
};
// Profiles
@ -373,14 +372,3 @@
}
});
})(jQuery);
$(function () {
// loading customer css
$.bc({
url: Dicts.css, swal: false, method: 'get',
callback: function (result) {
if (result.length > 0)
$('head').append($.format('<link href="{0}css/{1}" rel="stylesheet" type="text/css" />', $('#pathBase').attr('href'), result));
}
});
});

View File

@ -117,19 +117,5 @@
listCacheUrl();
});
var $css = $('#dictCssDefine');
$.bc({
url: Dicts.css, swal: false,
callback: function (result) {
var html = result.map(function (ele, index) { return $.format('<li><a href="#" data-val="{1}">{0}</a></li>', ele.Name, ele.Code); }).join('');
$('#cssContainer').append(html);
$.bc({
url: Dicts.css, swal: false, method: 'get',
callback: function (result) {
if (result.length > 0)
$css.val(result).dropdown('val');
}
});
}
});
var $css = $('#dictCssDefine').dropdown('val');
})

View File

@ -163,7 +163,7 @@ namespace Bootstrap.DataAccess
/// 获得系统中配置的可以使用的网站样式
/// </summary>
/// <returns></returns>
public static IEnumerable<BootstrapDict> RetrieveWebCss()
public static IEnumerable<BootstrapDict> RetrieveThemes()
{
var data = RetrieveDicts();
return data.Where(d => d.Category == "网站样式");
@ -172,10 +172,11 @@ namespace Bootstrap.DataAccess
/// 获得网站设置中的当前样式
/// </summary>
/// <returns></returns>
public static IEnumerable<BootstrapDict> RetrieveActiveCss()
public static string RetrieveActiveTheme()
{
var data = RetrieveDicts();
return data.Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0 && !d.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase));
var theme = data.Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0).FirstOrDefault();
return theme == null ? string.Empty : (theme.Code.Equals("site.css", StringComparison.OrdinalIgnoreCase) ? string.Empty : theme.Code);
}
/// <summary>
/// 获取头像路径