index.js 2.1 KB

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