tools.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import pinia from "@/store/init"
  2. import {useAppStore} from "@/store";
  3. const store = useAppStore(pinia)
  4. import {getObjVal, isNullES} from "js-fast-way";
  5. // 按钮权限
  6. export const btnAuth = async (auth = '') => {
  7. if(isNullES(auth)) auth = 'all'
  8. try {
  9. let isDownload = false, isPrint = false;
  10. //下载权限
  11. const pdfDownload = store.getButtonsVal('client-pdf-download')
  12. isDownload = !!getObjVal(pdfDownload)
  13. //打印权限
  14. const pdfPrint = store.getButtonsVal('client-pdf-print')
  15. isPrint = !!getObjVal(pdfPrint)
  16. //判断权限
  17. if(auth == 'all') {
  18. return isDownload && isPrint
  19. } else if(auth == 'download') {
  20. return isDownload
  21. } else if(auth == 'print') {
  22. return isPrint
  23. }
  24. } catch {
  25. return false
  26. }
  27. }
  28. //确认框
  29. export const showModal = async ({title, content, confirmText, cancelText}) => {
  30. return new Promise((resolve) => {
  31. uni.showModal({
  32. title: title,
  33. content: content,
  34. confirmText: confirmText ?? '确定',
  35. cancelText: cancelText ?? '取消',
  36. success: function (res) {
  37. if (res.confirm) {
  38. resolve(true)
  39. } else if (res.cancel) {
  40. resolve(false)
  41. }
  42. }
  43. });
  44. })
  45. }
  46. //成功提示
  47. export const successToast = (title = '成功', duration= 2000) => {
  48. uni.showToast({
  49. title: title,
  50. duration: duration,
  51. mask: true
  52. });
  53. }
  54. //失败提示
  55. export const errorToast = (title = '失败', duration= 1500) => {
  56. uni.showToast({
  57. title: title,
  58. duration: duration,
  59. icon: 'none'
  60. });
  61. }
  62. //获取元素信息
  63. export const querySelect = async (_this, name) => {
  64. return new Promise((resolve) => {
  65. const query = uni.createSelectorQuery().in(_this);
  66. query.select(`#${name}`).boundingClientRect(data => {
  67. resolve(data)
  68. }).exec();
  69. })
  70. }
  71. //表单验证
  72. export const formValidate = async (formRef) => {
  73. return new Promise( (resolve) => {
  74. formRef.validate().then((res) => {
  75. resolve(true)
  76. }).catch(err => {
  77. resolve(false)
  78. })
  79. });
  80. }
  81. //pdf文件预览
  82. export const toPdfPreview = async (url) => {
  83. const isAuth = await btnAuth()
  84. // #ifdef H5
  85. window.open(url, '_blank')
  86. // #endif
  87. // #ifdef APP-PLUS
  88. if(isAuth) {
  89. const res = await showModal({
  90. title: '提示',
  91. content: '请选择一种预览pdf的方式',
  92. confirmText: '系统级预览',
  93. cancelText: '应用内预览',
  94. })
  95. if (res) {
  96. openDocFile(url)
  97. } else {
  98. uni.navigateTo({
  99. url: '/pages/index/preview?url=' + encodeURIComponent(url)
  100. });
  101. }
  102. } else {
  103. uni.navigateTo({
  104. url: '/pages/index/preview?url=' + encodeURIComponent(url)
  105. });
  106. }
  107. // #endif
  108. }
  109. //预览文件
  110. export const openDocFile = (url, fileType='pdf') => {
  111. uni.showLoading({title: '加载中...', mask: true})
  112. uni.downloadFile({
  113. url: url,
  114. success: function (res) {
  115. uni.openDocument({
  116. filePath: res.tempFilePath,
  117. fileType: fileType,
  118. showMenu: true,
  119. })
  120. },
  121. complete: function () {
  122. uni.hideLoading()
  123. }
  124. });
  125. }