NopCommerce 根据手机浏览器和桌面浏览器切换 Theme
自从 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的更多相关文章
- 在thinkPHP3.2.3框架下实现手机和PC端浏览器的切换
查看thinkphp版本号方法 打开文件“根目录\ThinkPHP\ThinkPHP.php”下的文件ThinkPHP.php,在22--23行可以看到版本信息THINK_VERSION,如下图: 说 ...
- 用手机自带uc浏览器查看静态页面,css样式不显示
问题描述: 这个问题是一个同事在写手机页面用UC浏览器测试以后遇到的,其他浏览器静态页面显示正常,唯独UC浏览器不显示页面样式. 我测试过代码是没有问题的,因为临时没有找到安卓手机,就猜想PC端的应该 ...
- QTP鼠标点击和浏览器事件的动态切换
今天在群里有人问到一个问题,我觉得应该会有很多人会碰到,今天根据自己的思路把这个解决方案整理出来,供自己和大家参考 需求描述: 当输入一个身份证号码的时候,这个号码所对应的数据会被加载到所属的省和市的 ...
- 电脑创建WIFI/无线热点之后, 手机QQ能上浏览器不能上网
这个完全是个人经验,绝对原创,请尊重博主原创权,转载请注明转于此博客. 问题如题,大家电脑创建无线热点之后, 有的人手机会出现QQ,微信能上网, 但是浏览器或者基于浏览器的那些比如应用商店不能上网, ...
- 浏览器开启桌面通知Web Notification
本文主要描述如何开启各个浏览器的桌面通知功能 一.谷歌浏览器(chrome) 点击地址栏前面的图标
- selenium WebDriver 对浏览器标签页的切换
关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...
- H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例
H5手机移动端调起浏览器(qq浏览器,uc浏览器)自带分享功能实例 (转载:https://blog.csdn.net/weixin_38787928/article/details/86741227 ...
- 移动端浏览器和微信浏览器上禁止body的滚动条
一般禁止body滚动的做法就是设置overflow:hidden. 但是很奇怪的发现在移动端浏览器和微信浏览器上这个不起作用,然后我分析了我的写法,就是在body上加了一个class去定义属性,然后改 ...
- JavaScript判断是否为微信浏览器或支付宝浏览器
可以用手机安装的微信和支付宝扫描下方二维码测试 <!DOCTYPE html> <html lang="en"> <head> <meta ...
随机推荐
- linux下编写C++程序播放音频
参考: https://blog.csdn.net/zlyaxixuexi/article/details/79014441 格式转换: https://www.media.io/zh/
- react小知识2
概述 这是我学习react的过程中,学到的一些简便写法,都是利用了es6的特性,记录下来供以后开发时参考,相信对其他人也有用. 参考资料:dva.js 知识导图 析构 我们也可以析构传入的函数参数. ...
- Shell - Shell脚本调试方法
Shell脚本调试选项 Shell本身提供一些调试方法选项: -n,读一遍脚本中的命令但不执行,用于检查脚本中的语法错误. -v,一边执行脚本,一边将执行过的脚本命令打印到标准输出. -x,提供跟踪执 ...
- iOS-Xcode解决【workspace integrity couldn't load project'】
出现如标题提示的项目打不开情况,多半可能是因为Xcode版本不同,或者是SVN/Git使用导致 解决办法一: 删除冲突代码,"项目文件->Pod文件夹->Pods.xcodepr ...
- DeepLab 使用 Cityscapes 数据集训练模型
原文地址:DeepLab 使用 Cityscapes 数据集训练模型 0x00 操作环境 OS: Ubuntu 16.04 LTS CPU: Intel® Core™ i7-4790K GPU: Ge ...
- Linux_CentOS-服务器搭建 <二>
Tomat安装: 说明: 源码安装,下载地址:http://tomcat.apache.org/.我下了个apache-tomcat-7.0.42.tar.gz 安装开始: 配置环境. JDK: vi ...
- 从零开始学 Web 之 BOM(一)BOM的概念,一些BOM对象
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...
- kafka集群partition分布原理分析
1. Kafka集群partition replication默认自动分配分析 下面以一个Kafka集群中4个Broker举例,创建1个topic包含4个Partition,2 Replication ...
- mysql查看表注释和字段注释的方法
1.取字段注释 Select COLUMN_NAME 列名, DATA_TYPE 字段类型, COLUMN_COMMENT 字段注释from INFORMATION_SCHEMA.COLUMNSWhe ...
- netty源码解解析(4.0)-13 Channel NIO实现: 关闭和清理
Channel提供了3个方法用来实现关闭清理功能:disconnect,close,deregister.本章重点分析这个3个方法的功能的NIO实现. disconnect实现: 断开连接 disco ...