util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. var Crypto = require('cryptojs.js').Crypto;
  2. var app = getApp();
  3. function formatTime(date) {
  4. var year = date.getFullYear();
  5. var month = date.getMonth() + 1;
  6. var day = date.getDate();
  7. var hour = date.getHours();
  8. var minute = date.getMinutes();
  9. var second = date.getSeconds();
  10. return [year, month, day].map(formatNumber).join('.') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  11. }
  12. function formatDateCHS(date) {
  13. date = date.toString();
  14. var year = date.substr(0, 4);
  15. var month = date.substr(5, 2);
  16. var day = date.substr(8, 2);
  17. return year + "年" + month + "月" + day + "日";
  18. }
  19. function getTimeFormat(duration) {
  20. //console.log("duration:" + duration);
  21. var arr = ['', '', '']
  22. if (duration.indexOf("'") > 0)
  23. arr[0] = duration.substring(0, duration.indexOf("'"));
  24. if (duration.indexOf(".") > 0) {
  25. arr[1] = duration.substring(duration.indexOf("'") + 1, duration.indexOf(".") + 1);
  26. arr[2] = duration.substring(duration.indexOf(".") + 1, duration.indexOf('"'));
  27. }
  28. else {
  29. arr[1] = duration.substring(duration.indexOf("'") + 1, duration.indexOf('"'));
  30. }
  31. return arr;
  32. }
  33. function formatNumber(n) {
  34. n = n.toString()
  35. return n[1] ? n : '0' + n
  36. }
  37. //给字符串左侧补零
  38. function addZero(str, length) {
  39. while (str.length < length) {
  40. str = "0" + str;
  41. }
  42. return str;
  43. }
  44. function getMinuteSecond(second, chs) {
  45. var secondUnit = "\"";
  46. var minuteUnit = "'";
  47. var hourUnit = ":";
  48. if (chs) {
  49. secondUnit = "秒";
  50. minuteUnit = "分钟";
  51. hourUnit = "小时";
  52. second = Math.round(second);
  53. }
  54. if (second < 60)
  55. return second + secondUnit;
  56. else {
  57. var minute = Math.floor(second / 60);
  58. second = Math.round((second - minute * 60) * 1000) / 1000;
  59. if (minute >= 60) {
  60. var hour = Math.floor(minute / 60);
  61. minute = minute - hour * 60;
  62. if (minute == 0 && second == 0)
  63. return hour + hourUnit;
  64. else if (second == 0)
  65. return hour + hourUnit + minute + minuteUnit;
  66. else
  67. return hour + hourUnit + minute + minuteUnit + second + secondUnit;
  68. }
  69. else {
  70. if (second == 0)
  71. return minute + minuteUnit;
  72. else
  73. return minute + minuteUnit + second + secondUnit;
  74. }
  75. }
  76. }
  77. function Random(start, end) {
  78. var result = parseInt(Math.random() * (end - start + 1) + start);
  79. return result;
  80. }
  81. //打乱数组
  82. function RandomArray(arr) {
  83. var arrResult = [];
  84. var maxCount = 0;
  85. do {
  86. var rnd = Random(0, arr.length - 1);
  87. if (arr[rnd]) {
  88. arrResult.push(arr[rnd]);
  89. arr[rnd] = null;
  90. }
  91. maxCount++;
  92. }
  93. while (arrResult.length < arr.length && maxCount < 1000);
  94. return arrResult;
  95. }
  96. function Unique(arr) {
  97. var res = [];
  98. var json = {};
  99. for (var i = 0; i < arr.length; i++) {
  100. if (!json[arr[i]]) {
  101. res.push(arr[i]);
  102. json[arr[i]] = 1;
  103. }
  104. }
  105. return res;
  106. }
  107. function getEnumerationName(id, list) {
  108. for (var i = 0; i < list.length; i++) {
  109. if (id == list[i].EnumID) {
  110. return list[i].Name;
  111. }
  112. }
  113. return "";
  114. }
  115. function getEnumerationNameByDescription(description, list) {
  116. for (var i = 0; i < list.length; i++) {
  117. if (description == list[i].Description) {
  118. return list[i].Name;
  119. }
  120. }
  121. return "";
  122. }
  123. function getStringMaxLength(str, len) {
  124. if (str.length > len)
  125. return str.substr(0, len) + "...";
  126. else
  127. return str;
  128. }
  129. function sort(array, sort_order, obj, objType, obj2, objType2) {
  130. for (var i = 0; i < array.length - 1; i++) {
  131. for (var j = i + 1; j < array.length; j++) {
  132. var check;
  133. if (objType == "Number") {
  134. if (sort_order == "ASC")
  135. check = array[i][obj] < array[j][obj];
  136. else
  137. check = array[i][obj] > array[j][obj];
  138. }
  139. else {
  140. //console.log("array["+i+"]:"+array[i][obj]);
  141. //console.log("array["+j+"]:"+array[j][obj]);
  142. if (array[i][obj] && array[j][obj]) {
  143. try {
  144. if (sort_order == "ASC")
  145. check = array[i][obj].toString().localeCompare(array[j][obj].toString()) >= 0;
  146. else if (sort_order == "DESC")
  147. check = array[i][obj].toString().localeCompare(array[j][obj].toString()) < 0;
  148. }
  149. catch (ex) {
  150. console.log("ex:" + ex);
  151. if (sort_order == "ASC")
  152. check = array[i][obj].toString() >= array[j][obj].toString();
  153. else if (sort_order == "DESC")
  154. check = array[i][obj].toString() < array[j][obj].toString();
  155. }
  156. }
  157. else {
  158. check = false;
  159. }
  160. }
  161. if (check) {
  162. var temp = swap(array[i], array[j]);
  163. array[i] = temp.a;
  164. array[j] = temp.b;
  165. }
  166. }
  167. }
  168. return array;
  169. function swap(a, b) {
  170. var tempA = JSON.stringify(a);
  171. var tempB = JSON.stringify(b);
  172. return {
  173. a: JSON.parse(tempB),
  174. b: JSON.parse(tempA),
  175. }
  176. }
  177. }
  178. ////测试sort
  179. // var a = [
  180. // {
  181. // time: "2017-2-1",
  182. // a: 3,
  183. // }, {
  184. // time: "2017-4-1",
  185. // a: 5,
  186. // }, {
  187. // time: "2017-1-1",
  188. // a: 6
  189. // },{
  190. // time: "2017-2-1",
  191. // a: 3
  192. // },
  193. // ];
  194. // console.log(a);
  195. // // var a = common.sort(a,"ASC", "time","String");
  196. // // console.log(a);
  197. // var a = common.sort(a,"DESC", "time","String");
  198. // console.log(a);
  199. // //var a = common.sort(a,"DESC", "a","Number");
  200. // // console.log(a);
  201. function checkError(err) {
  202. switch (err) {
  203. case 404:
  204. case 500:
  205. case 501:
  206. case 502:
  207. case 503:
  208. case 504:
  209. case 505:
  210. wx.navigateTo({
  211. url: '../About/ErrorPage',
  212. fail: function (e) {
  213. wx.reLaunch({
  214. url: '../index/start',
  215. })
  216. }
  217. });
  218. break;
  219. }
  220. return null;
  221. }
  222. function Encrypt(word) {
  223. var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
  224. var eb = Crypto.charenc.UTF8.stringToBytes(word);
  225. var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
  226. var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
  227. var ub = Crypto.AES.encrypt(eb, kb, { iv: vb, mode: mode, asBpytes: true });
  228. return ub;
  229. }
  230. function Decrypt(word) {
  231. var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
  232. var eb = Crypto.util.base64ToBytes(word);
  233. var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
  234. var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
  235. var ub = Crypto.AES.decrypt(eb, kb, { asBpytes: true, mode: mode, iv: vb });
  236. return ub;
  237. }
  238. function isExistStr(str1, str2) {
  239. var result = false;
  240. if (str1) {
  241. if (str1.toString().indexOf(str2) >= 0) {
  242. result = true;
  243. }
  244. }
  245. return result;
  246. }
  247. function getSystemHeight() {
  248. var systemInfo = wx.getSystemInfoSync();
  249. var height = systemInfo.windowHeight;
  250. if (systemInfo.model) {
  251. if (height == 603 && systemInfo.model.indexOf("Plus") > 0) {
  252. height = 625;
  253. }
  254. else if (height == 504 && (
  255. systemInfo.model.indexOf("iPhone 6<") >= 0
  256. || systemInfo.model.indexOf("iPhone 7<") >= 0
  257. || systemInfo.model.indexOf("iPhone 6s<") >= 0
  258. || systemInfo.model.indexOf("iPhone 5") >= 0
  259. || systemInfo.model.indexOf("iPhone SE") >= 0
  260. )) {
  261. height = 596;
  262. }
  263. }
  264. height = height * 2;
  265. if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) {
  266. height = height + 168;
  267. }
  268. return height;
  269. }
  270. //获取存储数据,若不存在,则获得缺省值。
  271. function getStorageValue(obj, name, defaultStatus, callback) {
  272. wx.getStorage({
  273. key: name,
  274. success: function (res) {
  275. obj.data[name] = res.data;
  276. obj.setData(obj.data);
  277. if (callback)
  278. callback();
  279. },
  280. fail: function (res) {
  281. obj.data[name] = defaultStatus;
  282. obj.setData(obj.data);
  283. if (callback)
  284. callback();
  285. },
  286. });
  287. }
  288. //var arr=getWordArray("cakeface","a e");
  289. //console.log(arr);
  290. function getWordArray(word, key) {
  291. var result = [];
  292. word = word.toLowerCase();
  293. if (key.length == 1) {
  294. var number1 = word.indexOf(key);
  295. result.push({ css: "", name: word.substr(0, number1) });
  296. result.push({ css: "highlight", name: key });
  297. result.push({ css: "", name: word.substr(number1 + 1) });
  298. }
  299. else {
  300. if (key.indexOf(" ") >= 0) {
  301. var arrKey = key.split(" ");
  302. var number2 = word.lastIndexOf(arrKey[1]);
  303. var number1 = word.lastIndexOf(arrKey[0], number2 - 2);
  304. //console.log("number1:" + number1);
  305. //console.log("number2:" + number2);
  306. if (number1 + 2 != number2) {
  307. number1 = word.indexOf(arrKey[0], number1 + 1);
  308. number2 = word.indexOf(arrKey[1], number1 + 1);
  309. }
  310. result.push({ css: "", name: word.substr(0, number1) });
  311. result.push({ css: "highlight", name: arrKey[0] });
  312. result.push({ css: "", name: word.substr(number1 + 1, 1) });
  313. result.push({ css: "highlight", name: arrKey[1] });
  314. result.push({ css: "", name: word.substr(number2 + 1) });
  315. }
  316. else {
  317. var number1 = word.indexOf(key);
  318. var number2 = number1+key.length;
  319. result.push({ css: "", name: word.substr(0, number1) });
  320. result.push({ css: "highlight", name: key});
  321. result.push({ css: "", name: word.substr(number2) });
  322. }
  323. }
  324. return result;
  325. }
  326. //var arrTemp=["test","a","bdt","bca"];
  327. //console.log(sortArrayByStringLength(arrTemp));
  328. //排序数组根据字符串长度
  329. function sortArrayByStringLength(arr){
  330. var result=[],temp=[];
  331. for(var i=0;i<30;i++){
  332. temp.push([]);
  333. }
  334. for (var i = 0; i < arr.length; i++) {
  335. var item=arr[i];
  336. temp[item.length].push(item);
  337. }
  338. for (var i = 0; i < temp.length; i++) {
  339. temp[i].sort();
  340. for (var j = 0; j < temp[i].length;j++){
  341. result.push(temp[i][j]);
  342. }
  343. }
  344. return result;
  345. }
  346. function ReplaceAllString(str, replaceStrFrom, replaceStrTo) {//替换
  347. if (str && str.length > 0) {
  348. var reg = new RegExp(replaceStrFrom, "g");
  349. //console.log(str);
  350. return str.toString().replace(reg, replaceStrTo);
  351. }
  352. else
  353. return str;
  354. }
  355. module.exports = {
  356. formatTime: formatTime,
  357. formatDateCHS: formatDateCHS,
  358. getMinuteSecond: getMinuteSecond,
  359. random: Random,
  360. randomArray: RandomArray,
  361. unique: Unique,
  362. getEnumerationName: getEnumerationName,
  363. getStringMaxLength: getStringMaxLength,
  364. sort: sort,
  365. addZero: addZero,
  366. checkError: checkError,
  367. Encrypt: Encrypt,
  368. Decrypt: Decrypt,
  369. getEnumerationNameByDescription: getEnumerationNameByDescription,
  370. isExistStr: isExistStr,
  371. getSystemHeight: getSystemHeight,
  372. getStorageValue: getStorageValue,
  373. getTimeFormat: getTimeFormat,
  374. getWordArray: getWordArray,
  375. sortArrayByStringLength: sortArrayByStringLength,
  376. ReplaceAllString: ReplaceAllString
  377. }