/// <summary> /// 加载皮肤的css /// </summary> /// <param name="controllerContext"><see cref="RequestContext"/></param> void IThemeResolver.IncludeStyle(RequestContext controllerContext) { string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey"); if (String.IsNullOrEmpty(spaceKey)) { spaceKey = UserContext.CurrentUser.UserName; } IUserService userService = DIContainer.Resolve <IUserService>(); User user = userService.GetFullUser(spaceKey); if (user == null) { throw new ExceptionFacade(new ResourceExceptionDescriptor().WithUserNotFound(spaceKey, 0)); } PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.UserSpace); if (presentArea == null) { return; } string themeKey = null; string appearanceKey = null; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest <IPageResourceManager>(); if (!presentArea.EnableThemes) { string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); return; } if (user.IsUseCustomStyle) { var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, user.UserId); if (customStyleEntity == null) { return; } CustomStyle customStyle = customStyleEntity.CustomStyle; if (customStyle == null) { return; } string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : ""); string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, user.UserId); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); StringBuilder builder = new StringBuilder(); builder.AppendLine(".tn-page-bg{"); if (customStyle.IsUseBackgroundImage) { builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');"); builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } builder.AppendFormat("background-position:{0} top;\n", position); } builder.AppendLine("}"); builder.AppendLine("#tn-content{"); builder.AppendLine("margin-top:" + customStyle.HeaderHeight.ToString() + "px;"); builder.AppendLine("}"); resourceManager.RegisterStyleBlock(builder.ToString()); } else { string[] themeAppearanceArray = user.ThemeAppearance.Split(','); var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, user.ThemeAppearance); if (appearance != null && themeAppearanceArray.Count() == 2) { themeKey = themeAppearanceArray[0]; appearanceKey = themeAppearanceArray[1]; } else { themeKey = presentArea.DefaultThemeKey; appearanceKey = presentArea.DefaultAppearanceKey; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); } }
/// <summary> /// 自定义皮肤设置 /// </summary> /// <param name="presentAreaKey"></param> /// <param name="ownerId"></param> /// <param name="isUseCustomStyle"></param> /// <returns></returns> public ActionResult _CustomSettings(string presentAreaKey = PresentAreaKeysOfBuiltIn.UserSpace, long ownerId = 0, bool isUseCustomStyle = false) { if (!ThemeService.Validate(presentAreaKey, ownerId)) return Content("没有设置皮肤的权限"); var customStyleEntity = new CustomStyleService().Get(presentAreaKey, ownerId); CustomStyle customStyle = null; if (customStyleEntity != null) { customStyle = customStyleEntity.CustomStyle; } //配色方案相关操作 IEnumerable<CustomStyle> colorSchemes = new CustomStyleService().GetColorSchemes(presentAreaKey); ViewData["colorSchemes"] = colorSchemes; if (customStyle == null) { customStyle = CustomStyle.New(); BackgroundImageStyle backgroundImageStyle = new BackgroundImageStyle(); customStyle.BackgroundImageStyle = backgroundImageStyle; Dictionary<string, string> definedColours = null; if (colorSchemes.Count() > 0) definedColours = colorSchemes.First().DefinedColours; else { definedColours = new Dictionary<string, string>(); definedColours[ColorLabel.PageBackground.ToString()] = "#f2e3bf"; definedColours[ColorLabel.ContentBackground.ToString()] = "#f8f0e6"; definedColours[ColorLabel.BorderBackground.ToString()] = "#ebe6d9"; definedColours[ColorLabel.MainTextColor.ToString()] = "#666"; definedColours[ColorLabel.SubTextColor.ToString()] = "#ccc"; definedColours[ColorLabel.MainLinkColor.ToString()] = "#cc6673"; definedColours[ColorLabel.SubLinkColor.ToString()] = "#efc0ca"; } customStyle.DefinedColours = definedColours; } Dictionary<string, object> customStyleCssBlock = new Dictionary<string, object>(); if (customStyle.IsUseBackgroundImage) { customStyleCssBlock.TryAdd("background-image", "url('" + customStyle.BackgroundImageStyle.Url + "')"); customStyleCssBlock.TryAdd("background-repeat", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); customStyleCssBlock.TryAdd("background-attachment", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } customStyleCssBlock.TryAdd("background-position", position + " top"); } ViewData["customStyleCssBlock"] = System.Web.Helpers.Json.Encode(customStyleCssBlock); List<SelectListItem> selectHeaderHeight = new List<SelectListItem> { new SelectListItem { Text = "20", Value = "20" }, new SelectListItem { Text = "60", Value = "60" }, new SelectListItem { Text = "100", Value = "100" } }; ViewData["HeaderHeight"] = new SelectList(selectHeaderHeight, "Value", "Text", customStyle.HeaderHeight); return View(customStyle); }
void IThemeResolver.IncludeStyle(RequestContext controllerContext) { string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey"); TopicEntity topic = new TopicService().Get(spaceKey); if (topic == null) return; PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfExtension.TopicSpace); if (presentArea == null) return; string themeKey = null; string appearanceKey = null; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest<IPageResourceManager>(); if (!presentArea.EnableThemes) { string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); return; } if (topic.IsUseCustomStyle) { var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, topic.TopicId); if (customStyleEntity == null) return; CustomStyle customStyle = customStyleEntity.CustomStyle; if (customStyle == null) return; string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : ""); string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, topic.TopicId); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); StringBuilder builder = new StringBuilder(); builder.AppendLine(".tn-page-bg{"); if (customStyle.IsUseBackgroundImage && !string.IsNullOrEmpty(customStyle.BackgroundImageStyle.Url)) { builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');"); builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } builder.AppendFormat("background-position:{0} top;\n", position); } builder.AppendLine("}"); resourceManager.RegisterStyleBlock(builder.ToString()); } else { string[] themeAppearanceArray = topic.ThemeAppearance.Split(','); var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfExtension.TopicSpace, topic.ThemeAppearance); if (appearance != null && themeAppearanceArray.Count() == 2) { themeKey = themeAppearanceArray[0]; appearanceKey = themeAppearanceArray[1]; } else { themeKey = presentArea.DefaultThemeKey; appearanceKey = presentArea.DefaultAppearanceKey; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); } }
//使用RazorEngine解析 \Themes\[PresentAreaKey]\CustomStyle\css-dark.xml 或 css-light.xml public override void ProcessRequest(HttpContext context) { string presentAreaKey = context.Request.QueryString.GetString("PresentAreaKey", string.Empty); long ownerId = context.Request.QueryString.Get<long>("OwnerId", 0); if (string.IsNullOrEmpty(presentAreaKey)) { WebUtility.Return404(context); } CustomStyle customStyle = null; if (ownerId > 0) { CustomStyleEntity entity = new CustomStyleService().Get(presentAreaKey, ownerId); if (entity == null) WebUtility.Return404(context); customStyle = entity.CustomStyle; //通过QueryString 传递PresentAreaKey、OwnerId 获取css (使用客户端缓存) DateTime lastModified = customStyle.LastModified.ToUniversalTime(); if (IsCacheOK(context, lastModified)) { WebUtility.Return304(context); return; } context.Response.Cache.SetCacheability(HttpCacheability.Private); context.Response.Cache.SetLastModified(lastModified); context.Response.Cache.SetETag(lastModified.Ticks.ToString()); context.Response.Cache.SetAllowResponseInBrowserHistory(true); context.Response.Cache.SetValidUntilExpires(true); } else { string customStyleJson = context.Request.QueryString.GetString("CustomStyle", string.Empty); bool isDark = context.Request.QueryString.GetBool("isDark", false); Dictionary<string, string> dictionary = Json.Decode<Dictionary<string, string>>(customStyleJson) ?? new Dictionary<string, string>(); customStyle = CustomStyle.New(); customStyle.IsDark = isDark; customStyle.DefinedColours = dictionary; context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //通过QueryString 传递PresentAreaKey、CustomStyle的json数据 获取css(自定义风格时使用,禁用客户端缓存) } dynamic model = new ExpandoObject(); if (customStyle.DefinedColours.ContainsKey(ColorLabel.PageBackground.ToString())) model.PageBackground = customStyle.DefinedColours[ColorLabel.PageBackground.ToString()]; else model.PageBackground = "#f2e3bf"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.ContentBackground.ToString())) model.ContentBackground = customStyle.DefinedColours[ColorLabel.ContentBackground.ToString()]; else model.ContentBackground = "#f8f0e6"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.BorderBackground.ToString())) model.BorderBackground = customStyle.DefinedColours[ColorLabel.BorderBackground.ToString()]; else model.BorderBackground = "#ebe6d9"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.MainTextColor.ToString())) model.MainTextColor = customStyle.DefinedColours[ColorLabel.MainTextColor.ToString()]; else model.MainTextColor = "#666"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.SubTextColor.ToString())) model.SubTextColor = customStyle.DefinedColours[ColorLabel.SubTextColor.ToString()]; else model.SubTextColor = "#ccc"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.MainLinkColor.ToString())) model.MainLinkColor = customStyle.DefinedColours[ColorLabel.MainLinkColor.ToString()]; else model.MainLinkColor = "#cc6673"; if (customStyle.DefinedColours.ContainsKey(ColorLabel.SubLinkColor.ToString())) model.SubLinkColor = customStyle.DefinedColours[ColorLabel.SubLinkColor.ToString()]; else model.SubLinkColor = "#efc0ca"; string templateName = "css-dark"; if (customStyle.IsDark) templateName = "css-light"; XElement rootElement = XElement.Load(WebUtility.GetPhysicalFilePath(string.Format("~/Themes/{0}/Custom/css-{1}.xml", presentAreaKey, customStyle.IsDark ? "dark" : "light"))); try { //编译模板 string result = Razor.Parse(rootElement.Value, model, templateName); context.Response.Clear(); context.Response.ContentType = "text/css"; StreamWriter sw = new StreamWriter(context.Response.OutputStream, System.Text.Encoding.Default); sw.Write(result); sw.Flush(); } catch (Exception ex) { context.Response.Write(ex.ToString()); } }
/// <summary> /// 加载皮肤的css /// </summary> /// <param name="controllerContext"><see cref="RequestContext"/></param> void IThemeResolver.IncludeStyle(RequestContext controllerContext) { string spaceKey = controllerContext.GetParameterFromRouteDataOrQueryString("SpaceKey"); if(String.IsNullOrEmpty(spaceKey)) spaceKey = UserContext.CurrentUser.UserName; IUserService userService = DIContainer.Resolve<IUserService>(); User user = userService.GetFullUser(spaceKey); if (user == null) throw new ExceptionFacade(new ResourceExceptionDescriptor().WithUserNotFound(spaceKey, 0)); PresentArea presentArea = new PresentAreaService().Get(PresentAreaKeysOfBuiltIn.UserSpace); if (presentArea == null) return; string themeKey = null; string appearanceKey = null; IPageResourceManager resourceManager = DIContainer.ResolvePerHttpRequest<IPageResourceManager>(); if (!presentArea.EnableThemes) { string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, presentArea.DefaultThemeKey, presentArea.DefaultAppearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); return; } if (user.IsUseCustomStyle) { var customStyleEntity = new CustomStyleService().Get(presentArea.PresentAreaKey, user.UserId); if (customStyleEntity == null) return; CustomStyle customStyle = customStyleEntity.CustomStyle; if (customStyle == null) return; string themeCssPath = string.Format("{0}/Custom/theme{1}.css", presentArea.ThemeLocation, customStyle.IsDark ? "-deep" : ""); string appearanceCssPath = SiteUrls.Instance().CustomStyle(presentArea.PresentAreaKey, user.UserId); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); StringBuilder builder = new StringBuilder(); builder.AppendLine(".tn-page-bg{"); if (customStyle.IsUseBackgroundImage) { builder.AppendLine("background-image:url('" + customStyle.BackgroundImageStyle.Url + @"');"); builder.AppendFormat("background-repeat:{0};\n", customStyle.BackgroundImageStyle.IsRepeat ? "repeat" : "no-repeat"); builder.AppendFormat("background-attachment:{0};\n", customStyle.BackgroundImageStyle.IsFix ? "fixed" : "scroll"); string position = "center"; switch (customStyle.BackgroundImageStyle.BackgroundPosition) { case BackgroundPosition.Left: position = "left"; break; case BackgroundPosition.Center: position = "center"; break; case BackgroundPosition.Right: position = "right"; break; default: position = "center"; break; } builder.AppendFormat("background-position:{0} top;\n", position); } builder.AppendLine("}"); builder.AppendLine("#tn-content{"); builder.AppendLine("margin-top:" + customStyle.HeaderHeight.ToString() + "px;"); builder.AppendLine("}"); resourceManager.RegisterStyleBlock(builder.ToString()); } else { string[] themeAppearanceArray = user.ThemeAppearance.Split(','); var appearance = new ThemeService().GetThemeAppearance(PresentAreaKeysOfBuiltIn.UserSpace, user.ThemeAppearance); if (appearance != null && themeAppearanceArray.Count() == 2) { themeKey = themeAppearanceArray[0]; appearanceKey = themeAppearanceArray[1]; } else { themeKey = presentArea.DefaultThemeKey; appearanceKey = presentArea.DefaultAppearanceKey; } string themeCssPath = string.Format("{0}/{1}/theme.css", presentArea.ThemeLocation, themeKey); string appearanceCssPath = string.Format("{0}/{1}/Appearances/{2}/appearance.css", presentArea.ThemeLocation, themeKey, appearanceKey); resourceManager.IncludeStyle(themeCssPath); resourceManager.IncludeStyle(appearanceCssPath); } }