| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- var Crypto = require('cryptojs.js').Crypto;
- var app = getApp();
- function formatTime(date) {
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('.') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- function formatDateCHS(date) {
- date = date.toString();
- var year = date.substr(0, 4);
- var month = date.substr(5, 2);
- var day = date.substr(8, 2);
- return year + "年" + month + "月" + day + "日";
- }
- function formatDateCHSLong(date) {
- var year = date.getFullYear()
- var month = addZero(date.getMonth() + 1, 2);
- var day = addZero(date.getDate(), 2);
- var hour = addZero(date.getHours(), 2);
- var minute = addZero(date.getMinutes(), 2);
- return year + "年" + month + "月" + day + "日 "+hour+":"+minute;
- }
- function formatNumber(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- //给字符串左侧补零
- function addZero(str, length) {
- str=str.toString();
- while (str.length < length) {
- str = "0" + str;
- }
- return str;
- }
- function getMinuteSecond(second, chs) {
- if (!second)
- second = 0;
- var secondUnit = "\"";
- var minuteUnit = "'";
- var hourUnit = ":";
- if (chs) {
- secondUnit = "秒";
- minuteUnit = "分";
- hourUnit = "时";
- //second = Math.round(second);
- }
- if (second < 60)
- return second + secondUnit;
- else {
- var minute = Math.floor(second / 60);
- second = Math.round((second - minute * 60) * 1000) / 1000;
- if (minute >= 60) {
- var hour = Math.floor(minute / 60);
- minute = minute - hour * 60;
- if (minute == 0 && second == 0)
- return hour + hourUnit;
- else if (second == 0)
- return hour + hourUnit + minute + minuteUnit;
- else
- return hour + hourUnit + minute + minuteUnit + second + secondUnit;
- }
- else {
- if (second == 0)
- return minute + minuteUnit;
- else
- return minute + minuteUnit + second + secondUnit;
- }
- }
- }
- function Random(start, end) {
- var result = parseInt(Math.random() * (end - start + 1) + start);
- return result;
- }
- //打乱数组
- function RandomArray(arr) {
- var arrResult = [];
- var maxCount = 0;
- do {
- var rnd = Random(0, arr.length - 1);
- if (arr[rnd]) {
- arrResult.push(arr[rnd]);
- arr[rnd] = null;
- }
- maxCount++;
- }
- while (arrResult.length < arr.length && maxCount < 1000);
- return arrResult;
- }
- function Unique(arr) {
- var res = [];
- var json = {};
- for (var i = 0; i < arr.length; i++) {
- if (!json[arr[i]]) {
- res.push(arr[i]);
- json[arr[i]] = 1;
- }
- }
- return res;
- }
- function getEnumerationName(id, list) {
- for (var i = 0; i < list.length; i++) {
- if (id == list[i].EnumID) {
- return list[i].Name;
- }
- }
- return "";
- }
- function getEnumerationNameByDescription(description, list) {
- for (var i = 0; i < list.length; i++) {
- if (description == list[i].Description) {
- return list[i].Name;
- }
- }
- return "";
- }
- function getStringMaxLength(str, len) {
- if (str.length > len)
- return str.substr(0, len) + "...";
- else
- return str;
- }
- function sort(array, sort_order, obj, objType, obj2, objType2) {
- for (var i = 0; i < array.length - 1; i++) {
- for (var j = i + 1; j < array.length; j++) {
- var check;
- if (objType == "Number") {
- if (sort_order == "ASC")
- check = array[i][obj] > array[j][obj];
- else
- check = array[i][obj] < array[j][obj];
- }
- else {
- //console.log("array["+i+"]:"+array[i][obj]);
- //console.log("array["+j+"]:"+array[j][obj]);
- if (array[i][obj] && array[j][obj]) {
- try {
- if (sort_order == "ASC")
- check = array[i][obj].toString().localeCompare(array[j][obj].toString()) >= 0;
- else if (sort_order == "DESC")
- check = array[i][obj].toString().localeCompare(array[j][obj].toString()) < 0;
- }
- catch (ex) {
- console.log("ex:" + ex);
- if (sort_order == "ASC")
- check = array[i][obj].toString() >= array[j][obj].toString();
- else if (sort_order == "DESC")
- check = array[i][obj].toString() < array[j][obj].toString();
- }
- }
- else {
- check = false;
- }
- }
- if (check) {
- var temp = swap(array[i], array[j]);
- array[i] = temp.a;
- array[j] = temp.b;
- }
- }
- }
- return array;
- function swap(a, b) {
- var tempA = JSON.stringify(a);
- var tempB = JSON.stringify(b);
- return {
- a: JSON.parse(tempB),
- b: JSON.parse(tempA),
- }
- }
- }
- function checkError(err) {
- switch (err) {
- case 404:
- case 500:
- case 501:
- case 502:
- case 503:
- case 504:
- case 505:
- wx.redirectTo({
- url: './error',
- });
- break;
- }
- return null;
- }
- function Encrypt(word) {
- var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
- var eb = Crypto.charenc.UTF8.stringToBytes(word);
- var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
- var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
- var ub = Crypto.AES.encrypt(eb, kb, { iv: vb, mode: mode, asBpytes: true });
- return ub;
- }
- function Decrypt(word) {
- var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
- var eb = Crypto.util.base64ToBytes(word);
- var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
- var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
- var ub = Crypto.AES.decrypt(eb, kb, { asBpytes: true, mode: mode, iv: vb });
- return ub;
- }
- function isExistStr(str1, str2) {
- var result = false;
- if (str1) {
- if (str1.toString().indexOf(str2) >= 0) {
- result = true;
- }
- }
- return result;
- }
- function getSystemHeight() {
- var systemInfo = wx.getSystemInfoSync();
- var height = systemInfo.windowHeight;
- if (systemInfo.model) {
- if (height == 603 && systemInfo.model.indexOf("Plus") > 0) {
- height = 625;
- }
- else if (height == 504 && (
- systemInfo.model.indexOf("iPhone 6<") >= 0
- || systemInfo.model.indexOf("iPhone 7<") >= 0
- || systemInfo.model.indexOf("iPhone 6s<") >= 0
- || systemInfo.model.indexOf("iPhone 5") >= 0
- || systemInfo.model.indexOf("iPhone SE") >= 0
- )) {
- height = 596;
- }
- }
- height = height * 2;
- if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) {
- height = height + 168;
- }
- return height;
- }
- //获取存储数据,若不存在,则获得缺省值。
- function getStorageValue(obj, name, defaultStatus, callback) {
- wx.getStorage({
- key: name,
- success: function (res) {
- obj.data[name] = res.data;
- obj.setData(obj.data);
- if (callback)
- callback();
- },
- fail: function (res) {
- obj.data[name] = defaultStatus;
- obj.setData(obj.data);
- if (callback)
- callback();
- },
- });
- }
- function FormatMoney(money) {
- var result;
- if (money == 0)
- result = "0.00";
- else {
- result = (Math.round(money) / 100).toString();
- //console.log("1:"+result);
- if (result.indexOf(".") >= 0) {
- if (result.substr(result.indexOf(".")).length < 3)
- result += "0";
- }
- else {
- result += ".00";
- }
- }
- return result;
- }
- module.exports = {
- formatTime: formatTime,
- formatDateCHS: formatDateCHS,
- formatDateCHSLong: formatDateCHSLong,
- getMinuteSecond: getMinuteSecond,
- random: Random,
- randomArray: RandomArray,
- unique: Unique,
- getEnumerationName: getEnumerationName,
- getStringMaxLength: getStringMaxLength,
- sort: sort,
- addZero: addZero,
- checkError: checkError,
- Encrypt: Encrypt,
- Decrypt: Decrypt,
- getEnumerationNameByDescription: getEnumerationNameByDescription,
- isExistStr: isExistStr,
- getStorageValue: getStorageValue,
- getSystemHeight: getSystemHeight,
- FormatMoney: FormatMoney,
- }
|