index.js 10.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
'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;

/**
 * 查询用户信息
 *
 * @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 || {};
    });
};

/**
 * 个人中心页面收藏及订单等的数目数据
 *
 * @param int uid 用户ID
 * @return Object 处理之后的数据
 */
const _getInfoNumData = (uid) => {
    if (!uid) {
        return Promise.resolve({});
    }

    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+';
                }
            }
        }

        return Object.assign(res, {
            address_num: data[1].data && data[1].data.length ? data[1].data.length : ''
        });
    });
};

/**
 * 根据性别来决定  默认图片获取字段   如果是 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];
        }

        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;
};


/**
 * 个人中心主页数据
 *
 * @param int uid 用户ID
 * @return Object 处理之后的数据
 */
exports.getUserHomeData = (uid) => {
    return Promise.all([_getUserProfileData(uid), _getInfoNumData(uid)]);
};

/**
 * 帮助中心列表页
 *
 * @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 formatData;
    });
};

/**
 * 帮助中心详情页
 *
 * @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);
};

/**
 * 保存意见反馈数据
 *
 * @param data
 */
exports.saveFeedback = (data) => {
    return serviceAPI.post('suggest/api/v1/suggest/saveSuggest', data).then(result => {
        return {
            code: result.code,
            message: result.message
        };

        // 参考接口数据
    });
};

/**
 * 保存个人信息数据
 *
 * @param data
 */
exports.saveMydetails = (params) => {
    let defaultParam = {
            method: 'app.passport.modifyBase'
        },
        data = Object.assign(defaultParam, params);

    return api.get('', data).then(result => result);
};

/**
 * 最近浏览
 *
 * @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);
    });
};

exports.recentPreviewNew = (uid, limit) => {
    return api.get('', {
        method: 'app.browse.product',
        uid: uid,
        limit: limit,
        page: 1
    }, {
        code: 200
    }).then(data => {
        return data;
    });
};

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)=>`/product/pro_${item.product_id}_${item.goods_list[0].goods_id}/${item.cn_alphabet}.html`);

        for (let i in originArr.data.product_list) {
            originArr.data.product_list[i].goodsUrl = urls[i];
        }
        return originArr;
    });
};

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;
    });
};

exports.addRecords = (uid, udid, browseList) => {
    return api.get('', {
        method: 'app.browse.addBrowseBatch',
        uid: uid,
        udid: udid,
        browseList: browseList
    }, {
        code: 200
    }).then(data => {
        return data;
    });
};

const _getGradeGrade = (uid, channel) => {
    return api.get('', {
        method: 'app.passport.vip',
        uid: uid,
        channel: channel || 1
    }, {
        code: 200
    });
};

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];
};

exports.getGrade = (uid, channel) => {
    if (uid) {
        return api.all([
            _getGradeGrade(uid, channel),
            _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
                    });

                });

                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({});
    }
};

exports.getPreferential = (params) => {

    return api.get('', {
        method: 'app.passport.getPrivilege',
        uid: params.uid,
        channel: params.channel || 1
    }, {
        code: 200
    }).then((result) => {

        let obj = {};

        let resu = {
            privilege: []
        };

        _.forEach(result.data, function(val) {

            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
            });

        });

        return resu;

    });
};