Authored by 王水玲

上下文

'use strict';
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const helpers = global.yoho.helpers;
const _ = require('lodash');
const config = global.yoho.config;
const singleAPI = global.yoho.SingleAPI;
/**
* 查询用户信息
*
* @param int uid 用户ID
* @return Object 个人详情数据
*/
const _getUserProfileData = exports.getUserProfileData = (uid) => {
if (!uid) {
return Promise.resolve({});
}
return api.get('', {
uid: uid,
method: 'app.passport.profile'
}, {
code: 200
}).then(result => {
return result.data || {};
});
const iconId = (id) => {
return {
1: 'icon-5',
2: 'icon-0',
3: 'icon-8',
4: 'icon-niandu',
5: 'icon-kuaisu',
6: 'icon-zazhi',
9: 'icon-youhui',
10: 'icon-vip'
}[id];
};
/**
* 个人中心页面收藏及订单等的数目数据
*
* @param int uid 用户ID
* @return Object 处理之后的数据
*/
const _getInfoNumData = (uid) => {
if (!uid) {
return Promise.resolve({});
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
return api.all([api.get('', {
uid: uid,
method: 'app.home.getInfoNum' // 收藏及订单数目数据
}, {
code: 200
}), api.get('', {
uid: uid,
method: 'app.address.gethidden' // 用户地址的数据
}, {
code: 200
})]).then(data => {
const res = {
wait_pay_num: '',
wait_cargo_num: '',
send_cargo_num: '',
refund_exchange_num: '',
yoho_coin_num: ''
};
for (let k in data[0].data) {
if (res[k] !== null) {
res[k] = data[0].data[k] ? data[0].data[k] : '';
if (k !== 'yoho_coin_num' && res[k] > 99) {
res[k] = '99+';
}
}
/**
* 查询用户信息
*
* @param int uid 用户ID
* @return Object 个人详情数据
*/
getUserProfileData(uid) {
if (!uid) {
return Promise.resolve({});
}
return Object.assign(res, {
address_num: data[1].data && data[1].data.length ? data[1].data.length : ''
return this.get({
data: {
uid: uid,
method: 'app.passport.profile'
},
param: {
code: 200
}
}).then(result => {
return result.data || {};
});
});
};
/**
* 根据性别来决定 默认图片获取字段 如果是 2、3
*
* 则优先从cover_2 --》 cover_1 -- 》 images_url
* 否则优先从cover_1 --》 cover_2 -- 》 images_url
*
*/
const _procProductImg = (product) => {
if (product.gender === '2,3' || product.gender === '2' || product.gender === '3') {
return product.cover_2 || product.images_url || product.cover_1 || '';
}
return product.cover_1 || product.images_url || product.cover_2 || '';
};
const _processProduct = (list) => {
let newRes = [];
list = list || [];
list.forEach(item => {
let defaultGoods = _.find(item.goods_list, {is_default: 'Y'});
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = item.goods_list[0];
};
/**
* 个人中心页面收藏及订单等的数目数据
*
* @param int uid 用户ID
* @return Object 处理之后的数据
*/
_getInfoNumData(uid) {
if (!uid) {
return Promise.resolve({});
}
item.default_images = _procProductImg(defaultGoods);
item.goodsUrl = `${config.siteUrl}/product/pro_${item.product_id}_${defaultGoods.goods_id}/${item.cn_alphabet}.html`; // eslint-disable-line
});
_.chunk(list, 5).forEach(item => {
newRes.push({
list: item
});
});
return newRes;
};
return Promise.all([this.get({
data: {
uid: uid,
method: 'app.home.getInfoNum' // 收藏及订单数目数据
},
param: {
code: 200
}
}), this.get({
data: {
uid: uid,
method: 'app.address.gethidden' // 用户地址的数据
},
param: {
code: 200
}
})]).then(data => {
const res = {
wait_pay_num: '',
wait_cargo_num: '',
send_cargo_num: '',
refund_exchange_num: '',
yoho_coin_num: ''
};
/**
* 个人中心主页数据
*
* @param int uid 用户ID
* @return Object 处理之后的数据
*/
exports.getUserHomeData = (uid) => {
return Promise.all([_getUserProfileData(uid), _getInfoNumData(uid)]);
};
for (let k in data[0].data) {
if (res[k] !== null) {
res[k] = data[0].data[k] ? data[0].data[k] : '';
if (k !== 'yoho_coin_num' && res[k] > 99) {
res[k] = '99+';
}
}
}
/**
* 帮助中心列表页
*
* @param data
*
*/
exports.getHelpInfo = (data) => {
let defaultParam = {
method: 'app.help.li'
},
infoData = Object.assign(defaultParam, data);
return api.get('', infoData, {
code: 200
}).then(result => {
const formatData = [];
let helpData = result.data;
helpData = helpData || [];
helpData.forEach(function(item) {
formatData.push({
name: item.caption,
code: item.code,
url: helpers.urlFormat('/help', {
id: item.code,
caption: item.caption,
})
return Object.assign(res, {
address_num: data[1].data && data[1].data.length ? data[1].data.length : ''
});
});
return formatData;
});
};
};
/**
* 根据性别来决定 默认图片获取字段 如果是 2、3
*
* 则优先从cover_2 --》 cover_1 -- 》 images_url
* 否则优先从cover_1 --》 cover_2 -- 》 images_url
*
*/
_procProductImg(product) {
if (product.gender === '2,3' || product.gender === '2' || product.gender === '3') {
return product.cover_2 || product.images_url || product.cover_1 || '';
}
/**
* 帮助中心详情页
*
* @param data
*/
exports.getHelpDetail = (data) => {
let defaultParam = {
method: 'app.help.detail',
return_type: 'html'
},
detailData = Object.assign(defaultParam, data);
return api.get('', detailData, {
code: 200
}).then(result => result);
};
return product.cover_1 || product.images_url || product.cover_2 || '';
};
/**
* 保存意见反馈数据
*
* @param data
*/
exports.saveFeedback = (data) => {
return serviceAPI.post('suggest/api/v1/suggest/saveSuggest', data).then(result => {
return {
code: result.code,
message: result.message
};
// 参考接口数据
});
};
_processProduct(list) {
let newRes = [];
/**
* 保存个人信息数据
*
* @param data
*/
exports.saveMydetails = (params) => {
let defaultParam = {
method: 'app.passport.modifyBase'
},
data = Object.assign(defaultParam, params);
return api.get('', data).then(result => result);
};
list = list || [];
/**
* 最近浏览
*
* @param data
*/
exports.recentPreview = (skn, limit) => {
return api.get('', {
method: 'h5.product.batch',
productSkn: skn,
limit: limit,
page: 1
}, {
code: 200
}).then(data => {
return _processProduct(data.data.product_list);
});
};
list.forEach(item => {
let defaultGoods = _.find(item.goods_list, {is_default: 'Y'});
exports.recentPreviewNew = (uid, limit) => {
return api.get('', {
method: 'app.browse.product',
uid: uid,
limit: limit,
page: 1
}, {
code: 200
}).then(data => {
return data;
});
};
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = item.goods_list[0];
}
exports.getSknUrl = (skn, originArr) => {
return api.get('', {
method: 'h5.product.batch',
productSkn: skn,
page: 1
}, {
code: 200
}).then(data => {
let urls = data.data.product_list.map((item)=> {
item.default_images = this._procProductImg(defaultGoods);
item.goodsUrl = `${config.siteUrl}/product/pro_${item.product_id}_${defaultGoods.goods_id}/${item.cn_alphabet}.html`; // eslint-disable-line
});
_.chunk(list, 5).forEach(item => {
newRes.push({
list: item
});
});
return newRes;
};
/**
* 个人中心主页数据
*
* @param int uid 用户ID
* @return Object 处理之后的数据
*/
getUserHomeData(uid) {
return Promise.all([this.getUserProfileData(uid), this._getInfoNumData(uid)]);
};
/**
* 帮助中心列表页
*
* @param data
*
*/
getHelpInfo(data) {
let defaultParam = {
method: 'app.help.li'
},
infoData = Object.assign(defaultParam, data);
return this.get({
data: infoData,
param: {
code: 200
}
}).then(result => {
const formatData = [];
let helpData = result.data;
helpData = helpData || [];
helpData.forEach(function(item) {
formatData.push({
name: item.caption,
code: item.code,
url: helpers.urlFormat('/help', {
id: item.code,
caption: item.caption,
})
});
});
return formatData;
});
};
/**
* 帮助中心详情页
*
* @param data
*/
getHelpDetail(data) {
let defaultParam = {
method: 'app.help.detail',
return_type: 'html'
},
detailData = Object.assign(defaultParam, data);
return this.get({
data: detailData,
param: {
code: 200
}
}).then(result => result);
};
/**
* 保存意见反馈数据
*
* @param data
*/
saveFeedback(data) {
return this.post({
url: 'suggest/api/v1/suggest/saveSuggest',
data: data,
api: global.yoho.ServiceAPI
}).then(result => {
return {
skn: item.product_skn,
url: `/product/pro_${item.product_id}_${item.goods_list[0].goods_id}/${item.cn_alphabet}.html`
code: result.code,
message: result.message
};
});//eslint-disable-line
_.each(originArr.data.product_list, product => {
let urlItem = _.find(urls, url => url.skn === product.product_skn);
product.goodsUrl = _.get(urlItem, 'url', '');
// 参考接口数据
});
return originArr;
});
};
};
/**
* 保存个人信息数据
*
* @param data
*/
saveMydetails(params) {
let defaultParam = {
method: 'app.passport.modifyBase'
},
data = Object.assign(defaultParam, params);
return this.get({
data: data
}).then(result => result);
};
/**
* 最近浏览
*
* @param data
*/
recentPreview(skn, limit) {
return this.get({
data: {
method: 'h5.product.batch',
productSkn: skn,
limit: limit,
page: 1
},
param: {
code: 200
}
}).then(data => {
return this._processProduct(data.data.product_list);
});
};
recentPreviewNew(uid, limit) {
return this.get({
data: {
method: 'app.browse.product',
uid: uid,
limit: limit,
page: 1
},
param: {
code: 200
}
}).then(data => {
return data;
});
};
getSknUrl(skn, originArr) {
return this.get({
data: {
method: 'h5.product.batch',
productSkn: skn,
page: 1
},
param: {
code: 200
}
}).then(data => {
let urls = data.data.product_list.map((item)=> {
return {
skn: item.product_skn,
url: `/product/pro_${item.product_id}_${item.goods_list[0].goods_id}/${item.cn_alphabet}.html`
};
});//eslint-disable-line
_.each(originArr.data.product_list, product => {
let urlItem = _.find(urls, url => url.skn === product.product_skn);
product.goodsUrl = _.get(urlItem, 'url', '');
});
return originArr;
});
};
deleteRecordData(uid, skn) {
return this.get({
data: {
method: 'app.browse.delete',
uid: uid,
skn: skn
},
param: {
code: 200
}
}).then(data => {
return data;
});
};
recordNum(uid, udid) {
return this.get({
data: {
method: 'app.browse.total',
uid: uid,
udid: udid
},
param: {
code: 200
}
}).then(data => {
return data;
});
};
addRecords(uid, udid, browseList) {
return this.get({
data: {
method: 'app.browse.addBrowseBatch',
uid: uid,
udid: udid,
browseList: browseList
},
param: {
code: 200
}
}).then(data => {
return data;
});
};
_getGradeGrade(uid, channel) {
return this.get({
data: {
method: 'app.passport.vip',
uid: uid,
channel: channel || 1
},
param: {
code: 200
}
});
};
getGrade(uid, channel) {
if (uid) {
return Promise.all([
this._getGradeGrade(uid, channel),
this.getUserProfileData(uid)
]).then((result) => {
let obj = {
privilege: []
};
if (result[0] && result[0].data) {
let perf = (100 * (result[0].data.current_year_cost / result[0].data.next_need_cost)).toFixed(2);
_.forEach(result[0].data.enjoy_preferential, function(val) {
if (parseInt(val.id, 10) === 7 || parseInt(val.id, 10) === 8) {
return true;
}
obj.privilege.push({
description: val.description,
pic: iconId(parseInt(val.id, 10)),
title: val.title
});
exports.deleteRecordData = (uid, skn) => {
return api.get('', {
method: 'app.browse.delete',
uid: uid,
skn: skn
}, {
code: 200
}).then(data => {
return data;
});
};
});
exports.recordNum = (uid, udid) => {
return api.get('', {
method: 'app.browse.total',
uid: uid,
udid: udid
}, {
code: 200
}).then(data => {
return data;
});
};
switch (result[0].data.current_vip_level) {
case '0': // 普通会员
obj = _.assign(obj, {
vip0: true,
percent: perf
});
break;
case '1': // 银卡会员
obj = _.assign(obj, {
vip1: true,
percent: perf
});
break;
case '2': // 金卡会员
obj = _.assign(obj, {
vip2: true,
percent: perf
});
break;
case '3': // 白金会员
obj = _.assign(obj, {
vip3: true,
percent: 100
});
break;
default:
}
exports.addRecords = (uid, udid, browseList) => {
return api.get('', {
method: 'app.browse.addBrowseBatch',
uid: uid,
udid: udid,
browseList: browseList
}, {
code: 200
}).then(data => {
return data;
});
};
let upg = (1 * (result[0].data.upgrade_need_cost)).toFixed(2);
const _getGradeGrade = (uid, channel) => {
return api.get('', {
method: 'app.passport.vip',
uid: uid,
channel: channel || 1
}, {
code: 200
});
};
obj = _.assign(obj, {
costOfThisYear: result[0].data.current_year_cost,
sumCost: result[0].data.current_total_cost,
costGap: upg
});
const iconId = (id) => {
return {
1: 'icon-5',
2: 'icon-0',
3: 'icon-8',
4: 'icon-niandu',
5: 'icon-kuaisu',
6: 'icon-zazhi',
9: 'icon-youhui',
10: 'icon-vip'
}[id];
};
// if (result[0].data.current_vip_level === '3') {
// // 当vip等级升至顶级时,进度条满格
// obj = _.assign(obj, {
// percent: 100
// });
// } else {
exports.getGrade = (uid, channel) => {
if (uid) {
return api.all([
_getGradeGrade(uid, channel),
_getUserProfileData(uid)
]).then((result) => {
let obj = {
privilege: []
};
// let perf = (100 * (result[0].data.current_year_cost / result[0].data.next_need_cost)).toFixed(2);
if (result[0] && result[0].data) {
let perf = (100 * (result[0].data.current_year_cost / result[0].data.next_need_cost)).toFixed(2);
// obj = _.assign(obj, {
// percent: perf
// });
_.forEach(result[0].data.enjoy_preferential, function(val) {
// }
if (parseInt(val.id, 10) === 7 || parseInt(val.id, 10) === 8) {
return true;
}
}
obj.privilege.push({
description: val.description,
pic: iconId(parseInt(val.id, 10)),
title: val.title
if (result[0] && result[0].data) {
obj = _.assign(obj, {
name: result[1].nickname
});
});
switch (result[0].data.current_vip_level) {
case '0': // 普通会员
obj = _.assign(obj, {
vip0: true,
percent: perf
});
break;
case '1': // 银卡会员
obj = _.assign(obj, {
vip1: true,
percent: perf
});
break;
case '2': // 金卡会员
obj = _.assign(obj, {
vip2: true,
percent: perf
});
break;
case '3': // 白金会员
obj = _.assign(obj, {
vip3: true,
percent: 100
});
break;
default:
}
let upg = (1 * (result[0].data.upgrade_need_cost)).toFixed(2);
obj = _.assign(obj, {
costOfThisYear: result[0].data.current_year_cost,
sumCost: result[0].data.current_total_cost,
costGap: upg
});
// if (result[0].data.current_vip_level === '3') {
// // 当vip等级升至顶级时,进度条满格
// obj = _.assign(obj, {
// percent: 100
// });
// } else {
// let perf = (100 * (result[0].data.current_year_cost / result[0].data.next_need_cost)).toFixed(2);
// obj = _.assign(obj, {
// percent: perf
// });
// }
}
if (result[0] && result[0].data) {
obj = _.assign(obj, {
name: result[1].nickname
});
return obj;
});
} else {
return Promise.resolve({});
}
};
getPreferential(params) {
return this.get({
data: {
method: 'app.passport.getPrivilege',
uid: params.uid,
channel: params.channel || 1
},
param: {
code: 200
}
}).then((result) => {
return obj;
});
} else {
return Promise.resolve({});
}
};
exports.getPreferential = (params) => {
return api.get('', {
method: 'app.passport.getPrivilege',
uid: params.uid,
channel: params.channel || 1
}, {
code: 200
}).then((result) => {
let resu = {
privilege: []
};
let resu = {
privilege: []
};
_.forEach(result.data, function(val) {
_.forEach(result.data, function(val) {
if (parseInt(val.id, 10) === 7 || parseInt(val.id, 10) === 8) {
return true;
}
if (parseInt(val.id, 10) === 7 || parseInt(val.id, 10) === 8) {
return true;
}
resu.privilege.push({
description: val.description,
pic: iconId(parseInt(val.id, 10)),
title: val.title
});
resu.privilege.push({
description: val.description,
pic: iconId(parseInt(val.id, 10)),
title: val.title
});
});
return resu;
});
};
// 商品收藏数量接口
exports.favoriteNum = (uid) => {
return singleAPI.get('favorite', {
method: 'app.favorite.getFavoriteCount',
uid: uid
}, {
code: 200
});
};
return resu;
// 资讯收藏数量接口
exports.editorialNum = (uid) => {
return serviceAPI.post('guang/api/v2/favorite/getFavNum', {
uid: uid,
app_type: 1
}, {
code: 200
});
};
});
};
// 商品收藏数量接口
favoriteNum(uid) {
return this.get({
url: 'favorite',
data: {
method: 'app.favorite.getFavoriteCount',
uid: uid
},
param: {
code: 200
},
api: global.yoho.SingleAPI
});
};
// 资讯收藏数量接口
editorialNum(uid) {
return this.post({
url: 'guang/api/v2/favorite/getFavNum',
data: {
uid: uid,
app_type: 1
},
param: {
code: 200
},
api: global.yoho.ServiceAPI
});
};
};
\ No newline at end of file
... ...