Authored by ccbikai

增加图片上传

/**
* sub app shopping
* @author: jinhu.dong<jinhu.dong@yoho.cn>
* @date: 2016/07/04
*/
var express = require('express');
var app = express();
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
// router
app.use(require('./router'));
module.exports = app;
... ...
/**
* router of sub app shopping
* @author: jinhu.dong<jinhu.dong@yoho.cn>
* @date: 2016/07/04
*/
'use strict';
const router = require('express').Router(); // eslint-disable-line
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
const uploadApi = require('./upload/upload.js');
router.post('/upload/image', multipartMiddleware, uploadApi.uploadImg);
module.exports = router;
... ...
/**
* 上传接口
* @author: kai.bi<kai.bi@yoho.cn>
* @date: 2016/07/22
*/
'use strict';
const request = require('request-promise');
const fs = require('fs');
const _ = require('lodash');
const uploadImg = (req, res, next) => {
let files = req.files && req.files.filename || [];
if (!_.isArray(files)) {
files = [files];
}
req.body.files = [];
files.forEach(file => {
req.body.files.push(fs.createReadStream(file.path));
req.body.files.push(file.name);
});
request({
method: 'post',
url: 'http://upload.static.yohobuy.com',
formData: {
fileData: req.body.files,
project: req.body.bucket || 'goodsimg'
},
json: true
}).then(function(result) {
res.json(result);
}).catch(next);
};
module.exports = {
uploadImg
};
... ...
... ... @@ -7,6 +7,7 @@
module.exports = app => {
app.use('/', require('./apps/channel'));
app.use('/api', require('./apps/api'));
app.use('/product', require('./apps/product'));
app.use('/home', require('./apps/home'));
... ... @@ -14,6 +15,4 @@ module.exports = app => {
if (app.locals.devEnv) {
app.use('/example', require('./apps/example'));
}
app.use('/product', require('./apps/product'));
};
... ...
... ... @@ -22,6 +22,7 @@
"bluebird": "^3.4.1",
"body-parser": "^1.15.2",
"connect-memcached": "^0.2.0",
"connect-multiparty": "^2.0.0",
"cookie-parser": "^1.4.3",
"express": "^4.14.0",
"express-handlebars": "^3.0.0",
... ...
<template>
<div class="upload">
<form v-on:change="upload">
<label class="label-input" for="{{inputId}}">
<input id="{{inputId}}" type="file" name="filename">
</label>
</form>
</div>
</template>
<script>
module.exports = {
props: ['imageList', 'bucket'],
data() {
return {
inputId: 'input-' + Math.floor(Math.random() * 999999999) // 尽可能保证 input ID 唯一
};
},
methods: {
upload(e) {
const formData = new FormData(e.target.closest('form'));
formData.append('bucket', this.bucket || '');
$.ajax({
method: 'POST',
url: '/api/upload/image',
data: formData,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false // 告诉jQuery不要去设置Content-Type请求头
}).then(res => {
if (res.code === 200) {
res.data.imagesList.forEach(imagesPath => {
this.imageList.push(imagesPath);
});
} else {
alert(res.message);
}
});
}
}
};
</script>
<style>
/* 每个地方上传按钮可能不一样,使用的时候自己写样式 */
</style>
... ...
... ... @@ -12,10 +12,26 @@
<option v-for="reason in returnReason" v-bind:value="reason.id" selected="{{reason.id === product.reason.id}}">{{reason.name}}</option>
</select>
</div>
<upload v-bind:image-list="product.imageList"></upload>
</div>
</template>
<script>
const upload = require('component/tool/upload.vue');
const getImgHost = function(url) {
let urlArr = url.split('/'),
num = urlArr[urlArr.length - 1].substr(1, 1),
domain = 'static.yhbimg.com/goodsimg';
url = domain + url;
if (num === 2) {
return '//img11.' + url;
} else {
return '//img12.' + url;
}
};
module.exports = {
props: ['product', 'refundData'],
computed: {
... ... @@ -30,6 +46,9 @@
showTip() {
alert(this.refundData.specialNotice.remark);
}
},
components: {
upload
}
};
</script>
... ...