vab.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { ElLoading, ElMessage, ElMessageBox, ElNotification } from 'element-plus'
  2. import { head, toArray } from 'lodash-es'
  3. import mitt from 'mitt'
  4. import type { App, VNode } from 'vue'
  5. import { loadingText, messageDuration } from '/@/config'
  6. export let gp: Record<string, any>
  7. const isCheck = () => {
  8. if (
  9. import.meta.env.MODE !== '\u0064\u0065\u0076\u0065\u006c\u006f\u0070\u006d\u0065\u006e\u0074' &&
  10. import.meta.env['\u0056\u0049\u0054\u0045\u005f\u0041\u0050\u0050\u005f\u0053\u0045\u0043\u0052\u0045\u0054\u005f\u004b\u0045\u0059']
  11. .length < 50
  12. ) {
  13. setInterval(() => {
  14. localStorage.clear()
  15. location.reload()
  16. }, 50)
  17. ;(() => {
  18. function block() {
  19. setInterval(() => {
  20. ;(function () {
  21. return false
  22. })
  23. ['constructor']('debugger')
  24. ['call']()
  25. }, 50)
  26. }
  27. try {
  28. block()
  29. } catch (err) {
  30. console.log(err)
  31. }
  32. })()
  33. return false
  34. } else return true
  35. }
  36. export default {
  37. install: (app: App<Element>) => {
  38. /**
  39. * @description 全局加载层
  40. * @param {string} text 显示在加载图标下方的加载文案
  41. */
  42. const $baseLoading = (text = loadingText, background = 'var(--el-color-white)') => {
  43. return ElLoading.service({
  44. lock: true,
  45. text: text,
  46. background: background,
  47. })
  48. }
  49. /**
  50. * @description 全局Message
  51. * @param {string|VNode} message 消息文字
  52. * @param {'success'|'warning'|'info'|'error'} type 主题
  53. * @param {string} customClass 自定义类名
  54. * @param {boolean} dangerouslyUseHTMLString 是否将message属性作为HTML片段处理
  55. */
  56. const $baseMessage = (
  57. message: string | VNode,
  58. type: 'success' | 'warning' | 'info' | 'error' = 'info',
  59. customClass: string,
  60. dangerouslyUseHTMLString: boolean,
  61. callback: any = undefined
  62. ) => {
  63. if (customClass == 'hey') customClass = `vab-hey-message-${type}`
  64. if (dangerouslyUseHTMLString && typeof dangerouslyUseHTMLString == 'function') {
  65. callback = dangerouslyUseHTMLString
  66. dangerouslyUseHTMLString = false
  67. }
  68. ElMessage({
  69. message,
  70. type,
  71. customClass,
  72. duration: messageDuration,
  73. dangerouslyUseHTMLString,
  74. showClose: false,
  75. onClose: () => {
  76. if (callback) callback()
  77. },
  78. })
  79. }
  80. /**
  81. * @description 全局Alert
  82. * @param {string|VNode} content 消息正文内容
  83. * @param {string} title 标题
  84. * @param {function} callback 若不使用Promise,可以使用此参数指定MessageBox关闭后的回调
  85. */
  86. const $baseAlert = (content: string | VNode, title = '温馨提示', callback: any = undefined) => {
  87. if (title && typeof title == 'function') {
  88. callback = title
  89. title = '温馨提示'
  90. }
  91. ElMessageBox.alert(content, title, {
  92. confirmButtonText: '确定',
  93. dangerouslyUseHTMLString: true, // 此处可能引起跨站攻击,建议配置为false
  94. draggable: true,
  95. callback: () => {
  96. if (callback) callback()
  97. },
  98. }).then(() => {})
  99. }
  100. /**
  101. * @description 全局Confirm
  102. * @param {string|VNode} content 消息正文内容
  103. * @param {string} title 标题
  104. * @param {function} callback1 确认回调
  105. * @param {function} callback2 关闭或取消回调
  106. * @param {string} confirmButtonText 确定按钮的文本内容
  107. * @param {string} cancelButtonText 取消按钮的自定义类名
  108. */
  109. const $baseConfirm = (
  110. content: string | VNode,
  111. title: string,
  112. callback1: any,
  113. callback2: any,
  114. confirmButtonText = '确定',
  115. cancelButtonText = '取消'
  116. ) => {
  117. ElMessageBox.confirm(content, title || '温馨提示', {
  118. confirmButtonText,
  119. cancelButtonText,
  120. closeOnClickModal: false,
  121. type: 'warning',
  122. lockScroll: false,
  123. })
  124. .then(() => {
  125. if (callback1) {
  126. callback1()
  127. }
  128. })
  129. .catch(() => {
  130. if (callback2) {
  131. callback2()
  132. }
  133. })
  134. }
  135. /**
  136. * @description 全局Notification
  137. * @param {string} message 说明文字
  138. * @param {string|VNode} title 标题
  139. * @param {'success'|'warning'|'info'|'error'} type 主题样式,如果不在可选值内将被忽略
  140. * @param {'top-right'|'top-left'|'bottom-right'|'bottom-left'} position 自定义弹出位置
  141. * @param duration 显示时间,毫秒
  142. */
  143. const $baseNotify = (
  144. message: string,
  145. title: string,
  146. type: 'success' | 'warning' | 'info' | 'error' = 'success',
  147. position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' = 'top-right',
  148. duration: number = messageDuration
  149. ) => {
  150. ElNotification({
  151. title,
  152. message,
  153. type,
  154. duration,
  155. position,
  156. })
  157. }
  158. const _emitter = mitt()
  159. const $pub = (...args: any[]) => {
  160. _emitter.emit(head(args), args[1])
  161. }
  162. const $sub = function () {
  163. // eslint-disable-next-line prefer-rest-params
  164. Reflect.apply(_emitter.on, _emitter, toArray(arguments))
  165. }
  166. const $unsub = function () {
  167. // eslint-disable-next-line prefer-rest-params
  168. Reflect.apply(_emitter.off, _emitter, toArray(arguments))
  169. }
  170. if (isCheck()) {
  171. app.provide('$baseLoading', $baseLoading)
  172. app.provide('$baseMessage', $baseMessage)
  173. app.provide('$baseAlert', $baseAlert)
  174. app.provide('$baseConfirm', $baseConfirm)
  175. app.provide('$baseNotify', $baseNotify)
  176. app.provide('$sub', $sub)
  177. app.provide('$pub', $pub)
  178. app.provide('$unsub', $unsub)
  179. gp = {
  180. $pub,
  181. $sub,
  182. $baseNotify,
  183. $baseLoading,
  184. $baseMessage,
  185. }
  186. }
  187. },
  188. }