PixelAI-mobile/utils/utils.js
2025-05-30 20:04:47 +08:00

111 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import configService from "../common/config.service";
import store from "../store";
export const tools = {
methods: {
//复制url
copyUrl() {
this.uniCopy({
content: window.location.href,
success: () => {
uni.showToast({icon:none,title:'复制成功!'})
},
error: () => {
uni.showToast({icon:none,title:'复制失败!'})
}
})
},
//复制内容
uniCopy({content,success,error}) {
content = typeof content === 'string' ? content : content.toString() // 复制内容,必须字符串,数字需要转换为字符串
/**
* 小程序端 和 app端的复制逻辑
*/
//#ifndef H5
uni.setClipboardData({
data: content,
success: function() {
success("复制成功~")
},
fail: function() {
error("复制失败~")
}
});
//#endif
/**
* H5端的复制逻辑
*/
// #ifdef H5
if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
// 不支持
error('浏览器不支持')
}
let textarea = document.createElement("textarea")
textarea.value = content
textarea.readOnly = "readOnly"
document.body.appendChild(textarea)
textarea.select() // 选择对象
textarea.setSelectionRange(0, content.length) //核心
let result = document.execCommand("copy") // 执行浏览器复制命令
if (result) {
success("复制成功~")
} else {
error("复制失败请检查h5中调用该方法的方式是不是用户点击的方式调用的如果不是请改为用户点击的方式触发该方法因为h5中安全性不能js直接调用")
}
textarea.remove()
// #endif
},
//预览图片
lookImage(index, imagesList, itemList = ['发送给朋友', '保存图片', '收藏']) {
uni.previewImage({
current: index,
urls: imagesList,
indicator: "number",
showmenu: true,
longPressActions: {
itemList: itemList,
success: function(data) {
if(data.tapIndex === 1){
//先下载到本地获取临时路径
uni.downloadFile({
url: imagesList[data.index],
success: (res) => {
//将临时路径保存到相册,即可在相册中查看图片
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath, //不支持网络地址
success: function () {
uni.showToast({icon:"none",title: '保存图片到相册成功',position: 'bottom'});
}
});
},
fail: (err) => {
uni.showToast({icon:"none",title: '保存图片失败',position: 'bottom'});
}
})
}
console.log(data)
},
fail: function(err) {
console.log(err.errMsg);
}
}
});
},
// 上传文件
async uploadFile(filePath,success) {
let that = this;
await uni.uploadFile({
url: configService.apiUrl+'/enAttachment/upload',
filePath: filePath,
name: 'file',
header:{'token':store.state.vuex_token},
success: async(res) => {
success(JSON.parse(res.data));
},
fail: function (res) {
}
});
},
}
}