自从 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. Hibernate入门1 - Hibernate概述及第一个小例子

    一.什么是ORM? ORM,即Object Relational Mapping.我们知道,利用面向对象的思想编写的数据库应用程序最终都是把对象信息保存在关系型数据库中,于是需要编写与底层数据库相关的 ...

  2. Android基础-系统架构分析,环境搭建,下载Android Studio,AndroidDevTools,Git使用教程,Github入门,界面设计介绍

    系统架构分析 Android体系结构 安卓结构有四大层,五个部分,Android分四层为: 应用层(Applications),应用框架层(Application Framework),系统运行层(L ...

  3. Egg中使用egg-mongoose和常用的Mongoose 方法

    Mongoose Mongoose就是一套操作MongoDB数据库的接口,而Egg中有对应的插件egg-mongoose. 安装 $ npm install egg-mongoose --save 配 ...

  4. Java运行环境(win10)

    系统安装Java后,配置运行环境,我的系统是win10,之前随便装了,没想到最近执行javac命令报错,(网上找了一堆都没用)处理方式如下: 环境变量-新建:变量名:%JAVA_HOME%  变量值: ...

  5. Pycharm 连接Linux 远程开发

    Pycharm 连接Linux 远程开发 在Liunx上安装python3.6(Ubuntu16) 下载Python-3.6.4.tgz 解压 tar -xzvf Python-3.6.4.tgz 进 ...

  6. 【WebAPI No.5】Core WebAPI中的自定义格式化

    介绍 Web API为JSON和XML提供媒体类型格式化程序.框架默认将这些格式化程序插入管道中.客户端可以在HTTP请求的Accept标头中请求JSON或XML. 格式化数据这个东西,其实没有什么最 ...

  7. C#单元测试分享ppt

    单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类, ...

  8. 使用Jenkins部署Spring Boot项目

    jenkins是devops神器,本篇文章介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署分为三个步骤: 第一步,jenkins安装 第二步,插件安装和配置 第 ...

  9. 2018.4.23-ml笔记(线性回归、梯度下降)

    线性回归:找到最合适的一条线来最好的拟合我们的数据点. hθ(x) = θixi=θTx    θ被称之为权重参数    θ0为拟合参数 对每个样本yi=θTxi + εi    误差ε是独立并且具有 ...

  10. Java工程师学习指南 完结篇

    Java工程师学习指南 完结篇 先声明一点,文章里面不会详细到每一步怎么操作,只会提供大致的思路和方向,给大家以启发,如果真的要一步一步指导操作的话,那至少需要一本书的厚度啦. 因为笔者还只是一名在校 ...