tanyanfei 7 місяців тому
батько
коміт
b3b10a6e24
4 змінених файлів з 63 додано та 33 видалено
  1. 0 2
      pages/index/index.js
  2. 61 30
      pages/upload/upload.js
  3. 1 1
      pages/upload/upload.wxml
  4. 1 0
      utils/api.js

+ 0 - 2
pages/index/index.js

@@ -62,7 +62,6 @@ Page({
   getIndexData:function(){
     
     $api.getChampionlList({isbanner:1}).then(res=>{
-      console.log(res)
       this.setData({
         banners:res.data.data.list
       })
@@ -104,7 +103,6 @@ Page({
             item.total=total.toFixed(2)
           }
         })
-        console.log(2333,days)
         this.setData({
           days:weeks.reverse()
         })

+ 61 - 30
pages/upload/upload.js

@@ -1,6 +1,7 @@
 // pages/upload/upload.js
 const app = getApp()
 const $api = require('../../utils/api.js').API;
+const baseURL = 'https://test.hunanwanzhu.com';
 Page({
 
   /**
@@ -199,43 +200,73 @@ Page({
         is_markt: Number(e.detail.value)
       })
   },
-  uploadImg() {
-    var today_stock_img = this.data.today_stock_img;
-    wx.chooseImage({
-      success: res => {
+
+   // oss验签上传图片
+   upload() {
+    const that = this
+    var today_stock_img = that.data.today_stock_img;
+    wx.chooseMedia({
+      mediaType: ['image'], // 只能上传图片 还有个属性是可以上传视频
+      success(res) {
         wx.showLoading({
-          title: '正在上传',
+          title: '上传中',
+          mask: true
         })
-    
-        const tempFilePaths = res.tempFiles
-        for (let i = 0; i < tempFilePaths.length; i++) {
-          $api.upload(tempFilePaths[i].path, 'file').then(res => {
-            wx.hideLoading()
-            if (res.code == 0) {
-              console.log(res.data)
-              today_stock_img.push(res.data.url)
-              this.setData({
-                today_stock_img: today_stock_img
-              })
-              console.log(this.data.today_stock_img,22222222222)
-              if (i==0 && today_stock_img.length == 1){
-                  this.loadOcr(tempFilePaths[i].path,res.data.url)
-              }
-            } else {
-              wx.showToast({
-                title: '上传失败',
-              })
-            }
+        let imgs = []
+        let valueArr = []
+        // 需要oss 验签  此处是调取验签的接口  
+        $api.getOssSign(res).then((r) => {
+          wx.hideLoading()
+          const arr = res.tempFiles
+          // arr 是为了获取上传图片反的数组
+          arr.forEach((e) => {
+            return imgs.push(e.tempFilePath)
           })
-            .catch(err => {
-              wx.hideLoading()
+          for (var i = 0; i < imgs.length; i++) {
+            let only = imgs[i].split('.')
+            valueArr.push(
+              only[only.length - 1]
+            )
+            let filename = parseInt(Math.random() * 1000000 + new Date().getTime()) + '.' + valueArr[valueArr.length - 1]
+            let httpData = {
+              Key: r.data.dir + filename,
+              OSSAccessKeyId: r.data.AccessKeyID,
+              Policy: r.data.policy_encode,
+              Signature: r.data.signature,
+            }
+            wx.uploadFile({
+              url: baseURL+'/api/wx/uploadfile',
+              filePath: imgs[i],
+              formData: httpData,
+              fileType: 'image',
+              name: 'file',
+              timeout: 15000,
+              header: {
+                'Content-Type': 'application/x-www-form-urlencoded',
+              },
+              success: (res) => {
+                if (res.statusCode == 200) {
+                  var data=JSON.parse(res.data)
+                  today_stock_img.push(data.data.url)
+                  that.setData({
+                    today_stock_img: today_stock_img
+                  })
+                } else {
+                  // 返回错误信息
+                  console.log('error')
+                }
+              },
+              fail: function (res) {
+                console.log(res);
+              }
             })
-
-        }
-
+          }
+        })
       }
     })
+
   },
+
   loadOcr(path,url){
     var _this = this
 

+ 1 - 1
pages/upload/upload.wxml

@@ -15,7 +15,7 @@
         </view>
         
     </view>
-    <view  class="upload" bindtap="uploadImg">
+    <view  class="upload" bindtap="upload">
           <image mode="widthFix" src="../../images/img.png"></image>
             上传图片
     </view>

+ 1 - 0
utils/api.js

@@ -163,6 +163,7 @@ const API = {
   getChampionlList: (data) => request(GET, `/api/wx/v2/champion/article/list`, data),
   upload: (filePath, name) => uploadFile(`/api/wx/uploadfile`, filePath, name),
   uploadAvatar: (filePath, name) => uploadFile(`/api/wx/uploadavatar`, filePath, name),
+  getOssSign: (data) => request(GET, `/api/wx/oss2/uploadfile/sign`, data),
   updateStyle: (data) => request(PUT, `/api/wx/v2/mine/style`, data),
   getMyStyle: (data) => request(GET, `/api/wx/v2/mine/style`, data),
   getMyFollow: (data) => request(GET, `/api/wx/v2/user/follow/list`, data),