util.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. icon: 'none'
  42. })
  43. }
  44. })
  45. },
  46. fail() {
  47. wx.hideLoading();
  48. wx.showToast({
  49. title: '请稍后再试!',
  50. })
  51. }
  52. })
  53. }
  54. const toNavigatePage = (url = '') => {
  55. wx.navigateTo({
  56. url
  57. })
  58. }
  59. module.exports = {
  60. formatTime,
  61. openFileFunc,
  62. toNavigatePage
  63. }