武田小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.7 KiB

const app = getApp<IAppOption>();
// pages/story/a.ts
Component({
/**
*
*/
properties: {
chooseAvatar: {
type: Boolean,
value: false,
},
6 months ago
chooseMessage: {
type: Boolean,
value: false,
},
6 months ago
accept: {
type: String,
value: "image",
},
obscure: {
type: Boolean,
value: false,
},
6 months ago
edit: {
type: Boolean,
value: false,
},
},
/**
*
*/
6 months ago
data: {
6 months ago
show1: false,
6 months ago
fileTypeList: {
doc: 5,
docx: 5,
pdf: 4,
xls: 6,
xlsx: 6,
ppt: 7,
},
6 months ago
cacheEditParams: {},
6 months ago
},
/**
*
*/
methods: {
GetExtensionFileName(pathfilename) {
const reg = /(\\+)/g;
const pString = pathfilename.replace(reg, "#"); //用正则表达式来将\或\\替换成#
const arr = pString.split("#"); // 以“#”为分隔符,将字符分解为数组 例如 D Program Files bg.png
const lastString = arr[arr.length - 1]; //取最后一个字符
const arr2 = lastString.split("."); // 再以"."作为分隔符
return arr2[arr2.length - 1]; //将后缀名返回出来
},
6 months ago
handleAfterRead(e, editEnd = false) {
if (this.data.edit && !editEnd) {
this.setData({
show1: true,
cacheEditParams: e,
});
return;
}
6 months ago
if (this.data.obscure) {
wx.editImage({
src: e.detail.file.url || e.detail.file.tempFilePath,
6 months ago
success: (res: any) => {
this.uploadFile({
...e.detail.file,
tempFilePath: res.tempFilePath,
}).then((res) => {
6 months ago
this.triggerEvent("file", res as object);
});
},
6 months ago
fail: () => {
this.uploadFile(e.detail.file).then((res) => {
this.triggerEvent("file", res as object);
});
},
6 months ago
});
} else {
this.uploadFile(e.detail.file).then((res) => {
this.triggerEvent("file", res as object);
});
}
},
6 months ago
handleEditEnd() {
this.setData({
show1: false,
});
this.handleAfterRead(this.data.cacheEditParams, true);
},
handleChooseAvatar(e) {
this.uploadFile({
fileType: "image",
tempFilePath: e.detail.avatarUrl,
size: 0,
}).then((res) => {
this.triggerEvent("file", res as object);
});
},
6 months ago
handleChooseFile() {
wx.chooseMessageFile({
type: "file",
count: 1,
success: (res) => {
this.uploadFile({
...res.tempFiles[0],
fileType: res.tempFiles[0].type,
tempFilePath: res.tempFiles[0].path,
}).then((res) => {
this.triggerEvent("file", res as object);
});
},
fail() {
wx.showToast({
icon: "none",
title: "取消选择",
});
},
});
},
uploadFile(item) {
wx.showLoading({
title: "正在上传",
});
return new Promise((resolve, reject) => {
let url = `${app.globalData.upFileUrl}?r=file-service/upload-`;
if (item.fileType === "image") {
url += "img";
} else if (item.fileType === "video") {
url += "video";
} else if (item.fileType === "audio") {
url += "audio";
} else {
url += "doc";
}
wx.uploadFile({
filePath: item.tempFilePath,
name: "file",
url,
success: (res) => {
wx.hideLoading();
const data = JSON.parse(res.data);
6 months ago
const suffix = this.GetExtensionFileName(data.data.Url);
const expandJson = {
fileId: "",
name: data.data.Url,
size: (item.size / 1024).toFixed(2),
fileUrl: data.data.Url,
suffix: this.GetExtensionFileName(data.data.Url),
type: item.fileType,
imgUrl: "",
6 months ago
duration: 0,
fileType: this.data.fileTypeList[suffix] || 99,
};
if (item.fileType === "image") {
expandJson.imgUrl = data.data.Url;
6 months ago
} else if (item.fileType === "video") {
expandJson.imgUrl = data.data.SnapshotUrl;
6 months ago
} else if (item.fileType === "audio") {
expandJson.duration = Number.parseInt(String(item.duration / 1000));
}
6 months ago
console.log(expandJson);
resolve(expandJson);
},
fail() {
wx.hideLoading();
reject(new Error("上传失败"));
},
});
});
},
},
});
export {};