Browse Source

上传修改

xiaohaizhao 2 years ago
parent
commit
1b7d0c1182

+ 72 - 90
Eservice/components/autoUpload/upload.js

@@ -1,5 +1,5 @@
 import api from '../../api/api.js'
-
+let waitBindings = []; //等待绑定的附件
 Component({
   /**
    * 组件的属性列表
@@ -19,47 +19,91 @@ Component({
       value: wx.getStorageSync('siteP').appfolderid
     }
   },
-
-  /**
-   * 组件的初始数据
-   */
   data: {
     originFiles: [],
-    filelist: [],
-    attachmentids: [],
     gridConfig: {
       column: 5,
       width: 120,
       height: 120,
-    },
-    config: {
-      count: 1,
-    },
-    timer: null,
-    uploadCount: 0
+    }
   },
   lifetimes: {
     attached() {
       setTimeout(() => {
         this.fileData()
       }, 1000);
-
     }
   },
-  /**
-   * 组件的方法列表
-   */
   methods: {
     handleAdd(file) {
-      let files = this.data.originFiles.concat(file.detail.files.map(e => {
+      this.toSetFileData(file.detail.files.map(e => {
         e.status = 'loading'
         return e
-      }));
+      }))
+    },
+    toSetFileData(files) {
       this.setData({
-        uploadCount: files.length,
-        originFiles: files
+        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.msg == "成功") {
+                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)
+          })
+        }
       })
-      this.toSetFileData()
     },
     handleClick() {
       let that = this
@@ -92,37 +136,6 @@ Component({
       }
       this.deleteFile(file)
     },
-    toSetFileData() {
-
-      let files = this.data.originFiles
-
-      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 => {
-              if (res.msg == "成功") {
-                that.uploadFile(res.data, fileData)
-              }
-            })
-          },
-          fail: console.error
-        })
-      }
-      this.data.timer = setInterval(e => {
-        if (this.data.uploadCount === 0) {
-          clearInterval(this.data.timer)
-          this.filebindData()
-        }
-      })
-    },
     /* 请求类型 */
     requestType(file) {
       var index = file.url.lastIndexOf(".");
@@ -138,49 +151,17 @@ Component({
         }
       }
     },
-    /* 上传成功反馈 */
-    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 => {
-            that.data.attachmentids.push(rs.data.attachmentids[0])
-            that.setData({
-              uploadCount: that.data.uploadCount - 1,
-              originFiles: that.data.originFiles.map(e => {
-                if (e.name === res.filename) {
-                  e.status = '';
-                  e.attachmentids = rs.data.attachmentids[0]
-                }
-                return e
-              })
-            })
-          }).catch(err => {
-            console.log(err)
-          })
-        }
-      })
-    },
     async filebindData() {
-      this.data.bindData.attachmentids = this.data.attachmentids
+      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.msg != '成功') return;
       this.fileData()
+      waitBindings = [];
     },
     async fileData() {
       const res = await api._post({
@@ -188,9 +169,10 @@ Component({
         "method": "queryFileLink",
         "content": this.data.bindData
       })
+      console.log('获取文件', res)
       this.setData({
         originFiles: res.data
-      })
+      });
     },
     async deleteFile(data) {
       let item = data.currentTarget.dataset.item

+ 15 - 43
Eservice/components/form/form.js

@@ -10,11 +10,11 @@ Component({
     options: {
         multipleSlots: true // 复数插槽: 是
     },
-    
+
     properties: {
-        hideBtn:{
-            value:false,
-            type:Boolean
+        hideBtn: {
+            value: false,
+            type: Boolean
         },
         formLayoutData: {
             value: {},
@@ -148,37 +148,22 @@ Component({
         },
         validate(fn) {
             let arr = this.data.formLayoutData.formInfo.map((e, index) => {
-                // console.log(e)
                 if (!e.force) return e
-                // console.log(e)
-                if (e.label === '关联订单') {
-                    console.log(e.force && e.inputValue !== '')
-                }
+                if (e.label === '关联订单') console.log(e.force && e.inputValue !== '')
                 if (e.force && e.inputValue !== '') {
-
                     if (e.role.formatter) {
-
                         let regStatus = e.role.formatter(e.inputValue)
-
                         this.setErrorMsg(regStatus, index)
-
                     } else {
-
                         this.setErrorMsg('', index)
-
                     }
-
                 } else {
-                    console.log(e.label)
                     this.setErrorMsg(e.label + '不能为空', index)
                 }
                 return e
             })
-            console.log(arr)
             let isValidate = !arr.some(item => item.errorMsg && item.errorMsg !== '')
-            console.log(isValidate,'--')
             fn(isValidate)
-
         },
         setErrorMsg(msg, index) {
 
@@ -202,32 +187,19 @@ Component({
             that.setData({
                 disabledStatus: false
             })
-
-
-            that.compData = that.selectComponent("#upload")
-
-            that.compData.toSetFileData().then(e => {
-                that.setData({
-                    disabledStatus: false
-                })
-                data.forEach(e => {
-                    if (e.type === 'upload') {
-                        e.inputValue = that.compData.data.attachmentids
-                    }
-
+            data.forEach(e => {
+                if (e.type === 'upload') {
+                    obj[e.id] = that.selectComponent("#upload").handleBind();
+                } else if (e.id == "provinceArr") {
+                    let arr = obj.provinceArr.split(",");
+                    obj.province = arr[0] || ""
+                    obj.city = arr[1] || ""
+                    obj.county = arr[2] || ""
+                } else {
                     obj[e.id] = e.inputValue
-
-                })
-
-                if (obj.provinceArr) {
-                    let arr = obj.provinceArr.split(",")
-                    obj.province = arr[0]
-                    obj.city = arr[1]
-                    obj.county = arr[2]
                 }
-                that.triggerEvent("click", obj);
             })
-
+            that.triggerEvent("click", obj);
         },
         formSubmit() {
             var that = this

+ 23 - 71
Eservice/components/form/form.wxml

@@ -1,13 +1,13 @@
 <!--components/form/form.wxml-->
 <wxs module="format">
-	// wxs中不能写ES6的语法: 例如箭头函数
-	function isHide(hide,arr) {
+  // wxs中不能写ES6的语法: 例如箭头函数
+  function isHide(hide, arr) {
     var rs = true
-    arr.forEach(function(e){
-      hide.forEach(function(h){
+    arr.forEach(function (e) {
+      hide.forEach(function (h) {
         if (e.id === h.id) {
           e.force = false
-          h.value.forEach(function(v){
+          h.value.forEach(function (v) {
             if (e.inputValue === v) {
               rs = false
             }
@@ -15,42 +15,29 @@
         }
       })
     })
-		return rs
-	}
+    return rs
+  }
 
-	// 需要导出函数, 导出只能使用CommonJs规范
-	module.exports = {
-		// 对象中也不能使用对象的增强写法
-		isHide: isHide
-	}
+  // 需要导出函数, 导出只能使用CommonJs规范
+  module.exports = {
+    // 对象中也不能使用对象的增强写法
+    isHide: isHide
+  }
 </wxs>
 
 <view>
   <view wx:for="{{formLayoutData.formInfo}}" wx:key="{{index}}">
     <t-cell required="{{item.force}}" wx:if="{{item.type == 'cell'}}" title="{{item.label}}" note="{{item.inputValue}}" url="{{item.url}}" hover>
-      
+
     </t-cell>
     <!-- 输入 -->
-    <t-input 
-      wx:if="{{item.type == 'text'}}" 
-      value="{{item.inputValue}}"
-      data-value="{{item.id}}" 
-      data-index="{{index}}" 
-      layout="horizontal" 
-      status="error" 
-      data-item="{{item}}" 
-      tips="{{item.errorMsg}}" 
-      placeholder="{{item.placeholder}}"
-      align="right"
-      bind:blur="onBlur" 
-      bind:clear="onClear"
-      clearable>
+    <t-input wx:if="{{item.type == 'text'}}" value="{{item.inputValue}}" data-value="{{item.id}}" data-index="{{index}}" layout="horizontal" status="error" data-item="{{item}}" tips="{{item.errorMsg}}" placeholder="{{item.placeholder}}" align="right" bind:blur="onBlur" bind:clear="onClear" clearable>
       <view slot="label">
         {{item.label}} <text wx:if="{{item.force}}" style="color:#e34d59">*</text>
       </view>
     </t-input>
     <!-- 选择 -->
-    <t-cell wx:if="{{item.type == 'picker'}}" required="{{item.force}}" note="{{item.inputValue == 1?'是':item.inputValue == 0?'请选择':item.inputValue}}" class="block" title="{{item.label}}" data-index="{{index}}" data-item="{{item}}" arrow hover bind:click="onPicker" >
+    <t-cell wx:if="{{item.type == 'picker'}}" required="{{item.force}}" note="{{item.inputValue >= 1?'是':item.inputValue >= 0? '否' :(item.inputValue||'请选择')}}" class="block" title="{{item.label}}" data-index="{{index}}" data-item="{{item}}" arrow hover bind:click="onPicker">
       <text slot="description" class="t-class-description">{{item.errorMsg?item.errorMsg:''}}</text>
     </t-cell>
     <!-- 上传 -->
@@ -59,27 +46,15 @@
         附件上传
       </view>
       <slot style="flex:1" name="attinfos"></slot>
-
-      <upload id="upload"></upload>
+      <upload id="upload" originFiles='{{item.originFiles||[]}}' />
     </view>
     <!-- 时间选择 -->
-    <t-cell
-      wx:if="{{item.type == 'datepicker'}}"
-      title="{{item.label}}"
-      hover
-      note="{{item.inputValue}}"
-      arrow
-      data-item="{{item}}"
-      data-index="{{index}}"
-      data-mode="date"
-      bindtap="showDatePicker"
-      t-class="pannel-item"
-    />
+    <t-cell wx:if="{{item.type == 'datepicker'}}" title="{{item.label}}" hover note="{{item.inputValue}}" arrow data-item="{{item}}" data-index="{{index}}" data-mode="date" bindtap="showDatePicker" t-class="pannel-item" />
     <!-- 选择省市县 -->
     <t-cell required="{{item.force}}" wx:if="{{item.type == 'cascader'}}" title="{{item.label}}" data-index="{{index}}" note="{{item.inputValue}}" bind:click="showCascader">
       <text slot="description" class="t-class-description">{{item.errorMsg?item.errorMsg:''}}</text>
     </t-cell>
-    
+
     <!-- 跳转链接 -->
     <t-cell required="{{item.force}}" wx:if="{{item.type == 'link' && format.isHide(item.hide,formLayoutData.formInfo)}}" title="{{item.label}}" note="{{item.inputValue}}" url="{{item.url}}" hover arrow>
       <text slot="description" class="t-class-description">{{item.errorMsg?item.errorMsg:''}}</text>
@@ -87,34 +62,11 @@
 
   </view>
   <view wx:if="{{!hideBtn}}" style="padding:30px">
-    <t-button  theme="primary" size="medium" disabled="{{disabledStatus}}" bind:tap="formSubmit"  block>提 交</t-button>
+    <t-button theme="primary" size="medium" disabled="{{disabledStatus}}" bind:tap="formSubmit" block>提 交</t-button>
   </view>
-  <t-picker
-    visible="{{pickerVisible}}"
-    data-key="id"
-    title="选择{{actPicker.label}}"
-    cancelBtn="取消"
-    confirmBtn="确认"
-    bindconfirm="onPickerConfirm"
-    bindchange="onPickerChange"
-  >
+  <t-picker visible="{{pickerVisible}}" data-key="id" title="选择{{actPicker.label}}" cancelBtn="取消" confirmBtn="确认" bindconfirm="onPickerConfirm" bindchange="onPickerChange">
     <t-picker-item options="{{actPicker.data}}"></t-picker-item>
   </t-picker>
-  <t-date-time-picker
-    title="选择日期"
-    visible="{{dateVisible}}"
-    mode="date"
-    value="{{timePickerValue}}"
-    format="YYYY-MM-DD"
-    bindchange="onTimePickerConfirm"
-  />
-  <t-cascader
-    visible="{{cascaderVisible}}"
-    theme="step"
-    options="{{options}}"
-    value=''
-    title="请选择地址"
-    bind:change="onChange"
-  ></t-cascader>
-</view>
-
+  <t-date-time-picker title="选择日期" visible="{{dateVisible}}" mode="date" value="{{timePickerValue}}" format="YYYY-MM-DD" bindchange="onTimePickerConfirm" />
+  <t-cascader visible="{{cascaderVisible}}" theme="step" options="{{options}}" value='' title="请选择地址" bind:change="onChange"></t-cascader>
+</view>

+ 22 - 34
Eservice/components/imageViewer/index.js

@@ -1,49 +1,38 @@
-// components/imageViewer/index.js
 import api from '../../api/api'
 Component({
-  /**
-   * 组件的属性列表
-   */
   properties: {
-    list:{
-      value:'',
-      type:Array
+    list: {
+      value: '',
+      type: Array
     },
-    isdelete:{
-      type:Boolean,
-      value:true
+    isdelete: {
+      type: Boolean,
+      value: true
     },
   },
-
-  /**
-   * 组件的初始数据
-   */
   data: {
     visible: false,
     showIndex: false,
     closeBtn: false,
     deleteBtn: false,
-    images2:[],
+    images2: [],
     gridConfig: {
       column: 5,
       width: 120,
       height: 120,
     },
   },
-
-  /**
-   * 组件的方法列表
-   */
-  lifetimes:{
-    attached () {
-      setTimeout(() => {
-        this.setData({
-          images2:this.data.list.map(e=>{
-            return {url:e.url,type:this.validateImageType(e.postfix),linksid:e.linksid}
-          })
+  observers: {
+    "list": function (list) {
+      this.setData({
+        images2: list.map(e => {
+          return {
+            url: e.url,
+            type: this.validateImageType(e.postfix),
+            linksid: e.linksid
+          }
         })
-        console.log(this.data.images2)
-      }, 1000);
+      })
     }
   },
   methods: {
@@ -58,7 +47,7 @@ Component({
         return 'file'
       }
     },
-    async deleteFile (data) {
+    async deleteFile(data) {
       const res = await api._post({
         "classname": "system.attachment.Attachment",
         "method": "deleteFileLink",
@@ -68,17 +57,16 @@ Component({
       })
       if (res.code == 1) {
         this.setData({
-          images2:this.data.images2.filter(e=>{
+          images2: this.data.images2.filter(e => {
             return e.linksid !== data.detail.file.linksid
           })
         })
-      }
-      else {
+      } else {
         wx.showToast({
           title: res.data,
-          icon:'none'
+          icon: 'none'
         })
       }
     }
   }
-})
+})

+ 62 - 78
Eservice/components/upload/upload.js

@@ -1,27 +1,18 @@
 import api from '../../api/api.js'
-
+let waitBindings = []; //等待绑定的附件
 Component({
-  /**
-   * 组件的属性列表
-   */
   properties: {
     source: {
       value: 'media',
       type: String
     },
-    /* 文件夹ID */
     parentid: {
       type: String,
       value: wx.getStorageSync('siteP').appfolderid
-    }
+    },
+    originFiles: Array
   },
-
-  /**
-   * 组件的初始数据
-   */
   data: {
-    originFiles: [],
-    attachmentids: [],
     gridConfig: {
       column: 5,
       width: 120,
@@ -29,78 +20,33 @@ Component({
     },
     config: {
       count: 1,
-    },
-    timer: null,
-    uploadCount: 0
+    }
   },
-
-  /**
-   * 组件的方法列表
-   */
   methods: {
     handleAdd(file) {
-      let files = this.data.originFiles.concat(file.detail.files.map(e => {
+      this.toSetFileData(file.detail.files.map(e => {
         e.status = 'loading'
         return e
-      }));
-      this.setData({
-        uploadCount: files.length,
-        originFiles: files
-      })
-      this.toSetFileData()
+      }))
     },
-    handleRemove(data) {
-      let file = data.detail.file;
+    toSetFileData(files) {
+      let that = this;
       this.setData({
-        originFiles: this.data.originFiles.filter(e => e.name !== file.name),
-        attachmentids: this.data.attachmentids.filter(e => e !== file.attachmentids)
-      })
-    },
-    toSetFileData() {
-      let files = this.data.originFiles
+        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}`;
+        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: result => {
-            //返回临时文件路径
-            const fileData = result.data;
-            api._post(data).then(res => {
-              if (res.msg == "成功") {
-                that.uploadFile(res.data, fileData)
-              }
-            })
-          },
+          success: ({
+            data
+          }) => api._post(params).then(res => res.msg == "成功" && that.uploadFile(res.data, data)),
           fail: console.error
         })
       }
-      return new Promise((reslove, reject) => {
-        this.data.timer = setInterval(e => {
-          if (this.data.uploadCount === 0) {
-            clearInterval(this.data.timer)
-            reslove()
-          }
-        }, 1000)
-      })
-    },
-    /* 请求类型 */
-    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": this.data.parentid
-        }
-      }
     },
     /* 上传成功反馈 */
     uploadFile(res, data) {
@@ -121,16 +67,14 @@ Component({
             }
           }).then(rs => {
             console.log("上传附件反馈", rs)
-            that.data.attachmentids.push(rs.data.attachmentids[0])
+            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]
+            }
             that.setData({
-              uploadCount: that.data.uploadCount - 1,
-              originFiles: that.data.originFiles.map(e => {
-                if (e.name === res.filename) {
-                  e.status = '';
-                  e.attachmentids = rs.data.attachmentids[0]
-                }
-                return e
-              })
+              originFiles: that.data.originFiles
             })
           }).catch(err => {
             console.log(err)
@@ -138,5 +82,45 @@ Component({
         }
       })
     },
+    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.msg == '成功') 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,
+          "filetype": ext,
+          "parentid": this.data.parentid
+        }
+      }
+    },
+    handleBind() {
+      let arr = JSON.parse(JSON.stringify(waitBindings));
+      waitBindings = [];
+      return arr;
+    }
   }
 })