Browse Source

新增上传头像接口

maguohua 8 năm trước cách đây
mục cha
commit
38fdf0513f

+ 1 - 1
README.md

@@ -48,7 +48,7 @@ npm run dev
 - [x] 定位功能
 - [x] 城市列表
 - [x] 搜索地址
-- [ ] 添加商铺
+- [x] 添加商铺
 - [ ] 添加食品
 - [ ] 搜索美食,餐馆
 - [ ] 餐馆排序

+ 16 - 2
controller/shopping/shop.js

@@ -8,6 +8,7 @@ class Shop extends AddressComponent{
 	constructor(){
 		super()
 		this.addShop = this.addShop.bind(this);
+		this.uploadShopImg = this.uploadShopImg.bind(this);
 	}
 	async addShop(req, res, next){
 		let shopId;
@@ -21,7 +22,6 @@ class Shop extends AddressComponent{
 			return
 		}
 		const form = new formidable.IncomingForm();
-		form.uploadDir = './img/shop';
 		form.parse(req, async (err, fields, files) => {
 			try{
 				if (!fields.name) {
@@ -138,11 +138,25 @@ class Shop extends AddressComponent{
 					name: "开发票"
 				})
 			}
-			console.log(newShop)
 			res.send(newShop)
 			return
 		})
 	}
+	async uploadShopImg(req, res, next){
+		try{
+			let path = await this.uploadImg(req, 'shop');
+			res.send({
+				status: 1,
+				image_path: path
+			})
+		}catch(err){
+			res.send({
+				type: 'ERROR_PATH',
+				message: '上传头像失败',
+				status: 0,
+			})
+		}
+	}
 }
 
 export default new Shop()

+ 0 - 1
controller/v1/cities.js

@@ -57,7 +57,6 @@ class CityHandle extends AddressComponent{
 		let cityInfo;
 		try{
 			cityInfo = await this.guessPosition(req);
-			console.log(cityInfo)
 		}catch(err){
 			console.error()
 		}

+ 2 - 0
models/ids.js

@@ -9,6 +9,7 @@ const idsSchema = new mongoose.Schema({
 	userId: Number,
 	addressId: Number,
 	cartId: Number,
+	imgId: Number,
 });
 
 const Ids = mongoose.model('Ids', idsSchema);
@@ -22,6 +23,7 @@ Ids.findOne((err, data) => {
 			userId: 0,
 			addressId: 0,
 			cartId: 0,
+			imgId: 0,
 		});
 		newIds.save();
 	}

+ 48 - 0
prototype/baseComponent.js

@@ -1,5 +1,9 @@
 import fetch from 'node-fetch';
 import Ids from '../models/ids'
+import formidable from 'formidable'
+import path from 'path'
+import fs from 'fs'
+import gm from 'gm'
 
 export default class BaseComponent {
 	constructor(){
@@ -49,6 +53,12 @@ export default class BaseComponent {
 	}
 	//获取id列表
 	async getId(type){
+		const typeList = ['orderId', 'userId', 'addressId', 'cartId', 'imgId'];
+		if (!typeList.includes(type)) {
+			console.log('id类型错误');
+			throw new Error('id类型错误');
+			return
+		}
 		try{
 			const idData = await Ids.findOne();
 			idData[type] ++ ;
@@ -57,5 +67,43 @@ export default class BaseComponent {
 		}catch(err){
 			throw new Error(err)
 		}
+	}
+
+	async uploadImg(req, type = 'default'){
+		return new Promise((resolve, reject) => {
+			const form = formidable.IncomingForm();
+			form.uploadDir = './public/img/' + type;
+			form.parse(req, async (err, fields, files) => {
+				let imgId;
+				try{
+					imgId = await this.getId('imgId');
+				}catch(err){
+					console.log('获取图片id失败');
+					fs.unlink(files.file.path)
+					reject(err);
+				}
+				const imgUrl = new Date().getTime().toString() + imgId;
+				const extname = path.extname(files.file.name);
+				const repath = './public/img/' + type + '/' + imgUrl + extname;
+				try{
+					await fs.rename(files.file.path, repath);
+					gm(repath)
+					.resize(400, 400, '!')
+					.write(repath, async (err) => {
+						if(err){
+							console.log('改写图片尺寸失败');
+							fs.unlink(repath);
+							reject(err);
+						}else{
+							resolve(repath.replace(/^\.\/public/, ''));
+						} 
+					})
+				}catch(err){
+					console.log('改写图片路径失败');
+					fs.unlink(files.file.path)
+					reject(err);
+				}
+			});
+		})
 	}	
 }

+ 4 - 0
public/img/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

+ 1 - 0
routes/shopping.js

@@ -5,5 +5,6 @@ import Shop from '../controller/shopping/shop'
 const router = express.Router();
 
 router.post('/addshop', Shop.addShop);
+router.post('/addimg', Shop.uploadShopImg);
 
 export default router