vab.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import Vue from 'vue'
  2. import { loadingText, messageDuration } from '@/config'
  3. import { Loading, Message, MessageBox, Notification } from 'element-ui'
  4. import { dependencies } from '../../../package.json'
  5. /**
  6. * @description 全局加载层
  7. * @param {number} index 自定义加载图标类名ID
  8. * @param {string} text 显示在加载图标下方的加载文案
  9. */
  10. Vue.prototype.$baseLoading = (index = undefined, text = loadingText) => {
  11. return Loading.service({
  12. lock: true,
  13. text,
  14. spinner: index ? 'vab-loading-type' + index : index,
  15. background: 'hsla(0,0%,100%,.8)',
  16. })
  17. }
  18. /**
  19. * @description 全局多彩加载层
  20. * @param {number} index 自定义加载图标类名ID
  21. * @param {string} text 显示在加载图标下方的加载文案
  22. */
  23. Vue.prototype.$baseColorfullLoading = (
  24. index = undefined,
  25. text = loadingText
  26. ) => {
  27. let loading
  28. if (!index) {
  29. loading = Loading.service({
  30. lock: true,
  31. text,
  32. spinner: 'dots-loader',
  33. background: 'hsla(0,0%,100%,.8)',
  34. })
  35. } else {
  36. const spinnerDict = {
  37. 1: 'dots',
  38. 2: 'gauge',
  39. 3: 'inner-circles',
  40. 4: 'plus',
  41. }
  42. loading = Loading.service({
  43. lock: true,
  44. text,
  45. spinner: spinnerDict[index] + '-loader',
  46. background: 'hsla(0,0%,100%,.8)',
  47. })
  48. }
  49. return loading
  50. }
  51. /**
  52. * @description 全局Message
  53. * @param {string|VNode} message 消息文字
  54. * @param {'success'|'warning'|'info'|'error'} type 主题
  55. * @param {string} customClass 自定义类名
  56. * @param {boolean} dangerouslyUseHTMLString 是否将message属性作为HTML片段处理
  57. */
  58. Vue.prototype.$baseMessage = (
  59. message,
  60. type = 'info',
  61. customClass = undefined,
  62. dangerouslyUseHTMLString = false
  63. ) => {
  64. Message({
  65. message,
  66. type,
  67. customClass,
  68. duration: messageDuration,
  69. dangerouslyUseHTMLString,
  70. showClose: true,
  71. })
  72. }
  73. /**
  74. * @description 全局Alert
  75. * @param {string|VNode} content 消息正文内容
  76. * @param {string} title 标题
  77. * @param {function} callback 若不使用Promise,可以使用此参数指定MessageBox关闭后的回调
  78. */
  79. Vue.prototype.$baseAlert = (
  80. content,
  81. title = '温馨提示',
  82. callback = undefined
  83. ) => {
  84. MessageBox.alert(content, title, {
  85. confirmButtonText: '确定',
  86. dangerouslyUseHTMLString: true,
  87. callback: () => {
  88. if (callback) {
  89. callback()
  90. }
  91. },
  92. }).then(() => {})
  93. }
  94. /**
  95. * @description 全局Confirm
  96. * @param {string|VNode} content 消息正文内容
  97. * @param {string} title 标题
  98. * @param {function} callback1 确认回调
  99. * @param {function} callback2 关闭或取消回调
  100. * @param {string} confirmButtonText 确定按钮的文本内容
  101. * @param {string} cancelButtonText 取消按钮的自定义类名
  102. */
  103. Vue.prototype.$baseConfirm = (
  104. content,
  105. title = undefined,
  106. callback1 = undefined,
  107. callback2 = undefined,
  108. confirmButtonText = '确定',
  109. cancelButtonText = '取消'
  110. ) => {
  111. MessageBox.confirm(content, title || '温馨提示', {
  112. confirmButtonText,
  113. cancelButtonText,
  114. closeOnClickModal: false,
  115. type: 'warning',
  116. lockScroll: false,
  117. })
  118. .then(() => {
  119. if (callback1) {
  120. callback1()
  121. }
  122. })
  123. .catch(() => {
  124. if (callback2) {
  125. callback2()
  126. }
  127. })
  128. }
  129. /**
  130. * @description 全局Notification
  131. * @param {string} message 说明文字
  132. * @param {string|VNode} title 标题
  133. * @param {'success'|'warning'|'info'|'error'} type 主题样式,如果不在可选值内将被忽略
  134. * @param {'top-right'|'top-left'|'bottom-right'|'bottom-left'} position 自定义弹出位置
  135. * @param duration 显示时间,毫秒
  136. */
  137. Vue.prototype.$baseNotify = (
  138. message,
  139. title,
  140. type = 'success',
  141. position = 'top-right',
  142. duration = messageDuration
  143. ) => {
  144. Notification({
  145. title,
  146. message,
  147. type,
  148. duration,
  149. position,
  150. })
  151. }
  152. /**
  153. * @description 表格高度
  154. * @param {*} formType
  155. */
  156. Vue.prototype.$baseTableHeight = (formType) => {
  157. let height = window.innerHeight
  158. const paddingHeight = 245
  159. const formHeight = 50
  160. if ('number' === typeof formType) {
  161. height = height - paddingHeight - formHeight * formType
  162. } else {
  163. height = height - paddingHeight
  164. }
  165. return height
  166. }
  167. Vue.prototype.$noPagingTableHeight = (formType) => {
  168. let height = window.innerHeight
  169. const paddingHeight = 240
  170. const formHeight = 50
  171. if ('number' === typeof formType) {
  172. height = height - paddingHeight - formHeight * formType
  173. } else {
  174. height = height - paddingHeight
  175. }
  176. return height
  177. }
  178. /**
  179. * @description 全局事件总线
  180. */
  181. Vue.prototype.$baseEventBus = new Vue()
  182. !(() => {
  183. if (process.env.NODE_ENV !== 'development') {
  184. const str = '\u0076\u0061\u0062\u002d\u0069\u0063\u006f\u006e\u0073'
  185. const key = unescape(str.replace(/\\u/g, '%u'))
  186. if (!dependencies[key]) Vue.prototype = null
  187. if (!process.env.VUE_APP_SECRET_KEY) Vue.prototype = null
  188. }
  189. })()