12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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;
- };
|