util.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const FileSystemManager = wx.getFileSystemManager();
  2. const formatTime = date => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  10. }
  11. const formatNumber = n => {
  12. n = n.toString()
  13. return n[1] ? n : `0${n}`
  14. }
  15. const openFileFunc = (url, fileName = '') => {
  16. wx.showLoading({
  17. title: '',
  18. })
  19. wx.downloadFile({
  20. url,
  21. success: function (res) {
  22. const filePath = res.tempFilePath;
  23. FileSystemManager.saveFile({
  24. tempFilePath: filePath,
  25. filePath: wx.env.USER_DATA_PATH + '/' + decodeURI(fileName || '文件'),
  26. success() {
  27. wx.openDocument({
  28. filePath: wx.env.USER_DATA_PATH + '/' + decodeURI(fileName || '文件'),
  29. success: function (res) {
  30. wx.hideLoading();
  31. },
  32. fail() {
  33. wx.hideLoading();
  34. }
  35. })
  36. },
  37. fail(e) {
  38. wx.hideLoading()
  39. wx.showToast({
  40. title: '请稍后再试!',
  41. })
  42. }
  43. })
  44. },
  45. fail() {
  46. wx.hideLoading();
  47. wx.showToast({
  48. title: '请稍后再试!',
  49. })
  50. }
  51. })
  52. }
  53. const toNavigatePage = (url = '') => {
  54. wx.navigateTo({
  55. url
  56. })
  57. }
  58. module.exports = {
  59. formatTime,
  60. openFileFunc,
  61. toNavigatePage
  62. }