index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module.exports = {
  2. toBoolean(val) {
  3. if (typeof val === 'boolean') return val
  4. if (val === '') return val
  5. return val === 'true' || val == '1'
  6. },
  7. cookieToJson(cookie) {
  8. if (!cookie) return {}
  9. let cookieArr = cookie.split(';')
  10. let obj = {}
  11. cookieArr.forEach((i) => {
  12. let arr = i.split('=')
  13. obj[arr[0]] = arr[1]
  14. })
  15. return obj
  16. },
  17. cookieObjToString(cookie) {
  18. return Object.keys(cookie)
  19. .map(
  20. (key) =>
  21. `${encodeURIComponent(key)}=${encodeURIComponent(cookie[key])}`,
  22. )
  23. .join('; ')
  24. },
  25. getRandom(num) {
  26. var random = Math.floor(
  27. (Math.random() + Math.floor(Math.random() * 9 + 1)) *
  28. Math.pow(10, num - 1),
  29. )
  30. return random
  31. },
  32. generateRandomChineseIP() {
  33. const chinaIPPrefixes = ['116.25', '116.76', '116.77', '116.78']
  34. const randomPrefix =
  35. chinaIPPrefixes[Math.floor(Math.random() * chinaIPPrefixes.length)]
  36. return `${randomPrefix}.${generateIPSegment()}.${generateIPSegment()}`
  37. },
  38. }
  39. // 生成一个随机整数
  40. function getRandomInt(min, max) {
  41. return Math.floor(Math.random() * (max - min + 1)) + min
  42. }
  43. // 生成一个随机IP地址段
  44. function generateIPSegment() {
  45. return getRandomInt(1, 255)
  46. }