Showing
11 changed files
with
184 additions
and
0 deletions
apps/shop/controllers/shop.js
0 → 100644
apps/shop/controllers/upload.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +const request = require('request-promise'); | ||
4 | +const fs = require('fs'); | ||
5 | +const _ = require('lodash'); | ||
6 | + | ||
7 | +const utils = require('../models/utils'); | ||
8 | + | ||
9 | +const uploadImg = (req, res, next) => { | ||
10 | + let files = req.files && req.files.file || []; | ||
11 | + let errTip = null; | ||
12 | + | ||
13 | + if (!_.isArray(files)) { | ||
14 | + files = [files]; | ||
15 | + } | ||
16 | + | ||
17 | + req.body.files = []; | ||
18 | + files.forEach(file => { | ||
19 | + | ||
20 | + let types = file.type.split('/'); | ||
21 | + | ||
22 | + if (!types || types[0] !== 'image') { | ||
23 | + errTip = '上传文件格式不正确!'; | ||
24 | + } | ||
25 | + | ||
26 | + if (file.size > 10 * 1024 * 1024) { | ||
27 | + errTip = '上传文件尺寸太大!'; | ||
28 | + } | ||
29 | + req.body.files.push(fs.createReadStream(file.path)); | ||
30 | + req.body.files.push(file.name); | ||
31 | + }); | ||
32 | + | ||
33 | + if (errTip) { | ||
34 | + return res.status(403).send({ | ||
35 | + message: errTip | ||
36 | + }); | ||
37 | + } | ||
38 | + | ||
39 | + request({ | ||
40 | + method: 'post', | ||
41 | + url: 'http://upload.static.yohobuy.com', | ||
42 | + formData: { | ||
43 | + fileData: req.body.files, | ||
44 | + project: req.body.bucket | ||
45 | + }, | ||
46 | + json: true | ||
47 | + }).then(function(result) { | ||
48 | + | ||
49 | + if (result && result.code === 200) { | ||
50 | + result.data = result.data || {}; | ||
51 | + result.data.images = _.map(_.get(result, 'data.imagesList'), (it) => { | ||
52 | + return utils.getUploadImgAbsoluteUrl(it, req.body.bucket); | ||
53 | + }); | ||
54 | + } | ||
55 | + | ||
56 | + res.json(result); | ||
57 | + }).catch(next); | ||
58 | +}; | ||
59 | + | ||
60 | +module.exports = { | ||
61 | + uploadImg | ||
62 | +}; |
apps/shop/index.js
0 → 100644
1 | +/** | ||
2 | + * @author: huangtao | ||
3 | + */ | ||
4 | + | ||
5 | +var express = require('express'), | ||
6 | + path = require('path'); | ||
7 | + | ||
8 | +var app = express(); | ||
9 | + | ||
10 | +// set view engin | ||
11 | +var doraemon = path.join(__dirname, '../../doraemon/views'); // parent view root | ||
12 | + | ||
13 | +app.on('mount', function(parent) { | ||
14 | + delete parent.locals.settings; // 不继承父 App 的设置 | ||
15 | + Object.assign(app.locals, parent.locals); | ||
16 | +}); | ||
17 | + | ||
18 | +app.use(global.yoho.hbs({ | ||
19 | + extname: '.hbs', | ||
20 | + defaultLayout: 'layout', | ||
21 | + layoutsDir: doraemon, | ||
22 | + partialsDir: [path.join(__dirname, 'views/partial')], | ||
23 | + views: path.join(__dirname, 'views/action'), | ||
24 | + helpers: global.yoho.helpers | ||
25 | +})); | ||
26 | + | ||
27 | +// router | ||
28 | +app.use(require('./router')); | ||
29 | + | ||
30 | +module.exports = app; |
apps/shop/models/shop-api.js
0 → 100644
1 | +'use strict'; |
apps/shop/models/shop-service.js
0 → 100644
1 | +'use strict'; |
apps/shop/models/utils.js
0 → 100644
1 | +/** | ||
2 | + * common 相关 utils | ||
3 | + * @author: 刘传洋<chuanyang.liu@yoho.cn> | ||
4 | + * @date: 2016/10/31 | ||
5 | + */ | ||
6 | + | ||
7 | +/** | ||
8 | + * 01=["img10.static.yhbimg.com", "img11.static.yhbimg.com"], | ||
9 | + * 02=["img12.static.yhbimg.com", "img13.static.yhbimg.com"] | ||
10 | + * 03=["flv01.static.yhbimg.com", "flv01.static.yhbimg.com"] | ||
11 | + * @param url | ||
12 | + * @param bucket | ||
13 | + * @returns {*} | ||
14 | + */ | ||
15 | +'use strict'; | ||
16 | + | ||
17 | +const getUploadImgAbsoluteUrl = (url, bucket) => { | ||
18 | + | ||
19 | + if (!url) { | ||
20 | + return null; | ||
21 | + } | ||
22 | + | ||
23 | + let urlArr = url.split('/'), | ||
24 | + stag = urlArr[urlArr.length - 1].substr(0, 2), | ||
25 | + domain = `static.yhbimg.com/${bucket}`; | ||
26 | + | ||
27 | + url = domain + url; | ||
28 | + if (stag === '01') { | ||
29 | + return `//img11.${url}`; | ||
30 | + } else if (stag === '03') { | ||
31 | + return `//flv01.${url}`; | ||
32 | + } else { | ||
33 | + return `//img12.${url}`; | ||
34 | + } | ||
35 | +}; | ||
36 | + | ||
37 | +module.exports = { | ||
38 | + getUploadImgAbsoluteUrl | ||
39 | +}; |
apps/shop/router.js
0 → 100644
1 | +/** | ||
2 | + * @author: huangtao | ||
3 | + */ | ||
4 | + | ||
5 | +'use strict'; | ||
6 | + | ||
7 | +const router = require('express').Router(); // eslint-disable-line | ||
8 | +const cRoot = './controllers'; | ||
9 | + | ||
10 | +var multipart = require('connect-multiparty'); | ||
11 | +var multipartMiddleware = multipart(); | ||
12 | + | ||
13 | +const uploadCtrl = require(`${cRoot}/upload`); | ||
14 | +const shopCtrl = require(`${cRoot}/shop`); | ||
15 | + | ||
16 | +router.get('/', shopCtrl.index); | ||
17 | +router.post('/upload', multipartMiddleware, uploadCtrl.uploadImg); | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | +module.exports = router; | ||
22 | + | ||
23 | + | ||
24 | + | ||
25 | + |
apps/shop/views/action/.gitkeeper
0 → 100644
apps/shop/views/partial/.gitkeeper
0 → 100644
@@ -19,6 +19,7 @@ module.exports = app => { | @@ -19,6 +19,7 @@ module.exports = app => { | ||
19 | app.use('/guang', require('./apps/guang')); // 逛 | 19 | app.use('/guang', require('./apps/guang')); // 逛 |
20 | app.use('/cart', require('./apps/cart'));// 购物车 | 20 | app.use('/cart', require('./apps/cart'));// 购物车 |
21 | app.use('/help', require('./apps/help'));// 帮助中心 | 21 | app.use('/help', require('./apps/help'));// 帮助中心 |
22 | + app.use('/shop', require('./apps/shop'));// 店铺 | ||
22 | 23 | ||
23 | // 第三方,如广告联盟 | 24 | // 第三方,如广告联盟 |
24 | app.use('/3party', require('./apps/3party')); | 25 | app.use('/3party', require('./apps/3party')); |
-
Please register or login to post a comment