0

我使用 asp.net 核心代码弹出并在主视图中附加 html 和 js 文件,但如果有人知道如何解决,我会收到 $ not found 之类的错误请帮助

我的 ActionFilter 代码:-

    private readonly IStoreContext _storeContext;
    private readonly ISettingService _settingService;
    private readonly ILogger _logger;
    private readonly ILocalizationService _localizationService;
    private readonly IWorkContext _workContext;
    private readonly ITopicService _topicService;
    private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;

    #endregion

    #region const

    public PopupEngageFilterAttribute()
    {
        this._storeContext = EngineContext.Current.Resolve<IStoreContext>();
        this._settingService = EngineContext.Current.Resolve<ISettingService>();
        this._logger = EngineContext.Current.Resolve<ILogger>();
        this._localizationService = EngineContext.Current.Resolve<ILocalizationService>();
        this._workContext = EngineContext.Current.Resolve<IWorkContext>();
        this._topicService = EngineContext.Current.Resolve<ITopicService>();
        this._newsLetterSubscriptionService = EngineContext.Current.Resolve<INewsLetterSubscriptionService>();
    }

    #endregion

    #region methods

    public void PopupEngageOnResultExecuted(ActionExecutedContext filterContext)
    {
        var storeId = _storeContext.CurrentStore.Id;

        LicenseImplementer licenseImplementer = new LicenseImplementer();

        // load plugin settings
        var _setting = _settingService.LoadSetting<PopupEngageSetting>(storeId);
        var allStoreSettings = _settingService.LoadSetting<PopupEngageSetting>(0);

        //check plugin is enabled or not
        if (_setting.PopupEngageEnabled)
        {
            // check license 
            //if (!licenseImplementer.IsLicenseActive(allStoreSettings.LicenseKey, allStoreSettings.OtherLicenseSettings))
            //    return;

            StringBuilder sb = new StringBuilder();
            string bioepEngageScript = string.Empty;
            string popupEngageView = string.Empty;
            string popupEngageScript = string.Empty;
            string newsLetterScript = string.Empty;

            // get current customer
            var customer = _workContext.CurrentCustomer;

            // check customer cart
            string customerCart = Convert.ToString(customer.HasShoppingCartItems);

            // set cookie for customer cart 
            filterContext.HttpContext.Response.Cookies.Append("CustomerCart", customerCart, new CookieOptions() { Path = "/", HttpOnly = false, Secure = false });

            if(customerCart == "True")
            {
                // get bioep script file
                Stream bioepScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.bioep.min.js");
                if (bioepScriptFile != null)
                    using (StreamReader reader = new StreamReader(bioepScriptFile))
                    {
                        bioepEngageScript = reader.ReadToEnd();
                    }

                // get PopupEngage script
                string path = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Script"), "PopupEngage.js");

                if (File.Exists(path))
                {
                    popupEngageScript = File.ReadAllText(path);
                }

                // check current customers role
                var customerRole = customer.CustomerRoles.Where(x => x.Name == "Guests").FirstOrDefault();
                if (customerRole != null)
                {
                    // get Popup View file
                    string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngageNewsLetter.html");
                    if (File.Exists(popupEngageViewFile))
                    {
                        popupEngageView = File.ReadAllText(popupEngageViewFile);
                    }

                    // get NewsLetter Script file
                    Stream newsLetterScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.NewsLetter.js");
                    if (newsLetterScriptFile != null)
                        using (StreamReader reader = new StreamReader(newsLetterScriptFile))
                        {
                            newsLetterScript = reader.ReadToEnd();
                        }
                }
                else
                {
                    // get Popup View file
                    string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngage.html");
                    if (File.Exists(popupEngageViewFile))
                    {
                        popupEngageView = File.ReadAllText(popupEngageViewFile);
                    }
                }

                var topicBody=string.Empty;

                // get topic from settings
                var topic = _setting.TopicName;
                if (!string.IsNullOrEmpty(topic))
                {
                    // get topic by system name
                    var topicRecord = _topicService.GetTopicBySystemName(topic);
                    if(topicRecord != null)
                    {
                        topicBody = topicRecord.Body;
                    }
                }

                // replace new line with slash and double coute with single coute
                popupEngageView = popupEngageView.Replace(Environment.NewLine, String.Empty).Replace("\"", "'");
                topicBody = topicBody.Replace(Environment.NewLine, String.Empty).Replace("\"", "'");

                // append script
                sb.Append("<script type=\"text/javascript\" src=\"/wwwroot/lib/jquery-1.10.2.min.js\">\n\t");
                sb.Append(bioepEngageScript);
                sb.Append(popupEngageScript);
                sb.Append("$(\"" + popupEngageView + "\").insertAfter(\".newsletter\");");
                sb.Append("$('.popupengage_popupmsg').html(\"" + topicBody + "\");");
                sb.Append(newsLetterScript);
                sb.Append("</script>\n");
                var bytes = Encoding.ASCII.GetBytes(sb.ToString());
                filterContext.HttpContext.Response.Body.WriteAsync(bytes,0, bytes.Length);
            }
        }
    }
    #endregion

文件以完美的方式附加,但它在 jquery 之前将脚本附加到页面顶部。并且该脚本由字符串生成器附加。弹出js示例

4

1 回答 1

1

如果您使用的是 jquery,请确保它包含在使用 jquery 功能的脚本文件之前;

例如:如果您有一个名为“main.js”的 js 文件,其中包含类似的行,$().forEach那么您在 html 文件中的包含顺序应该是

<script>jquery.js </scrpt>
<script>main.js </scrpt>
于 2017-11-23T08:05:32.497 回答