base.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { send } from '../../../monitor'
  2. export default {
  3. props: {
  4. device: {
  5. type: Object,
  6. required: true
  7. }
  8. },
  9. data () {
  10. return {
  11. topic: '/function/invoke'
  12. }
  13. },
  14. created () {
  15. this.$asyncTimer = -1
  16. },
  17. beforeDestroy () {
  18. this.closeAsyncLoading()
  19. },
  20. methods: {
  21. sendTopic (invoke, inputs, aysncFailMessage, delay = 5000) {
  22. console.log('invoke', invoke, inputs)
  23. this.closeAsyncLoading()
  24. const timestamp = `${Date.now()}`
  25. const messageId = `${timestamp}`
  26. return send(
  27. this.topic,
  28. {
  29. messageId,
  30. timestamp,
  31. 'function': invoke,
  32. inputs: inputs || []
  33. }
  34. ).then(
  35. () => {
  36. if (aysncFailMessage) {
  37. this.$messageId = messageId
  38. if (aysncFailMessage === true) {
  39. return
  40. }
  41. this.$asyncloadingDialog = this.$showLoading()
  42. this.$asyncTimer = setTimeout(() => {
  43. this.closeAsyncLoading()
  44. this.$message({
  45. type: 'warning',
  46. message: aysncFailMessage
  47. })
  48. }, delay)
  49. } else {
  50. this.$message({
  51. type: 'success',
  52. message: '执行成功'
  53. })
  54. }
  55. },
  56. () => {
  57. this.$message({
  58. type: 'warning',
  59. message: '正在连接,请稍后再试'
  60. })
  61. return Promise.reject()
  62. }
  63. )
  64. },
  65. closeAsyncLoading () {
  66. this.$messageId = null
  67. if (this.$asyncloadingDialog) {
  68. clearTimeout(this.$asyncTimer)
  69. this.$closeLoading(this.$asyncloadingDialog)
  70. this.$asyncloadingDialog = null
  71. }
  72. }
  73. }
  74. }