何为操作库
在编写小程序的项目中。或者是其他终端的项目。我们都会抽取一些多页面会公用的代码块。我把这一块称为操作库。在小程序中我封装了基于用户反馈。验证,时间,数据关系的操作库。现开源如下:
js代码如下
//因为小程序中不支持使用alert弹框形式。所以这里借助
//小程序的的响应api进行操作的反馈
let messageBox = {};
//成功或者加载的弹窗
var showToast = function(sucText, iconText, duration, suc) {
if (typeof ( sucText ) !== "string") {
sucText = "成功"; //模态框文本
iconText = "success"; //默认成功
duration = 2000 //默认2s
}
wx.showToast({
title: sucText, //成功文本
icon: iconText, // "success", "loading", "none"
duration: duration, //持续时间
success: function() {
suc();
},
fail: function() {
console.log("此接口调用失败");
}
})
};
// comfirm框 设置用户操作成功的反馈
var showModal = function(content) {
if (!content) {
content = "未设置弹窗内容";
}
wx.showModal({
content: content,
showCancel: false,
confirmColor: "#0076ff", //颜色属于全局自定义
cancelColor: "#0076ff", //可以根据自己的需求定义颜色
success: function(res) {
if (res.confirm) {
}
},
fail: function() {
}
});
};
var popWin = function(sucText, suc, fail) {
if (! fail ) {
fail= function() {
//自定义错误回调,如果没有传递就用默认的错误回调
//一般在开发过程中可以不传递
};
};
wx.showModal({
content: sucText, //提示文本
confirmColor: "#0076ff",
cancelColor: "#0076ff",
success: function(res) {
if (res.confirm) {
//用户确认
sucfn();
} else {
failfn(res);
}
},
fail: function(err) {
console.log(err);
//弹窗提示不支持
}
});
};
//messageBox是消息提示框 下列三个分别对应着
//alert 含有确定和取消的 弹窗 以及需要确定或者失败具有回调
//性质的弹窗。
messageBox.showToast = showToast;
messageBox.showModal = showModal;
messageBox.popWin = popWin; //弹窗
//时间格式化api
let dateApi = {};
dateApi.list = {};
//格式化时间为 :某年某月某日...
dateApi.list.getTime = function(now) {
//格式化日期时间
function FormatDateTime(now) {
return FormatDate(now) + " " + FormatTime(now);
}
//格式化日期
function FormatDate(now) {
var year = now.getYear() + 1900;
var month = now.getMonth() + 1;
var day = now.getDate();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
return year + "-" + month + "-" + day;
}
//格式化时间
function FormatTime(now) {
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return hour + ":" + minute + ":" + second;
}
return FormatDateTime(now)
}
/* 生成查询时间 */
dateApi.list.createQueryTime = function(days) {
//接受数字类型的设置 查询时间 start --- end
var days = days == undefined ? 30 : days;
var endTime = dateApi.list.getTime(new Date()).substring(0, 10); //开始时间
var endTimeStmp = new Date().getTime() - days * 24 * 60 * 60 * 1000; //结束时间戳
var startTime = dateApi.list.getTime(new Date(endTimeStmp)).substring(0, 10); //结束时间
return {
start: startTime, //开始时间
end: endTime //结束时间
}
};
//用时间戳+用户+随机数生成订单号 这里做了防止重复的处理
dateApi.list.createTimeTemp = function(sorder) {
var num = "";
function sixNum() {
for (var i = 0; i < 6; i++) {
num += Math.floor(Math.random() * 10);
}
return num;
}
if (sorder == "") {
sorder = new Date().getTime() + sixNum() + wx.getStorageSync("userId");
}
return sorder;
};
/*
时间查询字符串是否正确 。
主要用于小程序某页面调用时间段查询响应的数据
*/
dataApi.list.timeChangeT = function(start,end) {
var callback = false;
//通过时间戳来判定是否正确
var callback = new Date(start).getTime() <= new Date(end).getTime();
return callback;
}
module.exports.messageBox = messageBox; //弹出框
module.exports.dateApi = dateApi; //时间转化api
写在最后
- 小程序比pc或者是移动端开发相对来说都比较容易。但是为了代码的可维护性我们还是需要封装一些公用的方法。这样会在实际的开发过程中事半功倍。因为限于偏长。这里不在放出完整代码。万变不离其宗.