permission.ts 775 B

1234567891011121314151617181920212223242526272829303132333435
  1. import type { Directive, DirectiveBinding } from 'vue'
  2. import { userStore } from '@/stores/user'
  3. export const vAuth: Directive = {
  4. mounted(el: HTMLElement, binding: DirectiveBinding) {
  5. el.addEventListener('click', async (e: Event) => {
  6. e.stopPropagation()
  7. const user = userStore();
  8. const callback = binding.value
  9. if (typeof callback !== 'function') {
  10. return
  11. }
  12. if (!user.checkUserLoggedIn) {
  13. // 这里调用你现有的显示登录框方法
  14. user.showLoginModel = true
  15. return
  16. }
  17. try {
  18. await callback(e)
  19. } catch (error) {
  20. console.error('操作执行失败:', error)
  21. }
  22. })
  23. }
  24. }
  25. export default {
  26. install(app: any) {
  27. app.directive('auth', vAuth)
  28. }
  29. }