123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { FILE_ICON } from '@/common/EnumConst';
- export const getImageUrl = (uri) => {
- return `${process.env.UNI_FILE_PREFIX}${uri}`;
- };
- export const viewFileFn = (fileUrl, fileName, fileSuffix) => { // 文件url 文件名 文件后缀
- uni.showLoading({
- title: '加载中…',
- mask: true,
- });
- let downloadTask = uni.downloadFile({
- url: fileUrl,
- success: function (res) {
- console.log('downloadFile 成功', res);
- var tempFilePath = res.tempFilePath; // 下载成功后的临时文件路径
- var newFilePath = `${wx.env.USER_DATA_PATH}/${fileName}${fileSuffix}`; // 新的文件路径,USER_DATA_PATH 是小程序的本地文件存储目录
- uni.saveFile({
- tempFilePath: tempFilePath,
- filePath: newFilePath,
- success: function (res) {
- console.log('saveFile 成功', res.savedFilePath);
- uni.openDocument({
- filePath: res.savedFilePath, // 打开新的文件路径
- showMenu: false,
- success: function (res) {
- console.log('openDocument 成功', res);
- },
- fail: function (err) {
- console.log('openDocument 失败', err);
- },
- });
- uni.hideLoading();
- },
- fail: function (err) {
- console.log('saveFile 失败', err);
- uni.hideLoading();
- },
- });
- },
- fail: function (res) {
- console.log('downloadFile 失败', res);
- uni.hideLoading();
- },
- });
- };
- export const setStorageObj = (key, value) => {
- const json = JSON.stringify(value);
- uni.setStorageSync(key, json);
- };
- export const getStorageObj = (key) => {
- const json = uni.getStorageSync(key);
- if (!json) {
- return null;
- }
- return JSON.parse(json);
- };
- export const getUserInfo = () => {
- const userInfo = getStorageObj('userInfo');
- if (!userInfo) {
- return null;
- }
- return userInfo;
- };
- export const getToken = () => {
- const token = getStorageObj('token');
- if (!token) {
- return null;
- }
- return token;
- };
- export const authLogin = (callback) => {
- const user = getUserInfo();
- if (!user) {
- uni.navigateTo({
- url: '/pasb/pages/login/index',
- });
- return;
- }
- callback(user);
- };
- export const fileIcon = (fileSuffix) => {
- let icon = '';
- if (FILE_ICON[fileSuffix]) {
- return getImageUrl(FILE_ICON[fileSuffix]);
- }
- return getImageUrl(FILE_ICON.defIcon);
- };
|