Authored by 陈轩

处理后台数据

'use strict';
const api = global.yoho.ServiceAPI;
const resourcesProcess = require('../../../utils/resources-process');
const resourcesProcess = require('../../../utils/beautify/resources');
let channel = {
getResourcesData(params) {
... ...
... ... @@ -18,3 +18,8 @@ exports.getProducts = (req, res, next)=>{
})
.catch(next);
};
// 进入某分类下的商品列表 页
exports.entryProducts = (req, res, next) => {
// TODO
};
... ...
... ... @@ -2,10 +2,11 @@
* product list Model
*/
'use strict';
const path = require('path');
const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const path = require('path');
const processProductList = require(path.join(global.utils, '/product-process')).processProductList;
const prettyFilter = require(path.join(global.utils, '/beautify/filters'));
const processProductList = require(path.join(global.utils, '/beautify/product')).processProductList;
let list = {
... ... @@ -26,10 +27,24 @@ let list = {
code: 200
})
.then(result => {
result = camelCase(result);
prettyFilter(result.data.filter);
result.data.productList = processProductList(result.data.productList);
result = camelCase(result);
return result;
});
},
getFilters(params) {
return api.post('', {
method: 'app.search.sales'
}, {
cache: true,
code: 200
})
.then(result => {
});
}
};
... ...
... ... @@ -15,8 +15,8 @@ module.exports = {
port: 6004,
siteUrl: '//m.yohoblk.com',
domains: {
api: 'http://devapi.yoho.cn:58078/',
service: 'http://devservice.yoho.cn:58077/'
api: 'http://api.yoho.cn/',
service: 'http://service.yoho.cn/'
},
subDomains: {
host: '.m.yohoblk.com',
... ...
... ... @@ -24,7 +24,6 @@ module.exports = {
methods: {
select: function(val) {
this.val = val;
this.$parent.select(val);
}
}
};
... ...
<template>
<div>
<brand-filter v-if="type === 'brand'" :data="data" :val="val"></brand-filter>
<normal-filter v-else :data="data" :val="val">
<brand-filter v-if="type === 'brand'" :data="data" :val.sync="val"></brand-filter>
<normal-filter v-else :data="data" :val.sync="val">
<slot></slot>
</normal-filter>
</div>
... ... @@ -16,12 +16,10 @@ module.exports = {
brandFilter,
normalFilter
},
methods: {
selected: function(val) {
let type = this.type;
console.log(`type:${type}, val:${val}`);
}
watch: {
val: function(newV, oldV) {
console.log(`type: ${this.type}, value: ${newV}`);
}
}
};
</script>
... ...
/**
* 格式化 后台 返回的 filters 数据
* @author chenxuan <xuan.chen@yoho.cn>
*/
'use strict';
const _ = require('lodash');
/**
* 处理 以风格的数据
* [
* {filter_attribute: value}
* ]
*
* 处理结果
* [
* {attribute:value,......}
* ]
*
*/
let verboseAttrHandler = (filterField, dataArr) => {
let result = [];
let re = new RegExp(`^${filterField}_`);
dataArr.forEach(obj => {
let item = {};
let keys = Object.keys(obj);
keys.forEach(key=>{
let newKey = key.replace(re, '');
item[newKey] = obj[key];
});
result.push(item);
});
return result;
};
/*
* 处理 以下风格:
* {
"340,99999": "¥339以上",
"0,149": "¥0-149",
"150,179": "¥150-179",
"180,339": "¥180-339"
* },
*
* 处理结果:
* [
* {id: '0,149', value: '0,149', name: '¥0-149'},
* {id: '150, 179', value: '150,179', name: '¥150-179'}
* .....
* ]
* 按照字符串顺序
*/
let keyIdHandler = (filterField, obj) => {
const result = [];
const keys = Object.keys(obj);
keys.sort();
keys.forEach(key=> {
let item = {};
item.id = key;
let t = obj[key];
let isObject = Object.prototype.toString.apply(t) === '[object Object]';
if (isObject) {
item = Object.assign(item, t);
} else {
item.name = t;
}
result.push(item);
});
return result;
};
function prettyFilter(filters) {
let keys = _.keys(filters);
_.forEach(keys, key => {
let process;
// 相同规律的 使用 相同的处理规则
switch (key) {
case 'color':
case 'size':
case 'brand':
process = verboseAttrHandler;
break;
case 'gender':
case 'priceRange':
case 'discount':
process = keyIdHandler;
break;
// 其他不做处理
default:
process = _.noop;
}
filters[key] = process(key, filters[key]) || filters[key];
});
}
module.exports = prettyFilter;
... ...