自从 NopCommerce 升级到 3.x 以来,默认的 ViewName.Mobile.cshtml 方式就被响应式的默认 Theme 取代了。

但是在今天各种手机专用前端库大行其道的情况下,响应式主题在手机端上的体验并不好,与专门制作的手机版页面相比显得傻大笨粗。所以如果是一个投入比较大的 NopCommerce 电商网站,比较完美的办法是同时制作 PC 和手机网站 Theme,然后依据用户浏览时所使用的浏览器自动切换。

实现 NopCommerce Theme 依据用户浏览器自动切换需要对 NopCommerce 的代码做如下修改(基于 NopCommerce 3.7):

1. 为 UserAgentHelper 增加判断是否移动设备的方法

修改 Nop.Services.Helpers.IUserAgentHelper 接口添加如下方法声明:

bool IsMobileDevice();

修改 Nop.Services.Helpers.UserAgentHelper 类添加 IsMobileDevice() 方法的实现代码:

        public virtual bool IsMobileDevice()
{
bool isTablet = false;
if (bool.TryParse(_httpContext.Request.Browser["IsTablet"], out isTablet) && isTablet)
{
return false;
} var userAgent = _httpContext.Request.UserAgent.ToLowerInvariant();
//微信浏览器的 UA 为 “micromessenger”
if (_httpContext.Request.Browser.IsMobileDevice || userAgent.Contains("micromessenger"))
{
return true;
} return false;
}

2. 修改 ThemeContext 类添加主题根据是否移动浏览器切换的功能:

以下是完整的修改后 Nop.Web.Framework.Themes.ThemeContext 类:

using System;
using System.Linq;
using Nop.Core;
using Nop.Core.Domain;
using Nop.Core.Domain.Customers;
using Nop.Services.Common;
using Nop.Services.Helpers; namespace Nop.Web.Framework.Themes
{
/// <summary>
/// Theme context
/// </summary>
public partial class ThemeContext : IThemeContext
{
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly IGenericAttributeService _genericAttributeService;
private readonly StoreInformationSettings _storeInformationSettings;
private readonly IThemeProvider _themeProvider;
private readonly IUserAgentHelper _userAgentHelper; private bool _themeIsCached;
private string _cachedThemeName; public ThemeContext(IWorkContext workContext,
IStoreContext storeContext,
IGenericAttributeService genericAttributeService,
StoreInformationSettings storeInformationSettings,
IUserAgentHelper userAgentHelper,
IThemeProvider themeProvider)
{
this._workContext = workContext;
this._storeContext = storeContext;
this._genericAttributeService = genericAttributeService;
this._storeInformationSettings = storeInformationSettings;
this._themeProvider = themeProvider;
this._userAgentHelper = userAgentHelper;
} /// <summary>
/// Get or set current theme system name
/// </summary>
public string WorkingThemeName
{
get
{
if (_themeIsCached)
return _cachedThemeName; string theme = "";
/*
if (_storeInformationSettings.AllowCustomerToSelectTheme)
{
if (_workContext.CurrentCustomer != null)
theme = _workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.WorkingThemeName, _genericAttributeService, _storeContext.CurrentStore.Id);
}
*/ if (this._userAgentHelper.IsMobileDevice())
{
theme = "你的手机版 Theme 名称";
}//default store theme
if (string.IsNullOrEmpty(theme))
theme = _storeInformationSettings.DefaultStoreTheme; //ensure that theme exists
if (!_themeProvider.ThemeConfigurationExists(theme))
{
var themeInstance = _themeProvider.GetThemeConfigurations()
.FirstOrDefault();
if (themeInstance == null)
throw new Exception("No theme could be loaded");
theme = themeInstance.ThemeName;
} //cache theme
this._cachedThemeName = theme;
this._themeIsCached = true;
return theme;
}
set
{
if (!_storeInformationSettings.AllowCustomerToSelectTheme)
return; if (_workContext.CurrentCustomer == null)
return; _genericAttributeService.SaveAttribute(_workContext.CurrentCustomer, SystemCustomerAttributeNames.WorkingThemeName, value, _storeContext.CurrentStore.Id); //clear cache
this._themeIsCached = false;
}
}
}
}

注意代码中

theme = "你的手机版 Theme 名称";

一行,这里的字符串修改为你的手机 Theme 的名称,也就是 Theme 的文件夹名。

修改完成以后,默认的桌面浏览器 Theme 将使用后台设置的主题,一旦检测到移动浏览器则使用代码里手机浏览器 Theme。

这里我们把手机 Theme 名称写死了,当然更好的办法是修改后台增加一个手机 Theme 的设置,不过那个需要修改的就代码就比较多了,各位读者可以自行实现。

NopCommerce 根据手机浏览器和桌面浏览器切换 Theme的更多相关文章

  1. 在thinkPHP3.2.3框架下实现手机和PC端浏览器的切换

    查看thinkphp版本号方法 打开文件“根目录\ThinkPHP\ThinkPHP.php”下的文件ThinkPHP.php,在22--23行可以看到版本信息THINK_VERSION,如下图: 说 ...

  2. 用手机自带uc浏览器查看静态页面,css样式不显示

    问题描述: 这个问题是一个同事在写手机页面用UC浏览器测试以后遇到的,其他浏览器静态页面显示正常,唯独UC浏览器不显示页面样式. 我测试过代码是没有问题的,因为临时没有找到安卓手机,就猜想PC端的应该 ...

  3. QTP鼠标点击和浏览器事件的动态切换

    今天在群里有人问到一个问题,我觉得应该会有很多人会碰到,今天根据自己的思路把这个解决方案整理出来,供自己和大家参考 需求描述: 当输入一个身份证号码的时候,这个号码所对应的数据会被加载到所属的省和市的 ...

  4. 电脑创建WIFI/无线热点之后, 手机QQ能上浏览器不能上网

    这个完全是个人经验,绝对原创,请尊重博主原创权,转载请注明转于此博客. 问题如题,大家电脑创建无线热点之后, 有的人手机会出现QQ,微信能上网, 但是浏览器或者基于浏览器的那些比如应用商店不能上网, ...

  5. 浏览器开启桌面通知Web Notification

    本文主要描述如何开启各个浏览器的桌面通知功能 一.谷歌浏览器(chrome) 点击地址栏前面的图标

  6. selenium WebDriver 对浏览器标签页的切换

    关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...

  7. H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例

    H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例 (转载:https://blog.csdn.net/weixin_38787928/article/details/86741227 ...

  8. 移动端浏览器和微信浏览器上禁止body的滚动条

    一般禁止body滚动的做法就是设置overflow:hidden. 但是很奇怪的发现在移动端浏览器和微信浏览器上这个不起作用,然后我分析了我的写法,就是在body上加了一个class去定义属性,然后改 ...

  9. JavaScript判断是否为微信浏览器或支付宝浏览器

    可以用手机安装的微信和支付宝扫描下方二维码测试 <!DOCTYPE html> <html lang="en"> <head> <meta ...

随机推荐

  1. HTML中的文本标签

    <span></span> 请使用 <span> 来组合行内元素,以便通过样式来格式化它们. 注释:span 没有固定的格式表现.当对它应用样式时,它才会产生视觉上 ...

  2. JDK 安装目录中 native2ascii.exe 命令详解

    native2ascii 简介 native2ascii 是 sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt,*.ini,*.properties,*.java等等)编码 ...

  3. Restore database use sql `*.bak` file

    1.第一步: 2.第二步: 3.第三布: 4.第四步:

  4. 使用Swagger 搭建高可读性ASP.Net WebApi文档

    一.前言 在最近一个商城项目中,使用WebApi搭建API项目.但开发过程中,前后端工程师对于沟通接口的使用,是非常耗时的.之前也有用过Swagger构建WebApi文档,但是API文档的可读性并不高 ...

  5. Maven classifier 元素妙用

    首先来看这么一个依赖 <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json- ...

  6. 必须熟练的基础linux命令

    推荐:命令大全 http://man.linuxde.net/ 重要的几个热键[Tab],[ctrl]-c, [ctrl]-d [Tab]按键---具有『命令补全』不『档案补齐』的功能 [Ctrl]- ...

  7. 机器学习与Tensorflow(4)——卷积神经网络与tensorflow实现

    1.标准卷积神经网络 标准的卷积神经网络由输入层.卷积层(convolutional layer).下采样层(downsampling layer).全连接层(fully—connected laye ...

  8. 基数排序的理解和实现(Java)

    基数排序是桶排序的扩展算法,其思想是:将整数按位数切割成不同的数字,然后按每个位数分别比较排序. 算法流程: 将所有待比较数值统一为同样的数位长度,数位较短的数前面补零. 从最低位开始,依次进行一次排 ...

  9. Android应用系列:手把手教你做一个小米通讯录(附图附源码)

    前言 最近心血来潮,突然想搞点仿制品玩玩,很不幸小米成为我苦逼的第一个试验品.既然雷布斯的MIUI挺受欢迎的(本人就是其的屌丝用户),所以就拿其中的一些小功能做一些小demo来玩玩.小米的通讯录大家估 ...

  10. 关于dao层的封装和前端分页的结合(文章有点长,耐心点哦)

    任何一个封装讲究的是,实用,多状态.Action:     任何一个Action继承分页有关参数类PageManage,自然考虑的到分页效果,我们必须定义下几个分页的参数.并根据这个参数进行查值. 然 ...