"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var promise_wx_api_1 = require("../../utils/promise-wx-api"); var app = getApp(); var sys = wx.getSystemInfoSync(); console.log(sys); Component({ behaviors: [], properties: { displayed: { type: Boolean, value: true }, list: { type: Array, value: [], }, showAdd: { type: Boolean, value: true, }, formatLimit: { type: Array, value: ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"], }, max: { type: Number, value: 10, }, showList: { type: Array, value: [], }, selType: { type: String, value: "all", }, defautAddFlag: { type: Boolean, value: false, }, }, data: { attachmentList: [], }, lifetimes: { attached: function () { this.init(); var _a = this.data, showList = _a.showList, selType = _a.selType, defautAddFlag = _a.defautAddFlag; if (showList.length > 0) { if (!defautAddFlag) { if (selType === "image") { showList.forEach(function (element) { element.type = "image"; element.tempFilePath = element.url; element.name = "图片"; }); } } this.addList(showList); } }, }, methods: { init: function () { return __awaiter(this, void 0, void 0, function () { var list; return __generator(this, function (_a) { list = this.data.list; this.syncList(list); return [2]; }); }); }, syncList: function (list) { }, handleTapAdd: function () { return __awaiter(this, void 0, void 0, function () { var selType, _a, msg, data, tapIndex, _b, msg, data, tapIndex; return __generator(this, function (_c) { switch (_c.label) { case 0: selType = this.data.selType; if (!(selType === 'all')) return [3, 5]; if (!(sys.platform === "windows")) return [3, 2]; return [4, promise_wx_api_1.wxShowActionSheet({ itemList: ["选择图片", "选择视频", "从聊天记录"], })]; case 1: _a = _c.sent(), msg = _a.msg, data = _a.data; if (msg == "success") { tapIndex = data.tapIndex; if (tapIndex === 0) { this.selectImage(); } else if (tapIndex === 1) { this.selectMedia(); } else if (tapIndex === 2) { this.selectFile(); } } return [3, 4]; case 2: return [4, promise_wx_api_1.wxShowActionSheet({ itemList: ["从相册选择或拍摄", "从聊天记录"], })]; case 3: _b = _c.sent(), msg = _b.msg, data = _b.data; if (msg == "success") { tapIndex = data.tapIndex; if (tapIndex === 0) { this.selectMedia(); } else if (tapIndex === 1) { this.selectFile(); } } _c.label = 4; case 4: return [3, 6]; case 5: if (selType === 'image') { this.selectImage(); } _c.label = 6; case 6: return [2]; } }); }); }, selectMedia: function () { return __awaiter(this, void 0, void 0, function () { var _a, max, attachmentList, count, _b, msg, data, tempFiles, type_1, newList; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = this.data, max = _a.max, attachmentList = _a.attachmentList; count = max - attachmentList.length; count = count > 9 ? 9 : count; return [4, promise_wx_api_1.wxChooseMedia({ count: count, sizeType: ["compressed"], mediaType: ["image", "video"], })]; case 1: _b = _c.sent(), msg = _b.msg, data = _b.data; console.log("选择media", msg, data); if (msg === "success") { tempFiles = data.tempFiles, type_1 = data.type; newList = tempFiles.map(function (item) { var format = _this.getFormatByPath(item.tempFilePath); var name = type_1 === "video" ? "视频" : "图片"; name = name + "." + format; return { tempFilePath: item.tempFilePath, size: item.size, duration: item.duration, thumbTempFilePath: item.thumbTempFilePath, type: type_1, format: format, name: name, url: "", error: "", }; }); this.addList(newList); } return [2]; } }); }); }, selectImage: function () { return __awaiter(this, void 0, void 0, function () { var _a, max, attachmentList, count, res2, tempFiles, newList; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.data, max = _a.max, attachmentList = _a.attachmentList; if (attachmentList.length === max) { wx.showToast({ title: "不能超过" + max + "张图片", icon: "none", duration: 1500 }); return [2]; } count = max - attachmentList.length; count = count > 9 ? 9 : count; return [4, promise_wx_api_1.wxChooseImage({ count: count, sizeType: ["compressed"], })]; case 1: res2 = _b.sent(); console.log("选择image", res2); if (res2.msg === "success") { tempFiles = res2.data.tempFiles; newList = tempFiles.map(function (item) { var format = _this.getFormatByPath(item.path); var name = "图片"; name = name + "." + format; return { tempFilePath: item.path, size: item.size, type: "image", format: format, name: name, url: "", error: "", }; }); this.addList(newList); } return [2]; } }); }); }, selectFile: function () { return __awaiter(this, void 0, void 0, function () { var _a, max, attachmentList, formatLimit, _b, msg, data, tempFiles, newList; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: _a = this.data, max = _a.max, attachmentList = _a.attachmentList, formatLimit = _a.formatLimit; if (attachmentList.length === max) { wx.showToast({ title: "不能超过" + max + "个附件", icon: "none", duration: 1500 }); return [2]; } return [4, promise_wx_api_1.wxChooseMessageFile({ count: max - attachmentList.length, type: 'file', extension: formatLimit, })]; case 1: _b = _c.sent(), msg = _b.msg, data = _b.data; if (msg === "success") { console.log(data); tempFiles = data.tempFiles; newList = tempFiles.map(function (item) { var format = _this.getFormatByPath(item.path); var name = item.name; return { tempFilePath: item.path, size: item.size, type: item.type === "file" ? "doc" : item.type, format: format, name: name, url: "", error: "", }; }); this.addList(newList); } else if (msg === "chooseMessageFile:fail:not supported") { return [2, wx.showToast({ title: "当前环境不支持此操作", icon: "none", duration: 2000, })]; } return [2]; } }); }); }, addList: function (list) { var _a = this.data, attachmentList = _a.attachmentList, formatLimit = _a.formatLimit; var pathList = attachmentList.map(function (item) { return item.tempFilePath; }); var errorList = []; var newList = list.filter(function (item) { if (item.type === "doc") { if (formatLimit.indexOf(item.format) === -1) { errorList.push("仅限pdf文件"); return false; } else if (item.size > 1024 * 1024 * 5) { errorList.push("文档大小不能超过5M"); return false; } } else if (item.type === "image" || item.type === "video") { if (item.size > 1024 * 1024 * 10) { errorList.push("图片或视频大小不能超过10M"); return false; } } return pathList.indexOf(item.tempFilePath) === -1; }); if (errorList.length > 0) { wx.showToast({ title: errorList[0], icon: "none", duration: 3000, }); } this.setData({ attachmentList: __spreadArrays(attachmentList, newList), }); this.triggerEvent("addFileList", this.data.attachmentList); }, getFormatByPath: function (path) { var arr = path.split("."); if (arr.length > 0) { return arr.pop(); } else { return ""; } }, handleTapItem: function (e) { return __awaiter(this, void 0, void 0, function () { var selType, index, _a, msg, data, tapIndex; return __generator(this, function (_b) { switch (_b.label) { case 0: selType = this.data.selType; index = e.currentTarget.dataset.index; console.log(index); return [4, promise_wx_api_1.wxShowActionSheet({ itemList: ["查看", "删除", "重命名"], })]; case 1: _a = _b.sent(), msg = _a.msg, data = _a.data; if (msg == "success") { tapIndex = data.tapIndex; if (tapIndex === 0) { this.previewItem(index); } else if (tapIndex === 1) { this.delItem(index); } else if (tapIndex === 2) { this.rename(index); } } return [2]; } }); }); }, previewItem: function (index) { return __awaiter(this, void 0, void 0, function () { var attachmentList, item, res; return __generator(this, function (_a) { switch (_a.label) { case 0: attachmentList = this.data.attachmentList; item = attachmentList[index]; if (!(item.type === "doc")) return [3, 1]; promise_wx_api_1.wxOpenDocument({ filePath: item.tempFilePath, }); return [3, 4]; case 1: if (!(item.type === "video")) return [3, 3]; return [4, promise_wx_api_1.wxPreviewMedia({ sources: [ { url: item.tempFilePath, type: item.type, }, ], })]; case 2: res = _a.sent(); if (res.msg === "previewMedia:fail:not supported") { return [2, wx.showToast({ title: "当前环境不支持查看视频", icon: "none", duration: 2000, })]; } return [3, 4]; case 3: if (item.type === "image") { promise_wx_api_1.wxPreviewImage({ urls: [item.tempFilePath], }); } _a.label = 4; case 4: return [2]; } }); }); }, delItem: function (index) { var attachmentList = this.data.attachmentList; attachmentList.splice(index, 1); this.setData({ attachmentList: attachmentList, }); this.triggerEvent("deFileList", this.data.attachmentList); }, rename: function (index) { return __awaiter(this, void 0, void 0, function () { var attachmentList, item, name, nameArr, format, pureName, $inputName, _a, msg, data, value; return __generator(this, function (_b) { switch (_b.label) { case 0: attachmentList = this.data.attachmentList; item = attachmentList[index]; name = item.name; nameArr = name.split("."); format = nameArr.pop(); pureName = nameArr.join("."); $inputName = this.selectComponent("#inputName"); this.triggerEvent("hideNativeComponents"); return [4, $inputName.show({ title: "文件重命名", value: pureName, })]; case 1: _a = _b.sent(), msg = _a.msg, data = _a.data; this.triggerEvent("showNativeComponents"); if (msg === "success") { value = data.value; if (value) { item.name = value + "." + format; this.setData({ attachmentList: attachmentList }); wx.showToast({ title: "重命名成功", duration: 1500, }); } else { wx.showToast({ title: "文件名不能为空", icon: "none", duration: 2000, }); } } return [2]; } }); }); }, upload: function (uploadUrl, formDataParams) { return __awaiter(this, void 0, void 0, function () { var uploadedList, attachmentsValid, attachmentsAllUploaded; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!uploadUrl) { uploadUrl = "oss/file/uploadvariedfile"; } if (this.data.attachmentList.length === 0) { return [2, { msg: "success", data: [], }]; } wx.showLoading({ title: "上传文件中", mask: true, }); return [4, new Promise(function (reslove) { return __awaiter(_this, void 0, void 0, function () { var attachmentList, residueNum; var _this = this; return __generator(this, function (_a) { attachmentList = this.data.attachmentList, residueNum = attachmentList.length; attachmentList.forEach(function (item, index) { return __awaiter(_this, void 0, void 0, function () { var _a, data, msg; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!(item.url == "")) return [3, 2]; return [4, promise_wx_api_1.wxUploadFile(uploadUrl, item.tempFilePath, {}, formDataParams)]; case 1: _a = _b.sent(), data = _a.data.data, msg = _a.msg; if (msg === "success" && data.code === 0) { item.url = data.data.url; item.error = ""; } console.log("上传附件", data); return [3, 3]; case 2: item.error = ""; _b.label = 3; case 3: attachmentList[index] = item; residueNum -= 1; if (residueNum === 0) { reslove(attachmentList); } return [2]; } }); }); }); return [2]; }); }); })]; case 1: uploadedList = _a.sent(); wx.hideLoading(); console.log(uploadedList); this.setData({ attachmentList: uploadedList }); attachmentsValid = uploadedList.every(function (item) { return item.error === ""; }), attachmentsAllUploaded = uploadedList.every(function (item) { return item.url !== ""; }); if (!attachmentsValid) { return [2, { msg: "invalid", error: "文件可能包含非法内容", }]; } else if (!attachmentsAllUploaded) { return [2, { msg: "error", error: "上传文件出错,请重试", }]; } else { return [2, { msg: "successs", data: uploadedList.map(function (item) { return { name: item.name, size: item.size, type: item.type, url: item.url, format: item.format, }; }), }]; } return [2]; } }); }); }, }, });