vab.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. export 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 (error) {
  30. console.log(error)
  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,
  46. 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
  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. grouping: true,
  76. plain: true,
  77. onClose: () => {
  78. if (callback) callback()
  79. },
  80. })
  81. }
  82. /**
  83. * @description 全局Alert
  84. * @param {string|VNode} content 消息正文内容
  85. * @param {string} title 标题
  86. * @param {function} callback 若不使用Promise,可以使用此参数指定MessageBox关闭后的回调
  87. */
  88. const $baseAlert = (content: string | VNode, title = '温馨提示', callback?: any) => {
  89. if (title && typeof title == 'function') {
  90. callback = title
  91. title = '温馨提示'
  92. }
  93. ElMessageBox.alert(content, title, {
  94. confirmButtonText: '确定',
  95. dangerouslyUseHTMLString: true, // 此处可能引起跨站攻击,建议配置为false
  96. draggable: true,
  97. callback: () => {
  98. if (callback) callback()
  99. },
  100. }).then(() => {})
  101. }
  102. /**
  103. * @description 全局Confirm
  104. * @param {string|VNode} content 消息正文内容
  105. * @param {string} title 标题
  106. * @param {function} callback1 确认回调
  107. * @param {function} callback2 关闭或取消回调
  108. * @param {string} confirmButtonText 确定按钮的文本内容
  109. * @param {string} cancelButtonText 取消按钮的自定义类名
  110. */
  111. const $baseConfirm = (
  112. content: string | VNode,
  113. title: string,
  114. callback1: any,
  115. callback2: any,
  116. confirmButtonText = '确定',
  117. cancelButtonText = '取消'
  118. ) => {
  119. ElMessageBox.confirm(content, title || '温馨提示', {
  120. confirmButtonText,
  121. cancelButtonText,
  122. closeOnClickModal: false,
  123. draggable: true,
  124. type: 'warning',
  125. lockScroll: false,
  126. })
  127. .then(() => {
  128. if (callback1) {
  129. callback1()
  130. }
  131. })
  132. .catch(() => {
  133. if (callback2) {
  134. callback2()
  135. }
  136. })
  137. }
  138. /**
  139. * @description 全局Notification
  140. * @param {string} message 说明文字
  141. * @param {string|VNode} title 标题
  142. * @param {'success'|'warning'|'info'|'error'} type 主题样式,如果不在可选值内将被忽略
  143. * @param {'top-right'|'top-left'|'bottom-right'|'bottom-left'} position 自定义弹出位置
  144. * @param duration 显示时间,毫秒
  145. */
  146. const $baseNotify = (
  147. message: string,
  148. title: string,
  149. type: 'success' | 'warning' | 'info' | 'error' = 'success',
  150. position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' = 'top-right',
  151. duration: number = messageDuration
  152. ) => {
  153. ElNotification({
  154. title,
  155. message,
  156. type,
  157. duration,
  158. position,
  159. })
  160. }
  161. const _emitter = mitt()
  162. const $pub = (...args: any[]) => {
  163. _emitter.emit(head(args), args[1])
  164. }
  165. const $sub = (...args: any[]) => {
  166. Reflect.apply(_emitter.on, _emitter, toArray(args))
  167. }
  168. const $unsub = (...args: any[]) => {
  169. Reflect.apply(_emitter.off, _emitter, toArray(args))
  170. }
  171. if (isCheck()) {
  172. app.provide('$baseAlert', $baseAlert)
  173. app.provide('$baseConfirm', $baseConfirm)
  174. app.provide('$baseLoading', $baseLoading)
  175. app.provide('$baseMessage', $baseMessage)
  176. app.provide('$baseNotify', $baseNotify)
  177. app.provide('$pub', $pub)
  178. app.provide('$sub', $sub)
  179. app.provide('$unsub', $unsub)
  180. gp = {
  181. $baseAlert,
  182. $baseConfirm,
  183. $baseLoading,
  184. $baseMessage,
  185. $baseNotify,
  186. $pub,
  187. $sub,
  188. $unsub,
  189. }
  190. }
  191. },
  192. }