小牛之路——提醒功能

来源:互联网 发布:大数据概念股票 编辑:程序博客网 时间:2024/05/20 19:17
直接上代码~
PS:作为职场新人,欢迎大牛批评指正,小弟不胜感激!
视图层:
1、设置定时器,根据自己的业务需求设置定时器时间和当前时间的格式

2、触发 Ajax 时尽量减少请求次数,做一个服务器是否响应的判断

     $(function () {
        run();              //加载页面时启动
        var interval;       //设置定时器
        function run() {    //设置参数
            interval = setInterval(chat, "60000");
        }
        function chat() {   //执行函数
            //1、获取当前时间
            var date = new Date()
            var Y = date.getFullYear()
            var M = date.getMonth() + 1
            var D = date.getDate()
            var h = date.getHours();
            var m = date.getMinutes();
            var s = date.getSeconds();
            time = Y + "/" + (M < 10 ? "0" + M : M) + "/" + (D < 10 ? "0" + D : D) + " " + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m);
            //alert(time);
            //2、判断是否响应
            var judge = true;
            if (judge)
            {
                judge = false;
                $.ajax({
                    url: "/Schedule/Remind",
                    data: { time: time },
                    success: function (data) {
                        judge = true;
                        if (data == "0") {
                            //alert("没有请求到数据!");接下来的操作~
                        }
                        else {
                            var obj = JSON.parse(data);
                            //$("#number").val(obj[0].Number);
                            //alert("请求到数据!");接下来的操作~                           
                            $(".remind img").attr("src", "http://localhost/Image/Index/reminders.png");
                            $(".remind img").bind({
                                mousemove: function () {
                                    $(this).css("cursor", "pointer");
                                    $(this).attr("src", "http://localhost/Image/Index/remingers.hover.png");
                                },
                                mouseout: function () {
                                    $(this).attr("src", "http://localhost/Image/Index/reminders.png");
                                },
                                click: function () {
                                    location.href = "/Employee/EmployeeInfo";
                                }
                            })
                        }
                    }
                })
            }           
        }
        function end() {    //关闭
            clearTimeout(interval);
        }
        function start() {  //重新启动
            end();
            interval = setInterval(chat, "60000");
        }
    })

控制器:
1、根据是否获取到数据,分别处理
2、list 转换为 string 的方法是很常用的

/// <summary>
        /// 日程提醒
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string Remind(DateTime? time)
        {
            var emodel = (Employee)Session["session-employee"];
            var number = emodel.Number;
            var list = new BLL.ScheduleLogic().Get(number, time);
            if (list == null || list.Count() < 1)
            {
                return "0";
            }
            else
            {
                var json = JsonConvert.SerializeObject(list);
                return json;
            }          
        }

逻辑层:
1、获取该员工当天所有的日程数据
2、根据时间进行判断,获取在提醒时间范围之内的信息

/// <summary>
        /// 日程提醒
        /// </summary>
        /// <param name="number"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public List<RemindScheduleModel> Get(string number, DateTime? time)
        {
            int yyyy = Convert.ToInt32(time.Value.ToString("yyyy"));
            int mm = Convert.ToInt32(time.Value.ToString("MM"));
            int dd = Convert.ToInt32(time.Value.ToString("dd"));
            var list = new ScheduleRepository(IUnitOfWork).GetModelList(s=>s.Employees.Where(e=>e.Number==number).Count()>0 && s.Time.Value.Year == yyyy && s.Time.Value.Month == mm && s.Time.Value.Day == dd && s.IsEff == "1").ToList();
            List<RemindScheduleModel> slist = new List<RemindScheduleModel>();
            foreach (var item in list)
            {
                TimeSpan t = new TimeSpan(item.Time.Value.Ticks) - new  TimeSpan(time.Value.Ticks);
                if(t.TotalMinutes<=30 && t.TotalMinutes>=-30)
                {
                    RemindScheduleModel model = new RemindScheduleModel();
                    model.Id = item.Id;
                    model.Time = item.Time;
                    model.Title = item.Title;
                    slist.Add(model);
                }


            }
            return slist;
        }

1 0
原创粉丝点击