import { ref } from "vue"; /** * opt object | string * to_url object | string * 例: * this.Tips('/pages/test/test'); 跳转不提示 * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转 * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上 * tab=1 一定时间后跳转至 table上 * tab=2 一定时间后跳转至非 table上 * tab=3 一定时间后返回上页面 * tab=4 关闭所有页面跳转至非table上 * tab=5 关闭当前页面跳转至table上 */ export function useToast() { // 提示标题 const tipTitle = ref(""); // 提示图标 const tipIcon = ref("none"); // 提示持续时间 const tipEndtime = ref(2000); /** * Tips - 显示提示并可按需跳转页面 * @param {Object|string} opt 提示配置或直接为跳转路径 * @param {Object|string|Function} to_url 跳转配置、路径或回调 */ function Toast(opt, to_url) { console.log("Toast", opt, to_url); // 如果第一个参数是字符串,视为跳转路径 if (typeof opt == "string") { to_url = opt; opt = {}; } // 提示内容 tipTitle.value = opt.title || ""; // 图标 tipIcon.value = opt.icon || "none"; // 持续时间 tipEndtime.value = opt.endtime || 1000; // 接口调用成功的回调函数 let success = opt.success; // 显示提示 if (tipTitle.value) uni.showToast({ title: tipTitle.value, icon: tipIcon.value, duration: tipEndtime.value, success, }); // 跳转逻辑 if (to_url != undefined) { if (typeof to_url == "object") { // 对象方式配置跳转 let tab = to_url.tab || 1, url = to_url.url || ""; console.log('tab', tab) switch (tab) { case 1: // 一定时间后跳转至 table setTimeout(() => uni.switchTab({ url }), tipEndtime.value); break; case 2: // 跳转至非table页面 setTimeout(() => uni.navigateTo({ url }), tipEndtime.value); break; case 3: // 返回上一个页面 setTimeout(() => { // #ifndef H5 uni.navigateBack({ delta: 1 }); // #endif // #ifdef H5 history.back(); // #endif }, tipEndtime.value); break; case 4: // 关闭所有页面跳转到非 tab 页面 setTimeout(() => uni.reLaunch({ url }), tipEndtime.value); break; case 5: // 关闭当前页面跳转到非 tab 页面 setTimeout(() => uni.redirectTo({ url }), tipEndtime.value); break; } } else if (typeof to_url == "function") { // 回调函数 setTimeout(() => to_url && to_url(), tipEndtime.value); } else { // 普通路径跳转 setTimeout( () => uni.navigateTo({ url: to_url }), tipTitle.value ? tipEndtime.value : 0 ); } } } // 导出响应式数据和方法 return { tipTitle, tipIcon, tipEndtime, Toast }; }