1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| // main.js 或者你的入口文件 import Vue from 'vue'; import { BASE_URL, downLoadUrl } from '@/api/request';
Vue.mixin({ data() { return { navBarHeight: uni.getStorageSync("navBarHeight") || 90, // 顶部高度 tabBarHeight: uni.getStorageSync("tabBarHeight") || 80, // 底部高度 imgurl: this.imgurl, // 图片前缀 BASE_URL: BASE_URL, // 请求前缀 downLoadUrl: downLoadUrl, // 下载文件前缀 } }, methods: { // 复制 copy(text) { uni.setClipboardData({ data: text, success: (res) => { this.$u.toast("复制成功"); }, fail: (err) => { this.$u.toast("复制失败"); }, }); }, // 计算表单高度 getNavBarHeight(className) { return new Promise((resolve, reject) => { const query = uni.createSelectorQuery().in(this); query .select(className) .boundingClientRect((data) => { console.log("data.height :>> ", data.height); resolve(data.height); }) .exec(); }); }, // 跳转页面 goPage(url) { if (!url) { return; } uni.navigateTo({ url: url, }); }, // 图片上传 uploadFilePromise(url) { return new Promise((resolve, reject) => { uni.uploadFile({ url: this.BASE_URL + "/uploadImage", // 仅为示例,非真实的接口地址 filePath: url, header: { token: uni.getStorageSync('token') || '' }, name: "file", success: (res) => { console.log('res :>> ', res); let data = JSON.parse(res.data).url setTimeout(() => { resolve(data); }, 1000); }, }); }); }, // 文件下载 downloadFilePromise(url) { console.log('url :>> ', url); console.log('this.downLoadUrl + url :>> ', this.downLoadUrl + url); return new Promise((resolve, reject) => { const downloadTask = uni.downloadFile({ url: this.downLoadUrl + url, // 文件下载地址 success: (res) => { if (res.statusCode === 200) { // 下载成功处理 console.log('下载成功'); // 保存到本地文件系统 uni.saveFile({ tempFilePath: res.tempFilePath, //临时文件路径 filePath: `${wx.env.USER_DATA_PATH}/${url}`, // 指定保存路径和文件名 success: (saveRes) => { console.log('文件保存成功', saveRes.savedFilePath); resolve(saveRes.savedFilePath) }, fail: (err) => { console.log('保存文件失败', err); } }); } else { console.log('下载失败', res.statusCode); } }, fail: (err) => { console.log('下载过程中出错', err); } });
downloadTask.onProgressUpdate((res) => { uni.showLoading({ title: `下载进度 ${res.progress}%`, mask: true, }); if (res.progress == 100) { uni.hideLoading(); uni.showToast({ title: "下载成功", icon: "success", duration: 1500, }); } }); }) } } });
|