123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import api from '../../api/api.js'
- let waitBindings = []; //等待绑定的附件
- Component({
- properties: {
- source: {
- value: 'media',
- type: String
- },
- parentid: {
- type: String,
- value: wx.getStorageSync('siteP').appfolderid
- },
- originFiles: Array
- },
- data: {
- gridConfig: {
- column: 5,
- width: 120,
- height: 120,
- },
- config: {
- count: 1,
- }
- },
- methods: {
- handleAdd(file) {
- this.toSetFileData(file.detail.files.map(e => {
- e.status = 'loading'
- return e
- }))
- },
- toSetFileData(files) {
- let that = this;
- this.setData({
- originFiles: this.data.originFiles.concat(files)
- });
- for (let i = 0; i < files.length; i++) {
- // 初始化数据
- let params = this.requestType(files[i]);
- params.content.filename = params.content.filename ? params.content.filename : `${Date.now()}.${params.content.filetype}`;
- //发送请求
- wx.getFileSystemManager().readFile({
- filePath: files[i].url,
- success: ({
- data
- }) => api._post(params).then(res => res.code == '1' && that.uploadFile(res.data, data)),
- fail: console.error
- })
- }
- },
- /* 上传成功反馈 */
- uploadFile(res, data) {
- var that = this;
- wx.request({
- url: res.uploadurl,
- method: "PUT",
- data: data,
- header: {
- 'content-type': 'application/octet-stream'
- },
- success(a) {
- api._post({
- "classname": "system.attachment.huawei.OBS",
- "method": "uploadSuccess",
- "content": {
- "serialfilename": res.serialfilename
- }
- }).then(rs => {
- console.log("上传附件反馈", rs)
- waitBindings.push(rs.data.attachmentids[0] || "");
- console.log("res", res)
- console.log("that.data.originFiles", that.data.originFiles)
- let data = that.data.originFiles.find(e => e.name === res.filename);
- if (data) {
- data.status = '';
- data.attachmentids = rs.data.attachmentids[0]
- }
- that.setData({
- originFiles: that.data.originFiles
- })
- }).catch(err => {
- console.log(err)
- })
- }
- })
- },
- async handleRemove(data) {
- let file = data.detail.file;
- if (file.linksid) {
- const res = await api._post({
- "classname": "system.attachment.Attachment",
- "method": "deleteFileLink",
- "content": {
- "linksids": [file.linksid]
- }
- })
- if (res.code == '1') this.setData({
- originFiles: this.data.originFiles.filter(e => e.linksid !== file.linksid),
- })
- } else {
- waitBindings = waitBindings.filter(e => e !== file.attachmentids);
- this.setData({
- originFiles: this.data.originFiles.filter(e => e.attachmentids !== file.attachmentids),
- })
- }
- },
- /* 请求类型 */
- requestType(file) {
- var index = file.url.lastIndexOf(".");
- var ext = file.url.substr(index + 1);
- //文件名称
- return {
- "classname": "system.attachment.huawei.OBS",
- "method": "getFileName",
- "content": {
- "filename": file.name || Date.now() + '.' + ext,
- "filetype": ext,
- "parentid": this.data.parentid
- }
- }
- },
- handleBind() {
- let arr = JSON.parse(JSON.stringify(waitBindings));
- waitBindings = [];
- return arr;
- }
- }
- })
|