Selaa lähdekoodia

优化

master
liuminjian 4 vuotta sitten
vanhempi
commit
44e1839c5d
3 muutettua tiedostoa jossa 191 lisäystä ja 255 poistoa
  1. +14
    -1
      index.html
  2. +167
    -252
      static/js/common/tools.js
  3. +10
    -2
      static/js/project/index.js

+ 14
- 1
index.html Näytä tiedosto

@@ -11,8 +11,21 @@
</head>

<body>
<div class="main">
<script id="nav-mian-text" type="text/html">
{{if isAdmin}}
<h1>{{title}}</h1>
<ul>
{{each list as value i}}
<li>索引 {{i + 1}} :{{value}}</li>
{{/each}}
</ul>
{{/if}}
</script>
<div id="nav-mian">


</div>
</div>

</body>
<script src="static/js/common/require.js" data-main="static/js/common/main" id="require-page" current-page="index"


+ 167
- 252
static/js/common/tools.js Näytä tiedosto

@@ -1,288 +1,203 @@
define(['jquery','dialog'],function($,dialog){
define(['jquery', 'dialog'], function ($, dialog) {
// 工具类
function Tool (){
function Tool() {
this.version = "1.0.0";
this.description = "这是一个工具类";
}
$.extend(Tool.prototype,{
$.extend(Tool.prototype, {
/**
* ajax post
* @param url (String)
* @param data (Json) 需要提交的数据
* @param cb(Function) 回调函数
*/
doPost : function(url,data,cb){
doPost: function (url, data, cb) {
var _this = this;
$.ajax({
url:url+'?='+Math.random(),
type:'POST',
data:data,
beforeSend: function(xhr) {
xhr.setRequestHeader("from", "weixin");
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token",_this.getCookie('tokeId'));
xhr.setRequestHeader("version",'1.1.6');
},
success:function(data){
url: url + '?=' + Math.random(),
type: 'POST',
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
xhr.setRequestHeader("version", '1.1.6');
},
success: function (data) {
cb(data);
}
});
},
doGetJsonp:function(url,data,cb){
doGetJsonp: function (url, data, cb) {
var _this = this;
$.ajax({
url:url,
dataType:"jsonp",
data:data,
beforeSend: function(xhr) {
xhr.setRequestHeader("from", "weixin");
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token",_this.getCookie('tokeId'));
xhr.setRequestHeader("version",'1.1.6');
},
jsonp:"jsonpcallback",
success:function(data){
url: url,
dataType: "jsonp",
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader("from", "weixin");
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
xhr.setRequestHeader("version", '1.1.6');
},
jsonp: "jsonpcallback",
success: function (data) {
cb(data);
}
});
},

/**
* ajax get
* @param url(String)
* @param data(Json) 需要提交的数据
* @param cb(Function) 回调函数
*/
doGet : function(uri,data,cb){
var _this = this;
var url = uri + '?';
if(data){
url += $.map(data,function(n,i){
if(i == data.length-1){
return i+'='+n;
}else{
return i+'='+n+'&';
}
}).join('');
}
$.ajax({
url:url,
type:'GET',
beforeSend: function(xhr) {
xhr.setRequestHeader("from", "weixin");
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token",_this.getCookie('tokeId'));
xhr.setRequestHeader("version",'1.1.6');
},
success:function(data){
cb(data);
}
});
},
/**
* 页面跳转util
* @param uri (String)
* @param params (Json)
* ajax get
* @param url(String)
* @param data(Json) 需要提交的数据
* @param cb(Function) 回调函数
*/
skip : function(uri,params){
var url = uri + '?',
time = 0.3;

if(params){
url += $.map(params,function(n,i){
return n;
}).join('');
}
if(uri){
$('html').addClass('skipurl').css({'transition':'all '+time+'s ease-in-out','-webkit-transition':'all '+time+'s ease-in-out','-moz-transition':'all '+time+'s ease-in-out','-o-transition':'all '+time+'s ease-in-out'})
var oTime = time*1000;

setTimeout(function(){
window.location.href = resourceURL+url;
},oTime)
}
},
cpzcSkip : function(uri,params){
var url = uri,
time = 0.3;
if(params){
url += $.map(params,function(n,i){
return n;
doGet: function (uri, data, cb) {
var _this = this;
var url = uri + '?';
if (data) {
url += $.map(data, function (n, i) {
if (i == data.length - 1) {
return i + '=' + n;
} else {
return i + '=' + n + '&';
}
}).join('');
}
if(uri){
$('html').addClass('skipurl').css({'transition':'all '+time+'s ease-in-out','-webkit-transition':'all '+time+'s ease-in-out','-moz-transition':'all '+time+'s ease-in-out','-o-transition':'all '+time+'s ease-in-out'})
var oTime = time*1000;

setTimeout(function(){
window.location.href = resourceURL+url;
},oTime)
$.ajax({
url: url,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader("from", "weixin");
xhr.setRequestHeader("timestamp", getNowFormatDate());
xhr.setRequestHeader("x-auth-token", _this.getCookie('tokeId'));
xhr.setRequestHeader("version", '1.1.6');
},
success: function (data) {
cb(data);
}
});
},
/**
* 创建dialog
* @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
* @param title (String) dialog的title
* @param content(String || HTMLElement) dialog的内容
* @param ok (Function) 点击确定按钮的回调函数
* @param cancel(Function) 点击取消按钮的回调函数
* 可以通过showModal来实现modal显示
*/
initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) {
var initDialog = dialog({
id: id ? id : '',
title: title,
content: content,
okValue: okValue ? okValue : '确定',
ok: ok,
cancelValue: cancelValue ? cancelValue : '取消',
cancel: cancel,
padding: 40,
top: '150px'
});
initDialog.show();
},
/**
* 创建error dialog
* @param errContent
*/
initError: function (errContent) {
var error = dialog({
content: errContent,
padding: 20
});
error.show();
var time = setTimeout(function () {
error.remove();
}, 1500);
},
/**
* 创建dialog tips
* @param tips
* @param align
*/
initTips: function (tips, align, $target, times) {
var tips = dialog({
content: tips,
padding: 20,
skin: 'min-dialog tips',
quickClose: true,
align: align
});
tips.show($target);
if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) {
times = 2000
} else {
times = times
}
},
/**
* 创建dialog
* @param id(String) 创建的dialog的Id 用于分辨唯一的dialog
* @param title (String) dialog的title
* @param content(String || HTMLElement) dialog的内容
* @param ok (Function) 点击确定按钮的回调函数
* @param cancel(Function) 点击取消按钮的回调函数
* 可以通过showModal来实现modal显示
*/
initDialog : function(title,content,ok,okValue,cancel,cancelValue,id){
var initDialog = dialog({
id:id?id:'',
title:title,
content:content,
okValue:okValue?okValue:'确定',
ok:ok,
cancelValue:cancelValue?cancelValue:'取消',
cancel:cancel,
padding:40,
top:'150px'
});
return initDialog;
},
/**
* 创建error dialog
* @param errContent
*/
initError : function(errContent){
var error = dialog({
content: errContent,
padding:20
});
error.show();
var time = setTimeout(function () {
error.remove();
}, 1500);
},
/**
* 创建dialog tips
* @param tips
* @param align
*/
initTips : function(tips,align,$target,times){
var tips = dialog({
content:tips,
padding:20,
skin:'min-dialog tips',
quickClose : true,
align:align
});
tips.show($target);
if(times == undefined || times =='undefined' || times =='' || times=='null' || times ==null){
times=2000
}else{
times = times
}
setTimeout(function () {
tips.close().remove();
setTimeout(function () {
tips.close().remove();
}, times);
},
setCookie : function(cname, cvalue, seconds){
var d = new Date();
d.setTime(d.getTime()+(seconds*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires+ ";path=/";
},
getCookie : function(cname){
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
},
removeCookie : function(cname){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = cname + "=; expires=" + exp.toGMTString();
},
getParam : function(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return (r[2]);
return null;
},
/*判断对象是否有内容*/
isEmptyObject: function(obj) {
for (var key in obj) {
return false;
}
return true;
},
/*添加url跳转特效*/
urlSkip: function(className,url){
var othis = $(className),
time = 0.3;
othis.on('click',function(){
//$('html').addClass('skipurl').css({'transition':'all '+time+'s ease-in-out','-webkit-transition':'all '+time+'s ease-in-out','-moz-transition':'all '+time+'s ease-in-out','-o-transition':'all '+time+'s ease-in-out'})
//var oTime = time*1000;
//setTimeout(function(){
window.location.href = resourceURL+url;
//},oTime)
})
/**
* setCookie 设置cookie
* @param cname 名称
* @param cvalue 值
* @param seconds 有效期
*/
setCookie: function (cname, cvalue, seconds) {
var d = new Date();
d.setTime(d.getTime() + (seconds * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/";
},
/**
* getCookie 获取cookie
* @param cname 名称
*/
getCookie: function (cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
},
/**
* removeCookie 删除cookie
* @param cname 名称
*/
removeCookie: function (cname) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = cname + "=; expires=" + exp.toGMTString();
},
/**
* getParam 获取url参数
* @param key 当前url链接
*/
getParam: function (key) {
var json = {}, data;
$.each(location.search.substr(1).split("&"), function (i, n) {
data = n.split("=");
json[data[0]] = data[1];
});
return key != undefined ? json[key] : json;
},
accordSkip: function(url){
time = 0.3;
$('html').addClass('skipurl').css({'transition':'all '+time+'s ease-in-out','-webkit-transition':'all '+time+'s ease-in-out','-moz-transition':'all '+time+'s ease-in-out','-o-transition':'all '+time+'s ease-in-out'})
var oTime = time*1000;
setTimeout(function(){
window.location.href = url;
},oTime)
/**
* getParam 判断对象是否有内容
* @param obj 传入对象
*/
isEmptyObject: function (obj) {
for (var key in obj) {
return false;
}
return true;
},
strComma:function(num){
var oNum = num.toString()
var strComma = oNum.split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(/\,$/, '').split('').reverse().join('');
return strComma;
}

});
return Tool;
});
//获取url参数
function getParam(key) {
var json = {}, data;
$.each(location.search.substr(1).split("&"), function(i, n) {
data = n.split("=");
json[data[0]] = data[1];
});
return key != undefined ? json[key] : json;
}


/*上传当前时间*/
function getNowFormatDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();

var hours =date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

if (month >= 1 && month <= 9) {month = "0" + month;}
if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}
if (hours >= 0 && hours <= 9) {hours = "0" + hours;}
if (minutes >= 0 && minutes <= 9) {minutes = "0" + minutes;}
if (seconds >= 0 && seconds <= 9) {seconds = "0" + seconds;}
var currentdate = year + month + strDate + hours + minutes +seconds;
return currentdate;
}

/**value:20001212*/
function formatStringyyyyMMddToyyyy_MM_dd (value){
if(value.length == 14 || value.length == 14){
return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8) + " " + value.substring(8, 10) + ":" + value.substring(10, 12) + ':' + value.substring(12, 14);
} else if(value.length == 8){
return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8);
} else if(value.length == 6){
return value.substring(0, 4) + "-" + value.substring(4, 6);
} else {
return value;
}
}

+ 10
- 2
static/js/project/index.js Näytä tiedosto

@@ -2,11 +2,19 @@
* Created by Administrator on 2017/3/8.
*/
define(['jquery', "template", "Tools"], function ($, template, Tools) {
var module = {};
var module = {
initData: {
title: '基本例子',
isAdmin: true,
list: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']
}
};
var tools = new Tools();

module.init = function (page) {
// console.log(page)
var navMian = template('nav-mian-text', module.initData);
$("#nav-mian").html(navMian);

};
module.login = function () { };
return module;

Ladataan…
Peruuta
Tallenna