123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- const _Http = getApp().globalData.http;
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- /* 最大上传数量 */
- maxCount: {
- type: String,
- value: 9
- },
- /* 文件上传类型限制 all media image file video */
- accept: {
- type: String,
- value: "all"
- }
- },
- /**
- * 组件的初始数据
- */
- data: {
- fileList: []
- },
- /**
- * 组件的方法列表
- */
- methods: {
- /* 上传文件 */
- afterRead(event) {
- for (let i = 0; i < event.detail.file.length; i++) {
- // 初始化数据
- let that = this,
- data = this.requestType(event.detail.file[i]);
- //发送请求
- wx.getFileSystemManager().readFile({
- filePath: event.detail.file[i].url,
- success: result => {
- //返回临时文件路径
- const fileData = result.data;
- _Http.basic(data).then(res => {
- console.log("res", res)
- if (res.msg == "成功") {
- that.uploadFile(res.data, fileData)
- } else {
- wx.showToast({
- title: `${fileData.filename}.${fileData.serialfilename}`,
- icon: "none"
- })
- }
- })
- },
- fail: console.error
- })
- }
- },
- /* 请求类型 */
- 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,
- "filetype": ext,
- "parentid": 2
- }
- }
- },
- /* 上传成功反馈 */
- uploadFile(res, data) {
- console.log('asd', res, data)
- var that = this
- wx.request({
- url: res.uploadurl,
- method: "PUT",
- data: data,
- header: {
- 'content-type': 'application/octet-stream'
- },
- success(a) {
- console.log("a", a)
- _Http.basic({
- "classname": "system.attachment.huawei.OBS",
- "method": "uploadSuccess",
- "content": {
- "serialfilename": res.serialfilename
- }
- }).then(s => {
- return console.log(s);
- if (res.msg != "成功") return;
- let fileList = that.data.fileList;
- for (let i = 0; i < res.data.length; i++) {
- let arr = {
- url: res.data[i].fobsurl,
- ownerid: res.data[i].ownerid,
- tattachmentid: res.data[i].tattachmentid,
- ownertable: res.data[i].ownertable,
- fdocument: res.data[i].fdocument,
- }
- fileList.push(arr)
- };
- // 用户头像 先删除 在修改
- if (that.data.upType == "userImage" && fileList.length >= 2) {
- that.dleeteDealWith(0);
- }
- that.setData({
- fileList
- });
- /* 返回数据 */
- that.triggerEvent("imageChange", {
- fileList
- })
- }).catch(err => {
- console.log(err)
- })
- }
- })
- },
- }
- })
|