'use strict'; var app = angular.module('app', [ 'angularUtils.directives.dirPagination', ////分页用到的插件 'ngSanitize'///$sanitize这个服务.此服务依赖于ngSanitize模块.(这个模块需要加载angular-sanitize.js插件) ////应用ng-bind-html如果不引用此模块,会报安全错误 ]); app.controller("dutyCtrl", ["$http", function ($http) { var self = this; self.userId = $.cookie("GlWorkPlatform-userid"); self.userName = $.cookie("GlWorkPlatform-username"); self.get_AccessToken = localStorage.getItem("GlWorkPlatform-AccessToken"); var postCfg = { headers: { 'Content-Type': 'application/json', 'Authorization': "Bearer " + self.get_AccessToken } }; ////加载本周的值班安排 self.getNowDay = function () { var objDate = new Date(); var str, colorhead, colorfoot; var yy = objDate.getFullYear(); var MM = objDate.getMonth() + 1; var dd = objDate.getDate(); str = yy + "年" + MM + "月" + dd + "日"; return str; }; var getStart = getWeekNumber(new Date()); self.duty = { year: sp.currentYear(), weekNum: getStart.week, nowDay: self.getNowDay(), weekday: getStart.weekday } //console.log(getStart); var weekNum = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]; self.weekinfo = []; self.getWeekInfo = function () { var data = { year:self.duty.year, weekNum: self.duty.weekNum }; $http.get("json/duty1.txt", data, postCfg) .success(function (res) { if (res.success) { self.weekinfo = res.Items; $.each(self.weekinfo, function (index, item) { self.weekinfo[index].weekNum = weekNum[index]; ////往数组中插入一个叫weekNum的变量 self.weekinfo[index].day = getStart.dateString.split(",")[index]; }); //console.log(JSON.stringify(self.weekinfo)); } }); }; self.getWeekInfo(); //数据表格的控制器,动态加载table表格数据 self.noDataTip = 1; //声明没有数据时变量 self.rows = []; self.pageno = 1; self.total_count = 0; self.itemsPerPage = 8; self.pageOptions = [8, 12, 16, 20, 24, 26]; self.getData = function (pageno) { self.pageno = pageno; self.rows = []; var data = { pagesize: self.itemsPerPage, pageno: self.pageno }; $http.get("json/duty2.txt", data, postCfg) .success(function (res) { if (res.Items == null || res.Items == undefined || res.Items == "") { self.noDataTip = 0; } else { self.noDataTip = 1; self.rows = res.Items; self.total_count = res.total_count; $.each(self.rows, function (index,item) { item.tdate = sp.format(item.tdate); }); } }); }; ///先加载该用户设置的分页码,有设置的话就用,没有就用上面的变量 self.loadData = function () { var data = { userid: "admin", gridid: window.location.pathname }; $http.get("json/getPageNo.txt", data, postCfg) .success(function (res) { console.log(res) if (res.success) { self.itemsPerPage = parseInt(res.data); } ///根据详情页返回的页码,加载该页码的数据 if (self.get_lastReturnPageno == undefined || self.get_lastReturnPageno == null || self.get_lastReturnPageno == "") { self.getData(1);// 初始化 当前页为 1 } else { self.getData(self.get_lastReturnPageno); ///加载接收页码的数据 } //self.getData(self.pageno); }); } self.loadData(); ////保存该用户设置的分页页码数量 self.changePageOption = function () { var data = { userid: "admin", gridid: window.location.pathname, "value": self.itemsPerPage }; $http.get("json/savePageNo.txt", data, postCfg) .success(function (res) { if (res.success) { console.log(res.message); } else { console.log(res.message); } }); }; //数据表格的控制器 end ///判断是否为闰年 function isLeapYear(year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } //判断每个月的天数 function getMonthDays(year, month) { return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28); } //格式化 function dateFtt(fmt, date) { var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (o[k])); } return fmt; } //判断所选日期是第几周 传入时间对象 function getWeekNumber(obj) { var year = obj.getFullYear(); //年 var month = obj.getMonth(); //月 var days = obj.getDate(); //日 var nowTime = obj.getTime(); //毫秒 var day = obj.getDay(); //星期几 //那一天是那一年中的第多少天 for (var i = 0; i < month; i++) { days += getMonthDays(year, i); } //那一年第一天是星期几 var yearFirstDay = new Date(year, 0, 1).getDay() || 7; var week = null; if (yearFirstDay == 1) { week = Math.ceil(days / 7); } else { days -= (7 - yearFirstDay + 1); week = Math.ceil(days / 7) + 1; } var oneDayLong = 24 * 60 * 60 * 1000; if (day == 0) { var MondayTime = nowTime - 6 * oneDayLong; var SundayTime = nowTime; } else { var MondayTime = nowTime - (day - 1) * oneDayLong; var SundayTime = nowTime + (7 - day) * oneDayLong; } var dateString=""; for (var i = 0;i<7;i++){ var time = MondayTime + (oneDayLong) * i; dateString += dateFtt("MM.dd", new Date(time)) + "," } var monday = dateFtt("yyyy-MM-dd", new Date(MondayTime)); var sunday = dateFtt("yyyy-MM-dd", new Date(SundayTime)); return { begin: monday, end: sunday, week: week, dateString: dateString.substring(0, dateString.length - 1), weekday: ["周日","周一", "周二", "周三", "周四", "周五", "周六"][day] } } //调用方式getWeekNumber(new Date()) self.getSelectedWeekInfo = function (strid) { self.duty.year = self.rows[strid].tyear self.duty.weekNum = self.rows[strid].tweek; self.getWeekInfo(); }; ////声明添加模块的变量 self.add = { year: sp.currentYear(), week: getStart.week, title: "", ctype:"日常" }; ////值班安排提交按钮 self.btnAddDuty = function () { var data = { tyear: self.add.year, tweek: self.add.week, title: self.add.year + "年第" + self.add.week + "周值班安排", ctype: self.add.ctype, depart:"办公室", person: "admin", tdate: sp.format(sp.getLocalDate()) }; console.log(data); $http.get("json/message.txt", data, postCfg) .success(function (res) { if (res.success) { sp.layerhide(); sp.dialog("你好,添加成功!"); self.rows.unshift(data); ///json往数组前头添加元素 setTimeout(function () { sp.dialoghide(); }, 1000); } else { sp.dialog("您好,新建失败!"); } }); } }]);