Browse Source

修改上传文件方式

xiaoxin 2 years ago
parent
commit
3d5be4a18d
7 changed files with 156 additions and 108 deletions
  1. 48 0
      package-lock.json
  2. 1 0
      package.json
  3. 28 26
      pages/complaint/complaint.vue
  4. 27 33
      pages/evaluate/evaluate.vue
  5. 26 30
      pages/send/send.vue
  6. 20 19
      pages/set/set.vue
  7. 6 0
      util/cos.js

+ 48 - 0
package-lock.json

@@ -5,20 +5,68 @@
   "packages": {
     "": {
       "dependencies": {
+        "cos-wx-sdk-v5": "^1.4.13",
         "dayjs": "^1.11.10"
       }
     },
+    "node_modules/@xmldom/xmldom": {
+      "version": "0.8.10",
+      "resolved": "https://registry.npmmirror.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz",
+      "integrity": "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw==",
+      "engines": {
+        "node": ">=10.0.0"
+      }
+    },
+    "node_modules/cos-wx-sdk-v5": {
+      "version": "1.4.13",
+      "resolved": "https://registry.npmmirror.com/cos-wx-sdk-v5/-/cos-wx-sdk-v5-1.4.13.tgz",
+      "integrity": "sha512-cTrccf+9sfihCiRnj1BcHeYsQu7T6tXMrFVK2hnHPr/uJHv6Qui7hFHQyjngx0mJPsQEaq0jn7QmrwWL9nWwsA==",
+      "dependencies": {
+        "@xmldom/xmldom": "^0.8.6",
+        "mime": "^2.4.6"
+      }
+    },
     "node_modules/dayjs": {
       "version": "1.11.10",
       "resolved": "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.10.tgz",
       "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ=="
+    },
+    "node_modules/mime": {
+      "version": "2.6.0",
+      "resolved": "https://registry.npmmirror.com/mime/-/mime-2.6.0.tgz",
+      "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==",
+      "bin": {
+        "mime": "cli.js"
+      },
+      "engines": {
+        "node": ">=4.0.0"
+      }
     }
   },
   "dependencies": {
+    "@xmldom/xmldom": {
+      "version": "0.8.10",
+      "resolved": "https://registry.npmmirror.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz",
+      "integrity": "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw=="
+    },
+    "cos-wx-sdk-v5": {
+      "version": "1.4.13",
+      "resolved": "https://registry.npmmirror.com/cos-wx-sdk-v5/-/cos-wx-sdk-v5-1.4.13.tgz",
+      "integrity": "sha512-cTrccf+9sfihCiRnj1BcHeYsQu7T6tXMrFVK2hnHPr/uJHv6Qui7hFHQyjngx0mJPsQEaq0jn7QmrwWL9nWwsA==",
+      "requires": {
+        "@xmldom/xmldom": "^0.8.6",
+        "mime": "^2.4.6"
+      }
+    },
     "dayjs": {
       "version": "1.11.10",
       "resolved": "https://registry.npmmirror.com/dayjs/-/dayjs-1.11.10.tgz",
       "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ=="
+    },
+    "mime": {
+      "version": "2.6.0",
+      "resolved": "https://registry.npmmirror.com/mime/-/mime-2.6.0.tgz",
+      "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg=="
     }
   }
 }

+ 1 - 0
package.json

@@ -1,5 +1,6 @@
 {
   "dependencies": {
+    "cos-wx-sdk-v5": "^1.4.13",
     "dayjs": "^1.11.10"
   }
 }

+ 28 - 26
pages/complaint/complaint.vue

@@ -70,6 +70,7 @@
 <script>
 import getLessLimitSizeImage from '../../util/imageCompress.js'
 var dayjs = require('dayjs')
+import { cos } from '@/util/cos.js'
 export default {
 	data() {
 		return {
@@ -194,40 +195,41 @@ export default {
 		},
 		handleUploadMini(ele) {
 			// console.log(ele)
+
 			// 开始上传
 			uni.showLoading({
 				title: '上传中'
 			})
-			uni.uploadFile({
-				url: `https://www.jinganrenjiams.com/hotelReservation/file/cos/upload`,
-				filePath: ele.tempFilePath,
-				name: 'files',
-				success: (uploadFileRes) => {
-					// console.log(JSON.parse(uploadFileRes.data))
-					let temRes = JSON.parse(uploadFileRes.data)
-					if (temRes.code === 200 || temRes.code === 1) {
-						this.subImgList.push(temRes.data)
-						// console.log(this.subImgList)
+
+			let filePath = ele.tempFilePath
+			let Key = filePath.substr(filePath.lastIndexOf('/') + 1)
+
+			cos.postObject(
+				{
+					Bucket: 'jinganminsu-1320402385',
+					Region: 'ap-nanjing',
+					Key: Key,
+					FilePath: filePath,
+					onProgress: (info) => {
+						// console.log(info)
+					}
+				},
+				(err, data) => {
+					if (err) {
+						console.log('上传失败', err)
+					} else {
+						// console.log('上传成功', data)
+
+						uni.hideLoading()
+						let imgUrl = 'https://' + data.Location
+						this.subImgList.push(imgUrl)
 						this.viewImgList.push({
-							url: temRes.data,
+							url: imgUrl,
 							fileType: ele.fileType === 'image' ? 'image' : 'video'
 						})
-						// console.log(this.viewImgList)
-					} else {
-						uni.showToast({
-							title: temRes.message || '上传失败',
-							icon: 'none'
-						})
 					}
-					uni.hideLoading()
-				},
-				fail: () => {
-					uni.showToast({
-						title: '上传失败',
-						icon: 'error'
-					})
 				}
-			})
+			)
 		},
 		// 提交按钮回调
 		async handleSub() {
@@ -442,4 +444,4 @@ export default {
 		background-color: #096562;
 	}
 }
-</style>
+</style>

+ 27 - 33
pages/evaluate/evaluate.vue

@@ -105,6 +105,7 @@
 // 图片压缩方法
 import getLessLimitSizeImage from '@/util/imageCompress.js'
 var dayjs = require('dayjs')
+import { cos } from '@/util/cos.js'
 export default {
 	data() {
 		return {
@@ -330,48 +331,41 @@ export default {
 		},
 		handleUploadMini(ele) {
 			// console.log(ele)
+
 			// 开始上传
 			uni.showLoading({
 				title: '上传中'
 			})
-			uni.uploadFile({
-				url: `https://www.jinganrenjiams.com/hotelReservation/file/cos/upload`,
-				filePath: ele.tempFilePath,
-				// name: 'myFile',
-				name: 'files',
-				success: (uploadFileRes) => {
-					// console.log(JSON.parse(uploadFileRes.data))
-					let temRes = JSON.parse(uploadFileRes.data)
-					if (temRes.code === 200 || temRes.code === 1) {
-						this.subImgList.push(temRes.data)
-						// console.log(this.subImgList)
+
+			let filePath = ele.tempFilePath
+			let Key = filePath.substr(filePath.lastIndexOf('/') + 1)
+
+			cos.postObject(
+				{
+					Bucket: 'jinganminsu-1320402385',
+					Region: 'ap-nanjing',
+					Key: Key,
+					FilePath: filePath,
+					onProgress: (info) => {
+						// console.log(info)
+					}
+				},
+				(err, data) => {
+					if (err) {
+						console.log('上传失败', err)
+					} else {
+						// console.log('上传成功', data)
+
+						uni.hideLoading()
+						let imgUrl = 'https://' + data.Location
+						this.subImgList.push(imgUrl)
 						this.viewImgList.push({
-							url: temRes.data,
+							url: imgUrl,
 							fileType: ele.fileType === 'image' ? 'image' : 'video'
 						})
-						// console.log(this.viewImgList)
-					} else {
-						uni.showToast({
-							title: temRes.message || '上传失败',
-							icon: 'none'
-						})
 					}
-				},
-				fail: () => {
-					uni.showToast({
-						title: '上传失败',
-						icon: 'error'
-					})
-				},
-				complete: (res) => {
-					// console.log(res);
-					uni.hideLoading({
-						complete: (hide) => {
-							// console.log(hide);
-						}
-					})
 				}
-			})
+			)
 		},
 		// 评分改变回调
 		onChange(e, index) {

+ 26 - 30
pages/send/send.vue

@@ -107,6 +107,7 @@
 <script>
 // 图片压缩方法
 import getLessLimitSizeImage from '@/util/imageCompress.js'
+import { cos } from '@/util/cos.js'
 export default {
 	data() {
 		return {
@@ -227,41 +228,36 @@ export default {
 			uni.showLoading({
 				title: '上传中'
 			})
-			uni.uploadFile({
-				url: `https://www.jinganrenjiams.com/hotelReservation/file/cos/upload`,
-				filePath: ele,
-				name: 'files',
-				success: (uploadFileRes) => {
-					// console.log(JSON.parse(uploadFileRes.data))
-					let temRes = JSON.parse(uploadFileRes.data)
-					if (temRes.code === 200 || temRes.code === 1) {
+
+			let filePath = ele
+			let Key = filePath.substr(filePath.lastIndexOf('/') + 1)
+
+			cos.postObject(
+				{
+					Bucket: 'jinganminsu-1320402385',
+					Region: 'ap-nanjing',
+					Key: Key,
+					FilePath: filePath,
+					onProgress: (info) => {
+						// console.log(info)
+					}
+				},
+				(err, data) => {
+					if (err) {
+						console.log('上传失败', err)
+					} else {
+						// console.log('上传成功', data)
+
+						uni.hideLoading()
+						let imgUrl = 'https://' + data.Location
 						if (type === 1) {
-							this.subImgList.push(temRes.data)
+							this.subImgList.push(imgUrl)
 						} else if (type === 2) {
-							this.subVideo = temRes.data
+							this.subVideo = imgUrl
 						}
-
-						// console.log(this.subImgList)
-						// console.log(this.subVideo)
-					} else {
-						uni.showToast({
-							title: temRes.message || '上传失败',
-							icon: 'none'
-						})
 					}
-				},
-				fail: () => {
-					uni.showToast({
-						title: '上传失败',
-						icon: 'error'
-					})
-				},
-				complete: (res) => {
-					uni.hideLoading({
-						complete: (hide) => {}
-					})
 				}
-			})
+			)
 		},
 		// 删除图片回调
 		handleDelete(index) {

+ 20 - 19
pages/set/set.vue

@@ -32,6 +32,7 @@
 </template>
 
 <script>
+import { cos } from '@/util/cos.js'
 export default {
 	data() {
 		return {
@@ -71,19 +72,25 @@ export default {
 				sizeType: ['compressed'],
 				sourceType: ['album'], //从相册选择
 				success: (res) => {
-					uni.showLoading({
-						title: '上传中'
-					})
-					uni.uploadFile({
-						url: `https://www.jinganrenjiams.com/hotelReservation/file/cos/upload`,
-						filePath: res.tempFilePaths[0],
-						name: 'files',
-						success: async (uploadFileRes) => {
-							const res = JSON.parse(uploadFileRes.data)
+					let filePath = res.tempFiles[0].path
+					let Key = filePath.substr(filePath.lastIndexOf('/') + 1)
+					cos.postObject(
+						{
+							Bucket: 'jinganminsu-1320402385',
+							Region: 'ap-nanjing',
+							Key: Key,
+							FilePath: filePath,
+							onProgress: (info) => {
+								// console.log(info)
+							}
+						},
+						async (err, data) => {
+							if (err) {
+								console.log('上传失败', err)
+							} else {
+								// console.log('上传成功', data)
+								this.imgUrl = 'https://' + data.Location
 
-							if (res.code === 200 || res.code === 1) {
-								uni.hideLoading()
-								this.imgUrl = res.data
 								const result = await this.$myRequest({
 									url: '/mhotel/ampupdateUserInfo.action',
 									data: {
@@ -103,14 +110,8 @@ export default {
 									}, 1500)
 								}
 							}
-						},
-						fail: () => {
-							uni.showToast({
-								title: '上传失败',
-								icon: 'error'
-							})
 						}
-					})
+					)
 				}
 			})
 		},

+ 6 - 0
util/cos.js

@@ -0,0 +1,6 @@
+let COS = require('cos-wx-sdk-v5')
+export const cos = new COS({
+	SecretId: 'AKIDJSP9gz469yZv77tQjotsgHguvjIgtvkL',
+	SecretKey: 'b3MVRD1g8RYS9g195oMkvukQvDKZfbOz',
+	SimpleUploadMethod: 'putObject'
+})