123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import api from '../../api/api.js'
- let waitBindings = []; //等待绑定的附件
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- source: {
- value: 'media',
- type: String
- },
- bindData: {
- value: {},
- type: Object
- },
- /* 文件夹ID */
- parentid: {
- type: String,
- value: wx.getStorageSync('siteP').appfolderid
- }
- },
- data: {
- originFiles: [],
- gridConfig: {
- column: 5,
- width: 120,
- height: 120,
- }
- },
- lifetimes: {
- attached() {
- setTimeout(() => {
- this.fileData()
- getApp().globalData.Language.getLanguagePackage(this)
- }, 1000);
- }
- },
- methods: {
- handleAdd(file) {
- this.toSetFileData(file.detail.files.map(e => {
- e.status = 'loading'
- return e
- }))
- },
- toSetFileData(files) {
- this.setData({
- originFiles: this.data.originFiles.concat(files)
- })
- for (let i = 0; i < files.length; i++) {
- // 初始化数据
- let that = this,
- data = this.requestType(files[i]);
- data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
- //发送请求
- wx.getFileSystemManager().readFile({
- filePath: files[i].url,
- success: result => {
- //返回临时文件路径
- const fileData = result.data;
- api._post(data).then(res => {
- console.log("文件上传", res)
- if (res.code == '1') {
- that.uploadFile(res.data, fileData, files.length);
- }
- })
- },
- fail: console.error
- });
- }
- },
- /* 上传成功反馈 */
- uploadFile(res, data, count) {
- 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] || "");
- let data = that.data.originFiles.find(e => e.name === res.filename);
- if (data) {
- data.status = '';
- data.attachmentids = rs.data.attachmentids[0]
- }
- if (waitBindings.length == count) {
- that.filebindData();
- that.setData({
- originFiles: that.data.originFiles
- })
- }
- }).catch(err => {
- console.log(err)
- })
- }
- })
- },
- handleClick() {
- let that = this
- wx.chooseMessageFile({
- count: 10,
- type: 'file',
- success(res) {
- let arr = res.tempFiles.map(e => {
- return {
- name: e.name,
- url: e.path,
- type: e.type,
- }
- })
- that.handleAdd({
- detail: {
- files: arr
- }
- })
- }
- })
- },
- handleRemove(data) {
- let file = {
- currentTarget: {
- dataset: {
- item: data.detail.file
- }
- }
- }
- this.deleteFile(file)
- },
- /* 请求类型 */
- 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
- }
- }
- },
- async filebindData() {
- this.data.bindData.attachmentids = JSON.parse(JSON.stringify(waitBindings));
- const res = await api._post({
- "classname": "system.attachment.Attachment",
- "method": "createFileLink",
- "content": this.data.bindData
- })
- console.log("绑定", res)
- if (res.code != '1') return;
- this.fileData()
- waitBindings = [];
- },
- async fileData() {
- const res = await api._post({
- "classname": "system.attachment.Attachment",
- "method": "queryFileLink",
- "content": this.data.bindData
- })
- console.log('获取文件', res)
- this.setData({
- originFiles: res.data
- });
- },
- async deleteFile(data) {
- let item = data.currentTarget.dataset.item
- const res = await api._post({
- "classname": "system.attachment.Attachment",
- "method": "deleteFileLink",
- "content": {
- "linksids": [item.linksid]
- }
- })
- this.fileData()
- }
- }
- })
|