时空网前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. /*
  2. 全局共享实用方法 shared.js
  3. */
  4. let shareMD5 = require("./shareMD5.js"); // 签名加密js文件
  5. // 判断对错/是否显示,万能校验
  6. export function isRight(obj) {
  7. if (isValueType(obj) === 'string') {
  8. obj = obj.trim();
  9. if (obj === 'null' || obj === 'undefined') {
  10. return false;
  11. }
  12. } else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
  13. return true;
  14. } else if (isValueType(obj) === 'boolean') {
  15. return obj
  16. }
  17. for (var key in obj) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. // 判断一个值所属的类型,返回一个字符串
  23. export function isValueType(value) {
  24. let str = Object.prototype.toString.call(value);
  25. return str.match(/\[object (.*?)\]/)[1].toLowerCase();
  26. }
  27. // 生成随机唯一token
  28. export function createToken(par = {}){
  29. const date = new Date(); // 获取时间对象
  30. let fullYear = date.getFullYear(); // 年
  31. let month = String(date.getMonth() + 1); // 月
  32. month = fillZero(month);
  33. let getdate = String(date.getDate()); // 日
  34. getdate = fillZero(getdate);
  35. let hours = String(date.getHours()); // 时
  36. hours = fillZero(hours);
  37. let minutes = String(date.getMinutes()); // 分
  38. minutes = fillZero(minutes);
  39. let seconds = String(date.getSeconds()); // 秒
  40. seconds = fillZero(seconds);
  41. let randomStr = Math.random().toString(32).substr(2); // 随机字符串
  42. let tmp = `${fullYear}/${month}/${getdate} ${hours}:${minutes}:${seconds} ${randomStr}`;
  43. for(let i in par){
  44. tmp += par[i];
  45. }
  46. let token = shareMD5.md5(tmp, 16);
  47. return token;
  48. }
  49. //不足2位补0
  50. export function fillZero(time) {
  51. if (time <= 9) {
  52. time = '0' + time;
  53. }
  54. return time;
  55. }