util.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 formatNumber(n) {
  13. n = n.toString()
  14. return n[1] ? n : '0' + n
  15. }
  16. //给字符串左侧补零
  17. function addZero(str, length) {
  18. while (str.length < length) {
  19. str = "0" + str;
  20. }
  21. return str;
  22. }
  23. function getMinuteSecond(second, chs) {
  24. if (!second)
  25. second = 0;
  26. var secondUnit = "\"";
  27. var minuteUnit = "'";
  28. var hourUnit = ":";
  29. if (chs) {
  30. secondUnit = "秒";
  31. minuteUnit = "分";
  32. hourUnit = "时";
  33. //second = Math.round(second);
  34. }
  35. if (second < 60)
  36. return second + secondUnit;
  37. else {
  38. var minute = Math.floor(second / 60);
  39. second = Math.round((second - minute * 60) * 1000) / 1000;
  40. if (minute >= 60) {
  41. var hour = Math.floor(minute / 60);
  42. minute = minute - hour * 60;
  43. if (minute == 0 && second == 0)
  44. return hour + hourUnit;
  45. else if (second == 0)
  46. return hour + hourUnit + minute + minuteUnit;
  47. else
  48. return hour + hourUnit + minute + minuteUnit + second + secondUnit;
  49. }
  50. else {
  51. if (second == 0)
  52. return minute + minuteUnit;
  53. else
  54. return minute + minuteUnit + second + secondUnit;
  55. }
  56. }
  57. }
  58. function Random(start, end) {
  59. var result = parseInt(Math.random() * (end - start + 1) + start);
  60. return result;
  61. }
  62. //打乱数组
  63. function RandomArray(arr) {
  64. var arrResult = [];
  65. var maxCount = 0;
  66. do {
  67. var rnd = Random(0, arr.length - 1);
  68. if (arr[rnd]) {
  69. arrResult.push(arr[rnd]);
  70. arr[rnd] = null;
  71. }
  72. maxCount++;
  73. }
  74. while (arrResult.length < arr.length && maxCount < 1000);
  75. return arrResult;
  76. }
  77. function Unique(arr) {
  78. var res = [];
  79. var json = {};
  80. for (var i = 0; i < arr.length; i++) {
  81. if (!json[arr[i]]) {
  82. res.push(arr[i]);
  83. json[arr[i]] = 1;
  84. }
  85. }
  86. return res;
  87. }
  88. function getEnumerationName(id, list) {
  89. for (var i = 0; i < list.length; i++) {
  90. if (id == list[i].EnumID) {
  91. return list[i].Name;
  92. }
  93. }
  94. return "";
  95. }
  96. function getEnumerationNameByDescription(description, list) {
  97. for (var i = 0; i < list.length; i++) {
  98. if (description == list[i].Description) {
  99. return list[i].Name;
  100. }
  101. }
  102. return "";
  103. }
  104. function getStringMaxLength(str, len) {
  105. if (str.length > len)
  106. return str.substr(0, len) + "...";
  107. else
  108. return str;
  109. }
  110. function sort(array, sort_order, obj, objType, obj2, objType2) {
  111. for (var i = 0; i < array.length - 1; i++) {
  112. for (var j = i + 1; j < array.length; j++) {
  113. var check;
  114. if (objType == "Number") {
  115. if (sort_order == "ASC")
  116. check = array[i][obj] > array[j][obj];
  117. else
  118. check = array[i][obj] < array[j][obj];
  119. }
  120. else {
  121. //console.log("array["+i+"]:"+array[i][obj]);
  122. //console.log("array["+j+"]:"+array[j][obj]);
  123. if (array[i][obj] && array[j][obj]) {
  124. try {
  125. if (sort_order == "ASC")
  126. check = array[i][obj].toString().localeCompare(array[j][obj].toString()) >= 0;
  127. else if (sort_order == "DESC")
  128. check = array[i][obj].toString().localeCompare(array[j][obj].toString()) < 0;
  129. }
  130. catch (ex) {
  131. console.log("ex:" + ex);
  132. if (sort_order == "ASC")
  133. check = array[i][obj].toString() >= array[j][obj].toString();
  134. else if (sort_order == "DESC")
  135. check = array[i][obj].toString() < array[j][obj].toString();
  136. }
  137. }
  138. else {
  139. check = false;
  140. }
  141. }
  142. if (check) {
  143. var temp = swap(array[i], array[j]);
  144. array[i] = temp.a;
  145. array[j] = temp.b;
  146. }
  147. }
  148. }
  149. return array;
  150. function swap(a, b) {
  151. var tempA = JSON.stringify(a);
  152. var tempB = JSON.stringify(b);
  153. return {
  154. a: JSON.parse(tempB),
  155. b: JSON.parse(tempA),
  156. }
  157. }
  158. }
  159. ////测试sort
  160. // var a = [
  161. // {
  162. // time: "2017-2-1",
  163. // a: 3,
  164. // }, {
  165. // time: "2017-4-1",
  166. // a: 5,
  167. // }, {
  168. // time: "2017-1-1",
  169. // a: 6
  170. // },{
  171. // time: "2017-2-1",
  172. // a: 3
  173. // },
  174. // ];
  175. // console.log(a);
  176. // // var a = common.sort(a,"ASC", "time","String");
  177. // // console.log(a);
  178. // var a = common.sort(a,"DESC", "time","String");
  179. // console.log(a);
  180. // //var a = common.sort(a,"DESC", "a","Number");
  181. // // console.log(a);
  182. function checkError(err) {
  183. switch (err) {
  184. case 404:
  185. case 500:
  186. case 501:
  187. case 502:
  188. case 503:
  189. case 504:
  190. case 505:
  191. wx.redirectTo({
  192. url: './error',
  193. });
  194. break;
  195. }
  196. return null;
  197. }
  198. function Encrypt(word) {
  199. var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
  200. var eb = Crypto.charenc.UTF8.stringToBytes(word);
  201. var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
  202. var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
  203. var ub = Crypto.AES.encrypt(eb, kb, { iv: vb, mode: mode, asBpytes: true });
  204. return ub;
  205. }
  206. function Decrypt(word) {
  207. var mode = new Crypto.mode.CBC(Crypto.pad.pkcs7);
  208. var eb = Crypto.util.base64ToBytes(word);
  209. var kb = Crypto.charenc.UTF8.stringToBytes(app.globalData.Key);//KEY
  210. var vb = Crypto.charenc.UTF8.stringToBytes(app.globalData.IV);//IV
  211. var ub = Crypto.AES.decrypt(eb, kb, { asBpytes: true, mode: mode, iv: vb });
  212. return ub;
  213. }
  214. function isExistStr(str1, str2) {
  215. var result = false;
  216. if (str1) {
  217. if (str1.toString().indexOf(str2) >= 0) {
  218. result = true;
  219. }
  220. }
  221. return result;
  222. }
  223. function getSystemHeight() {
  224. var systemInfo = wx.getSystemInfoSync();
  225. var height = systemInfo.windowHeight;
  226. if (systemInfo.model) {
  227. if (height == 603 && systemInfo.model.indexOf("Plus") > 0) {
  228. height = 625;
  229. }
  230. else if (height == 504 && (
  231. systemInfo.model.indexOf("iPhone 6<") >= 0
  232. || systemInfo.model.indexOf("iPhone 7<") >= 0
  233. || systemInfo.model.indexOf("iPhone 6s<") >= 0
  234. || systemInfo.model.indexOf("iPhone 5") >= 0
  235. || systemInfo.model.indexOf("iPhone SE") >= 0
  236. )) {
  237. height = 596;
  238. }
  239. }
  240. height = height * 2;
  241. if (systemInfo.system && systemInfo.system.indexOf("Android") >= 0) {
  242. height = height + 168;
  243. }
  244. return height;
  245. }
  246. //获取存储数据,若不存在,则获得缺省值。
  247. function getStorageValue(obj, name, defaultStatus, callback) {
  248. wx.getStorage({
  249. key: name,
  250. success: function (res) {
  251. obj.data[name] = res.data;
  252. obj.setData(obj.data);
  253. if (callback)
  254. callback();
  255. },
  256. fail: function (res) {
  257. obj.data[name] = defaultStatus;
  258. obj.setData(obj.data);
  259. if (callback)
  260. callback();
  261. },
  262. });
  263. }
  264. module.exports = {
  265. formatTime: formatTime,
  266. getMinuteSecond: getMinuteSecond,
  267. random: Random,
  268. randomArray: RandomArray,
  269. unique: Unique,
  270. getEnumerationName: getEnumerationName,
  271. getStringMaxLength: getStringMaxLength,
  272. sort: sort,
  273. addZero: addZero,
  274. checkError: checkError,
  275. Encrypt: Encrypt,
  276. Decrypt: Decrypt,
  277. getEnumerationNameByDescription: getEnumerationNameByDescription,
  278. isExistStr: isExistStr,
  279. getStorageValue: getStorageValue,
  280. getSystemHeight: getSystemHeight,
  281. }