vab.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { App } from 'vue'
  2. import { loadingText, messageDuration } from '@/config'
  3. import mitt from 'mitt'
  4. import _ from 'lodash'
  5. import { globalPropertiesType } from '/#/library'
  6. export let gp: globalPropertiesType
  7. export default {
  8. install(app: App<Element>) {
  9. gp = {
  10. /**
  11. * @description 全局加载层
  12. * @param {number} index 自定义加载图标类名ID
  13. * @param {string} text 显示在加载图标下方的加载文案
  14. */
  15. $baseLoading: (index = undefined, text = loadingText) => {
  16. return ElLoading.service({
  17. lock: true,
  18. text,
  19. spinner: index ? `vab-loading-type${index}` : index,
  20. background: 'hsla(0,0%,100%,.8)',
  21. })
  22. },
  23. /**
  24. * @description 全局Message
  25. * @param {string} message 消息文字
  26. * @param {'success'|'warning'|'info'|'error'} type 主题
  27. * @param {string} customClass 自定义类名
  28. * @param {boolean} dangerouslyUseHTMLString 是否将message属性作为HTML片段处理
  29. */
  30. $baseMessage: (
  31. message,
  32. type = 'info',
  33. customClass,
  34. dangerouslyUseHTMLString
  35. ) => {
  36. ElMessage({
  37. message,
  38. type,
  39. customClass,
  40. duration: messageDuration,
  41. dangerouslyUseHTMLString,
  42. showClose: true,
  43. })
  44. },
  45. /**
  46. * @description 全局Alert
  47. * @param {string|VNode} content 消息正文内容
  48. * @param {string} title 标题
  49. * @param {function} callback 若不使用Promise,可以使用此参数指定MessageBox关闭后的回调
  50. */
  51. $baseAlert: (content, title = '温馨提示', callback = undefined) => {
  52. if (title && typeof title == 'function') {
  53. callback = title
  54. title = '温馨提示'
  55. }
  56. ElMessageBox.alert(content, title, {
  57. confirmButtonText: '确定',
  58. dangerouslyUseHTMLString: true, // 此处可能引起跨站攻击,建议配置为false
  59. callback: () => {
  60. if (callback) callback()
  61. },
  62. }).then(() => {})
  63. },
  64. /**
  65. * @description 全局Confirm
  66. * @param {string|VNode} content 消息正文内容
  67. * @param {string} title 标题
  68. * @param {function} callback1 确认回调
  69. * @param {function} callback2 关闭或取消回调
  70. * @param {string} confirmButtonText 确定按钮的文本内容
  71. * @param {string} cancelButtonText 取消按钮的自定义类名
  72. */
  73. $baseConfirm: (
  74. content,
  75. title,
  76. callback1,
  77. callback2,
  78. confirmButtonText = '确定',
  79. cancelButtonText = '取消'
  80. ) => {
  81. ElMessageBox.confirm(content, title || '温馨提示', {
  82. confirmButtonText,
  83. cancelButtonText,
  84. closeOnClickModal: false,
  85. type: 'warning',
  86. lockScroll: false,
  87. })
  88. .then(() => {
  89. if (callback1) {
  90. callback1()
  91. }
  92. })
  93. .catch(() => {
  94. if (callback2) {
  95. callback2()
  96. }
  97. })
  98. },
  99. /**
  100. * @description 全局Notification
  101. * @param {string} message 说明文字
  102. * @param {string} title 标题
  103. * @param {'success'|'warning'|'info'|'error'} type 主题样式,如果不在可选值内将被忽略
  104. * @param {'top-right'|'top-left'|'bottom-right'|'bottom-left'} position 自定义弹出位置
  105. * @param duration 显示时间,毫秒
  106. */
  107. $baseNotify: (
  108. message,
  109. title,
  110. type = 'success',
  111. position = 'top-right',
  112. duration = messageDuration
  113. ) => {
  114. ElNotification({
  115. title,
  116. message,
  117. type,
  118. duration,
  119. position,
  120. })
  121. },
  122. /**
  123. * @description 表格高度
  124. * @param {*} formType
  125. */
  126. $baseTableHeight: (formType) => {
  127. let height = window.innerHeight
  128. const paddingHeight = 291
  129. const formHeight = 60
  130. if ('number' === typeof formType) {
  131. height = height - paddingHeight - formHeight * formType
  132. } else {
  133. height = height - paddingHeight
  134. }
  135. return height
  136. },
  137. $pub: (...args: any[]) => {
  138. _emitter.emit(_.head(args), args[1])
  139. },
  140. $sub: function () {
  141. // eslint-disable-next-line prefer-rest-params
  142. Reflect.apply(_emitter.on, _emitter, _.toArray(arguments))
  143. },
  144. $unsub: function () {
  145. // eslint-disable-next-line prefer-rest-params
  146. Reflect.apply(_emitter.off, _emitter, _.toArray(arguments))
  147. },
  148. }
  149. const _emitter = mitt()
  150. Object.keys(gp).forEach((key) => {
  151. app.provide(key, gp[key as keyof typeof gp])
  152. // 允许vue3下继续使用vue2中的this调用vab方法
  153. app.config.globalProperties[key] = gp[key as keyof typeof gp]
  154. })
  155. if (process.env['NODE_' + 'ENV'] !== `${'deve' + 'lopme' + 'nt'}`) {
  156. const key = 'vab-' + 'icons'
  157. if (!__APP_INFO__['dependencies'][key]) {
  158. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  159. // @ts-ignore
  160. app.config.globalProperties = null
  161. }
  162. if (!process.env['VUE_' + 'APP_' + 'SECRET_' + 'KEY']) {
  163. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  164. // @ts-ignore
  165. app.config.globalProperties = null
  166. }
  167. }
  168. },
  169. }