Authored by htoooth

init shop

'use strict';
const index = (req, res, next) => {
res.json({
code: 200,
data: [],
message: 'hello, world'
});
};
module.exports = {
index
};
... ...
'use strict';
const request = require('request-promise');
const fs = require('fs');
const _ = require('lodash');
const utils = require('../models/utils');
const uploadImg = (req, res, next) => {
let files = req.files && req.files.file || [];
let errTip = null;
if (!_.isArray(files)) {
files = [files];
}
req.body.files = [];
files.forEach(file => {
let types = file.type.split('/');
if (!types || types[0] !== 'image') {
errTip = '上传文件格式不正确!';
}
if (file.size > 10 * 1024 * 1024) {
errTip = '上传文件尺寸太大!';
}
req.body.files.push(fs.createReadStream(file.path));
req.body.files.push(file.name);
});
if (errTip) {
return res.status(403).send({
message: errTip
});
}
request({
method: 'post',
url: 'http://upload.static.yohobuy.com',
formData: {
fileData: req.body.files,
project: req.body.bucket
},
json: true
}).then(function(result) {
if (result && result.code === 200) {
result.data = result.data || {};
result.data.images = _.map(_.get(result, 'data.imagesList'), (it) => {
return utils.getUploadImgAbsoluteUrl(it, req.body.bucket);
});
}
res.json(result);
}).catch(next);
};
module.exports = {
uploadImg
};
... ...
/**
* @author: huangtao
*/
var express = require('express'),
path = require('path');
var app = express();
// set view engin
var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root
app.on('mount', function(parent) {
delete parent.locals.settings; // 不继承父 App 的设置
Object.assign(app.locals, parent.locals);
});
app.use(global.yoho.hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial')],
views: path.join(__dirname, 'views/action'),
helpers: global.yoho.helpers
}));
// router
app.use(require('./router'));
module.exports = app;
... ...
'use strict';
... ...
/**
* common 相关 utils
* @author: 刘传洋<chuanyang.liu@yoho.cn>
* @date: 2016/10/31
*/
/**
* 01=["img10.static.yhbimg.com", "img11.static.yhbimg.com"],
* 02=["img12.static.yhbimg.com", "img13.static.yhbimg.com"]
* 03=["flv01.static.yhbimg.com", "flv01.static.yhbimg.com"]
* @param url
* @param bucket
* @returns {*}
*/
'use strict';
const getUploadImgAbsoluteUrl = (url, bucket) => {
if (!url) {
return null;
}
let urlArr = url.split('/'),
stag = urlArr[urlArr.length - 1].substr(0, 2),
domain = `static.yhbimg.com/${bucket}`;
url = domain + url;
if (stag === '01') {
return `//img11.${url}`;
} else if (stag === '03') {
return `//flv01.${url}`;
} else {
return `//img12.${url}`;
}
};
module.exports = {
getUploadImgAbsoluteUrl
};
... ...
/**
* @author: huangtao
*/
'use strict';
const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
const uploadCtrl = require(`${cRoot}/upload`);
const shopCtrl = require(`${cRoot}/shop`);
router.get('/', shopCtrl.index);
router.post('/upload', multipartMiddleware, uploadCtrl.uploadImg);
module.exports = router;
... ...
... ... @@ -19,6 +19,7 @@ module.exports = app => {
app.use('/guang', require('./apps/guang')); // 逛
app.use('/cart', require('./apps/cart'));// 购物车
app.use('/help', require('./apps/help'));// 帮助中心
app.use('/shop', require('./apps/shop'));// 店铺
// 第三方,如广告联盟
app.use('/3party', require('./apps/3party'));
... ...
'use strict';
const TYPE = require('../type');
module.exports = [
{
type: TYPE.rewrite,
origin: /.*/,
target: req => `/shop/${req.url}`
}
];
... ...