index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { FILE_ICON } from '@/common/EnumConst';
  2. export const getImageUrl = (uri) => {
  3. return `${process.env.UNI_FILE_PREFIX}${uri}`;
  4. };
  5. export const viewFileFn = (fileUrl, fileName, fileSuffix) => { // 文件url 文件名 文件后缀
  6. uni.showLoading({
  7. title: '加载中…',
  8. mask: true,
  9. });
  10. let downloadTask = uni.downloadFile({
  11. url: fileUrl,
  12. success: function (res) {
  13. console.log('downloadFile 成功', res);
  14. var tempFilePath = res.tempFilePath; // 下载成功后的临时文件路径
  15. var newFilePath = `${wx.env.USER_DATA_PATH}/${fileName}${fileSuffix}`; // 新的文件路径,USER_DATA_PATH 是小程序的本地文件存储目录
  16. uni.saveFile({
  17. tempFilePath: tempFilePath,
  18. filePath: newFilePath,
  19. success: function (res) {
  20. console.log('saveFile 成功', res.savedFilePath);
  21. uni.openDocument({
  22. filePath: res.savedFilePath, // 打开新的文件路径
  23. showMenu: false,
  24. success: function (res) {
  25. console.log('openDocument 成功', res);
  26. },
  27. fail: function (err) {
  28. console.log('openDocument 失败', err);
  29. },
  30. });
  31. uni.hideLoading();
  32. },
  33. fail: function (err) {
  34. console.log('saveFile 失败', err);
  35. uni.hideLoading();
  36. },
  37. });
  38. },
  39. fail: function (res) {
  40. console.log('downloadFile 失败', res);
  41. uni.hideLoading();
  42. },
  43. });
  44. };
  45. export const setStorageObj = (key, value) => {
  46. const json = JSON.stringify(value);
  47. uni.setStorageSync(key, json);
  48. };
  49. export const getStorageObj = (key) => {
  50. const json = uni.getStorageSync(key);
  51. if (!json) {
  52. return null;
  53. }
  54. return JSON.parse(json);
  55. };
  56. export const getUserInfo = () => {
  57. const userInfo = getStorageObj('userInfo');
  58. if (!userInfo) {
  59. return null;
  60. }
  61. return userInfo;
  62. };
  63. export const getToken = () => {
  64. const token = getStorageObj('token');
  65. if (!token) {
  66. return null;
  67. }
  68. return token;
  69. };
  70. export const authLogin = (callback) => {
  71. const user = getUserInfo();
  72. if (!user) {
  73. uni.navigateTo({
  74. url: '/pasb/pages/login/index',
  75. });
  76. return;
  77. }
  78. callback(user);
  79. };
  80. export const fileIcon = (fileSuffix) => {
  81. let icon = '';
  82. if (FILE_ICON[fileSuffix]) {
  83. return getImageUrl(FILE_ICON[fileSuffix]);
  84. }
  85. return getImageUrl(FILE_ICON.defIcon);
  86. };