index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * 判断数据是否为 type, 或返回 type
  3. * @param {*} data
  4. * @param {*} type
  5. * @returns
  6. */
  7. function isType(data, type = undefined) {
  8. const dataType = Object.prototype.toString
  9. .call(data)
  10. .match(/\s(.+)]/)[1]
  11. .toLowerCase()
  12. return type ? dataType === type.toLowerCase() : dataType
  13. }
  14. /**
  15. autoList 参考示例
  16. [
  17. {
  18. time: 1000 * 5, // 循环调用时间
  19. fn: this.currentTime, // 要调用的函数
  20. }
  21. ]
  22. **/
  23. function autoVueFn(autoList = [], opt) {
  24. if (!opt) {
  25. opt = {
  26. vm: this,
  27. }
  28. } else if (opt.$attrs) {
  29. opt = {
  30. vm: opt,
  31. }
  32. }
  33. const { vm, batEnd } = opt
  34. let end = 0
  35. vm.timer_ = []
  36. autoList.forEach(async (item) => {
  37. const fn = async () => {
  38. await item.fn.call(vm)
  39. end = end + 1
  40. if (end >= autoList.length) {
  41. batEnd && batEnd.call(vm)
  42. }
  43. }
  44. fn()
  45. const timer = setInterval(fn, item.time)
  46. vm.timer_.push(timer)
  47. })
  48. vm.$options.beforeDestroy = [
  49. ...(vm.$options.beforeDestroy || []),
  50. function () {
  51. vm.timer_.forEach((item) => {
  52. console.log(`卸载`, item)
  53. clearInterval(item)
  54. })
  55. },
  56. ]
  57. }
  58. /**
  59. * 根据区域 code 获取区域 id
  60. * @param {} vm
  61. * @param {*} code
  62. * @returns
  63. */
  64. async function code2base(vm, code) {
  65. const list = (await vm.$http.get(`/api/base`)) || []
  66. console.log(list, code.slice(0, 2))
  67. const item =
  68. list.find((item) => String(item.名称).includes(String(code.slice(0, 2)))) ||
  69. list[0] ||
  70. {}
  71. return item
  72. }
  73. export default {
  74. code2base,
  75. autoVueFn,
  76. isType,
  77. }