tools.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //确认框
  2. export const showModal = async ({title, content, confirmText, cancelText}) => {
  3. return new Promise((resolve) => {
  4. uni.showModal({
  5. title: title,
  6. content: content,
  7. confirmText: confirmText ?? '确定',
  8. cancelText: cancelText ?? '取消',
  9. success: function (res) {
  10. if (res.confirm) {
  11. resolve(true)
  12. } else if (res.cancel) {
  13. resolve(false)
  14. }
  15. }
  16. });
  17. })
  18. }
  19. //成功提示
  20. export const successToast = (title = '成功', duration= 2000) => {
  21. uni.showToast({
  22. title: title,
  23. duration: duration,
  24. mask: true
  25. });
  26. }
  27. //失败提示
  28. export const errorToast = (title = '失败', duration= 1500) => {
  29. uni.showToast({
  30. title: title,
  31. duration: duration,
  32. icon: 'none'
  33. });
  34. }
  35. //获取元素信息
  36. export const querySelect = async (_this, name) => {
  37. return new Promise((resolve) => {
  38. const query = uni.createSelectorQuery().in(_this);
  39. query.select(`#${name}`).boundingClientRect(data => {
  40. resolve(data)
  41. }).exec();
  42. })
  43. }
  44. //表单验证
  45. export const formValidate = async (formRef) => {
  46. return new Promise( (resolve) => {
  47. formRef.validate().then((res) => {
  48. resolve(true)
  49. }).catch(err => {
  50. resolve(false)
  51. })
  52. });
  53. }
  54. //pdf文件预览
  55. export const toPdfPreview = async (url) => {
  56. // #ifdef H5
  57. window.open(url, '_blank')
  58. // #endif
  59. // #ifdef APP-PLUS
  60. const res = await showModal({
  61. title: '提示',
  62. content: '请选择一种预览pdf的方式',
  63. confirmText: '系统级预览',
  64. cancelText: '应用内预览',
  65. })
  66. if (res) {
  67. openDocFile(url)
  68. } else {
  69. uni.navigateTo({
  70. url: '/pages/index/preview?url=' + encodeURIComponent(url)
  71. });
  72. }
  73. // #endif
  74. }
  75. //预览文件
  76. export const openDocFile = (url, fileType='pdf') => {
  77. uni.showLoading({title: '加载中...', mask: true})
  78. uni.downloadFile({
  79. url: url,
  80. success: function (res) {
  81. uni.openDocument({
  82. filePath: res.tempFilePath,
  83. fileType: fileType,
  84. showMenu: true,
  85. })
  86. },
  87. complete: function () {
  88. uni.hideLoading()
  89. }
  90. });
  91. }