@hzy1123581324/z-view-ui
Version:
z-view-ui是使用vue3开发的组件,开发中,有部分组件功能未实现,慎用
489 lines (449 loc) • 13.2 kB
JavaScript
import {
parseParam
} from '../utils/queryParams.js';
import {
isIos
} from '../utils/test.js';
import {
isArray
} from './test.js'
import {
getCurrentInstance
} from 'vue';
// 设置粘贴板
export function setCopy(data = "", title = '复制成功', duration = 2000) {
// #ifndef H5
uni.setClipboardData({
data,
success: () => {
toast(title, duration);
}
});
// #endif
// #ifdef H5
const textarea = document.createElement('textarea')
textarea.value = data
textarea.readOnly = true
document.body.appendChild(textarea)
textarea.select()
textarea.setSelectionRange(0, data.length)
document.execCommand('copy')
textarea.remove()
uni.showToast({
title,
duration,
icon: 'none'
})
// #endif
}
// 获取黏贴版
// deme
// 需要使用 await
// await getCopy()
export async function getCopy() {
console.warn('需要使用 await');
let data = "";
// #ifndef H5
data = await uni.getClipboardData().then(res => res.data);
// #endif
// #ifdef H5
// try {
// console.log('44444444444')
data = await navigator.clipboard.readText().then(res => {
// console.log(res);
return res;
}).catch(err => console.log(err, '5555555'))
// } catch (error) {
// fail(error)
// }
// #endif
// console.log/('会打印两次不知道为什么')
return data;
}
/**
* @description 预加载列表
* @param {Array} list 预加载列表
*/
export function preloadPageList(list) {
// #ifdef H5
list.map((url) => {
uni.preloadPage({
url, //"/pages/order/order"
});
})
// #endif
}
/**
* @description 当页面有可能处于第一页时
*/
export function firstJump(func = () => {}) {
if (getCurrentPages()?.length == 1) {
if (uni.getStorageSync('lastRouter')) {
// console.log(uni.getStorageSync('lastRouter').path);
uni.switchTab({
url: uni.getStorageSync('lastRouter').path
})
} else {
uni.switchTab({
url: "/pages/index/index"
})
}
} else {
//
func();
}
}
/**
* @description 页面返回
* @param {Number} delta 返回第几页 默认返回上一页
* @param {String} redirectPath 重定向路由 默认是首页 /pages/index/index
*/
export function pageBack({
delta = 1,
redirectPath = "/pages/index/index"
} = {}) {
const pageList = getCurrentPages();
if (pageList.length <= 1) {
uni.switchTab({
url: redirectPath
})
} else {
uni.navigateBack({
delta
})
}
}
/**
* @description 通用跳转
* @param
*/
export function jump(url, {
type = 'navigateTo',
message = '待开放',
} = {}) {
if (url === '' || url === '#') {
return toast(message)
}
switch (type) {
case 'navigateTo':
case 'redirectTo':
case 'switchTab':
case 'reLaunch':
// 'switchTab','reLaunch' 参数传递不了
// 以下没效果
// if (['switchTab', 'reLaunch'].indexOf(type) > -1) {
// console.log( getCurrentPages()[0].$route,'8888888888855555555555')
// const param = parseParam(url);
// const router = {
// param,
// path: url.split(',')[0],
// fullPath: url,
// }
// uni.setStorageSync('lastRouter', router);
// }
uni[type]({
url,
fail() {
if (process.env.NODE_ENV === 'development') {
uni.showToast({
title: '页面未注册',
icon: "error"
})
}
}
});
break;
case 'navigateBack':
// 返回上一页url 是数字
firstJump(() => {
uni.navigateBack({
delta: url
})
});
// 第三方外部链接
case "_blank":
case "_self":
// #ifndef H5
// 跳转到webview页面
uni.navigateTo({
url: '/pages/webview/webview?url=' + url,
fail() {
if (process.env.NODE_ENV === 'development') {
uni.showToast({
title: '页面未注册',
icon: "error"
})
}
}
})
// #endif
// #ifdef H5
if (type == '_self') {
// console.log('https://baidu.com','999999999999999')
window.location.href = url;
// window.open('https://baidu.com','_self')
// window.location.href = url;
} else {
origin.open(url, '_blank')
}
//#endif
default:
console.lolg(type, '没匹配上')
break;
}
}
export function goOtherApp({
pname = "",
action = "",
message = "",
extra = {},
urlSchemes = "",
downloadUrl = "",
} = {}) {
// console.log(pname,action,message,extra,urlSchemes,JSON.stringify(extra))
// #ifdef APP-PLUS
console.log('12312412341324')
// 判断平台
if (plus.os.name == 'Android') {
if (!pname) {
toast('安卓需要传入pname值')
return
}
if (plus.runtime.isApplicationExist({ //查看安卓系统手机有没有下载这款app
pname, //B款app云打包的包名
})) { //安装了app
plus.runtime.openURL(urlSchemes, function(res) {
console.log(res);
});
} else {
console.log("没安装")
downloadUrl && plus.runtime.openURL(downloadUrl, function(res) {
console.log(res);
});
}
// plus.runtime.launchApplication({
// // pname: {
// // name: '微信',
// // pname: 'im.token.app',
// // scheme: 'weixin://'
// // },
// // pname: 'com.taobao.taobao'
// pname,
// extra: JSON.stringify(extra)
// },
// (e) => {
// console.log('Open system default browser failed: ' + e.message);
// toast(message||e.message)
// }
// );
} else if (plus.os.name == 'iOS') {
if (!action) {
toast('ios需要传入action值')
return
}
plus.runtime.launchApplication({
action,
/* action: 'taobao://'*/
}, (e) => {
// console.log('Open system default browser failed: ' + e.message);
toast(message)
downloadUrl && plus.runtime.openURL(urlSchemes, function(res) {
console.log(res);
});
});
}
// #endif
// #ifdef H5
if (urlSchemes) {
window.location.href = urlSchemes;
} else {
if (process.env.NODE_ENV === 'development') { // 开发环境
console.warn("需要传入urlSchemes")
console.warn("需要传入urlSchemes")
console.warn("需要传入urlSchemes")
console.warn("需要传入urlSchemes")
}
}
// #endif
}
/*
if (plus.os.name == "Android") { //安卓
if (plus.runtime.isApplicationExist({ //查看安卓系统手机有没有下载这款app
pname: 'com.baijia.live', //B款app云打包的包名
})) { //安装了app
plus.runtime.openURL( 'bjhlliveapp://room.join?joinCode=' + params.joinCode + '&userName=' + params.userName + '&userType=' + params.userType + '&classType=' + params.classType + '&privateDomain=e87766440', function(res) {
console.log(res);
});
}else { //未安装app
uni.showModal({
title: '提示',
content: '您还没有此APP,去下载',
success: function (res) {
if (res.confirm) {
plus.runtime.openURL('https://a.app.qq.com/o/simple.jsp?pkgname=com.baijia.live', function(res) { //打开应用宝 让应用宝下载app
//进入后台小哥哥给我的appStore下载app链接,,让你们后台给你
//这链接会判断你手机是ios还是Android,ios进入应用宝下载app
//跟上面的是一个链接
console.log(res);
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
} else if (plus.os.name == "iOS") { //苹果
//因为ios查不到B款app在ios系统手机里面,其实下载了,也是检测不到,所以就不检测了
//直接打开B款app,B款app没有的话,会进入回调报错,我们在回调去打开下载链接
plus.runtime.launchApplication({
action: 'bjhlliveapp://room.join?joinCode=' + params.joinCode + '&userName=' + params.userName + '&userType=' + params.userType + '&classType=' + params.classType + '&privateDomain=e87766440'
}, function(e) {
uni.showModal({
title: '提示',
content: '您还没有此APP,去下载',
success: function (res) {
if (res.confirm) {
plus.runtime.openURL('https://a.app.qq.com/o/simple.jsp?pkgname=com.baijia.live', function(res) {//打开应用宝 让应用宝下载app
//进入后台小哥哥给我的appStore下载app链接,,让你们后台给你
//这链接会判断你手机是ios还是Android,ios进入应用宝下载app
//跟上面的是一个链接
console.log(res);
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
});
}
————————————————
版权声明:本文为CSDN博主「臭臭鬼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40630924/article/details/112463231
*/
/**
* @description jumpEvent()
*/
// export function jumpEvent(url="",){
// uni.navigateTo({
// url,
// events: {
// // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
// acceptDataFromOpenedPage: function(data) {
// console.log(data)
// },
// someEvent: function(data) {
// console.log(data)
// }
// }
// })
// }
/**
* @description 只有文本的提示框
*/
export function toast(title, duration = 1500) {
return uni.showToast({
title,
icon: "none",
duration
})
}
/**
* @param {String} className 样式类
* @description 获取节点信息
*/
export function getElQuery(className, component = null) {
// 组件内需要传component
// const { proxy } = getCurrentInstance();
// component = proxy
return new Promise(resolve => {
let queryInfo = '';
// 获取元素节点信息,请查看uniapp相关文档
// https://uniapp.dcloud.io/api/ui/nodes-info?id=nodesrefboundingclientrect
queryInfo = uni.createSelectorQuery().in(component);
//#ifdef MP-ALIPAY
queryInfo = uni.createSelectorQuery();
//#endif
queryInfo.select(className).boundingClientRect();
queryInfo.exec(data => {
// console.log(data);
// console.log('%%%%%%%%%%%%%%%');
resolve(data);
});
});
}
export function getRect(selector, all = false, component = null) {
return new Promise((resolve) => {
uni.createSelectorQuery()
.in(component)[all ? 'selectAll' : 'select'](selector)
.boundingClientRect((rect) => {
if (all && isArray(rect) && rect.length) {
resolve(rect)
}
if (!all && rect) {
resolve(rect)
}
})
.exec()
})
}
// 获取节点信息
export function getElQueryFields(className, component = null, fields = {
size: true,
rect: true,
scrollOffset: true,
}, ) {
// 组件内需要传component
// const { proxy } = getCurrentInstance();
// component = proxy
return new Promise((resolve, reject) => {
let queryInfo = '';
// 获取元素节点信息,请查看uniapp相关文档
// https://uniapp.dcloud.io/api/ui/nodes-info?id=nodesrefboundingclientrect
queryInfo = uni.createSelectorQuery().in(component);
//#ifdef MP-ALIPAY
queryInfo = uni.createSelectorQuery();
//#endif
queryInfo.select(className).fields(fields, data => {
// console.log( "得到布局位置信息" + JSON.stringify(data));
// console.log("节点离页面顶部的距离为" + data?.top);
if (data) {
resolve(data);
} else {
reject(data);
}
}).exec();
});
}
// 图片保存到相册
function saveImageToPhotosAlbum(url) {
// #ifdef MP
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success() {
_save()
}
});
} else {
_save()
}
}
});
// #endif
// #ifdef APP-PLUS
function _save() {
uni.saveImageToPhotosAlbum({
filePath: url,
success: (res) => {
toast('保存成功');
},
fail: (res) => {
toast('保存失败');
}
});
}
_save()
// #endif
}