自从 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. 7.ViewPagerIndicator

     ViewPager指针项目,在使用ViewPager的时候能够指示ViewPager所在的位置,就像Google Play中切换的效果一样,还能使用在应用初始化的介绍页面</item> ...

  2. Java 实现网络图片的读取与下载

    //网络图片的下载,读取与删除 public static void fileDowAndDel(String httpurl){ try { URL url = new URL(httpurl); ...

  3. Apache重写规则

    1..htaccess文件使用前提 .htaccess的主要作用就是实现url改写,也就是当浏览器通过url访问到服务器某个文件夹时,作为主人,我们可以来接待这个url,具体 地怎样接待它,就是此文件 ...

  4. windows上xshell6的安装

    各位大兄弟,好用的xshell组件来了,话不多说,开搞. 软件链接链接:https://pan.baidu.com/s/1vcRo2L-LNe2BrJ9-VCy57A 密码:ei73 有下面四个软件, ...

  5. python中使用双端队列解决回文问题

    双端队列:英文名字:deque (全名double-ended queue)是一种具有队列和栈性质的抽象数据类型. 双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行. 双端队列可以在 ...

  6. 浏览器上安装vue devtools

    安装前要检查一下node版本的(node -v),必须将版本提高到>4.4.7.低版本的node在安装devtools时执行npm install 时报错.如何升级node版本,若在window ...

  7. (转)10大H5前端框架

    http://www.cnblogs.com/kingboy2008/p/5261771.html 作为一名做为在前端死缠烂打6年并且懒到不行的攻城士,这几年我还是阅过很多同门从知名到很知名的各种前端 ...

  8. Google Optimization Tools实现加工车间任务规划【Python版】

    上一篇介绍了<使用.NET Core与Google Optimization Tools实现加工车间任务规划>,这次将Google官方文档python实现的版本的完整源码献出来,以满足喜爱 ...

  9. 关于JS的一些东西

    1.声明Js代码域    1.在head标签中使用script声明js代码域    <head>        ....        <!--声明js代码域-->       ...

  10. Atom相关资料整理

    官网地址 https://atom.io/ Atom 中文社区 https://atom-china.org/ 常用插件 Emmet 这款插件是用来支持zend-coding,Emmet的前身是大名鼎 ...