Sfoglia il codice sorgente

上传图片改为七牛

maguohua 8 anni fa
parent
commit
f12460e2b0
2 ha cambiato i file con 54 aggiunte e 22 eliminazioni
  1. 1 0
      README.md
  2. 53 22
      prototype/baseComponent.js

+ 1 - 0
README.md

@@ -2,6 +2,7 @@
 
 此项目是前端项目 [vue2-elm](https://github.com/bailicangdu/vue2-elm) 的后台系统,保持和官网一致的API接口。并提供了对应的后台管理系统 [vue2-manage](https://github.com/bailicangdu/back-manage) 。
 
+__注:此项目只用做nodejs的学习,请不要用于商业用途,正常下单请选择饿了么官方客户端。__
 
 
 # 说明

+ 53 - 22
prototype/baseComponent.js

@@ -3,13 +3,18 @@ import Ids from '../models/ids'
 import formidable from 'formidable'
 import path from 'path'
 import fs from 'fs'
-import gm from 'gm'
+// import gm from 'gm'
+import qiniu from 'qiniu'
+qiniu.conf.ACCESS_KEY = 'Ep714TDrVhrhZzV2VJJxDYgGHBAX-KmU1xV1SQdS';
+qiniu.conf.SECRET_KEY = 'XNIW2dNffPBdaAhvm9dadBlJ-H6yyCTIJLxNM_N6';
+
 
 export default class BaseComponent {
 	constructor(){
 		this.idList = ['restaurant_id', 'food_id', 'order_id', 'user_id', 'address_id', 'cart_id', 'img_id', 'category_id', 'item_id', 'sku_id'];
 		this.imgTypeList = ['shop', 'food', 'avatar','default'];
 		this.uploadImg = this.uploadImg.bind(this)
+
 	}
 	async fetch(url = '', data = {}, type = 'GET', resType = 'JSON'){
 		type = type.toUpperCase();
@@ -99,32 +104,40 @@ export default class BaseComponent {
 				})
 				return 
 			}
-			const imgUrl = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
+			const imgName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
 			const extname = path.extname(files.file.name);
-			const repath = './public/img/' + type + '/' + imgUrl + extname;
+			const repath = './public/img/' + type + '/' + imgName + extname;
 			try{
+				const key = imgName + extname;
 				await fs.rename(files.file.path, repath);
-				gm(repath)
-				.resize(400, 400, '!')
-				.write(repath, async (err) => {
-					if(err){
-						console.log('改写图片尺寸失败');
-						fs.unlink(repath);
-						res.send({
-							status: 0,
-							type: 'ERROR_GET_SIZE',
-							message: '改写图片尺寸失败',
-						})
-					}else{
-						const path = repath.replace(/^\.\/public/, '');
-						res.send({
-							status: 1,
-							image_path: path
-						})
-					} 
+				const token = this.uptoken('node-elm', key);
+				const qiniuImg = await this.uploadFile(token.toString(), key, repath);
+				fs.unlink(repath);
+				res.send({
+					status: 1,
+					image_path: qiniuImg
 				})
+				// gm(repath)
+				// .resize(400, 400, '!')
+				// .write(repath, async (err) => {
+				// 	if(err){
+				// 		console.log('改写图片尺寸失败');
+				// 		fs.unlink(repath);
+				// 		res.send({
+				// 			status: 0,
+				// 			type: 'ERROR_GET_SIZE',
+				// 			message: '改写图片尺寸失败',
+				// 		})
+				// 	}else{
+				// 		const path = repath.replace(/^\.\/public/, '');
+				// 		res.send({
+				// 			status: 1,
+				// 			image_path: path
+				// 		})
+				// 	} 
+				// })
 			}catch(err){
-				console.log('改写图片路径失败');
+				console.log('改写图片路径失败', err);
 				fs.unlink(files.file.path)
 				res.send({
 					status: 0,
@@ -133,5 +146,23 @@ export default class BaseComponent {
 				})
 			}
 		});
+	}
+	uptoken(bucket, key){
+		var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
+  		return putPolicy.token();
+	}
+	uploadFile(uptoken, key, localFile){
+		return new Promise((resolve, reject) => {
+			var extra = new qiniu.io.PutExtra();
+		    qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) {
+			    if(!err) {  
+			    	resolve(ret.key)
+			    } else {
+			    	console.log('图片上传至七牛失败', err);
+			    	reject(err)
+			    }
+		  	});
+
+		})
 	}	
 }