user-service.js 19.8 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/**
 * 个人中心 编辑资料models
 * @author: gaohongwei<hongwei.gao@yoho.cn>
 * @date: 2016/8/16
 */

'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const api = global.yoho.API;
const userApi = require('./user-api');

const searchApi = global.yoho.SearchAPI;
const Images = require('../../../utils/images');
const headerModel = require('../../../doraemon/models/header');

const configData = {
    gender: [{
        key: 'gender',
        value: 1,
        text: '男'
    }, {
        key: 'gender',
        value: 2,
        text: '女'
    }],
    income: [{
        key: 'income',
        value: 0,
        text: '请选择'
    }, {
        key: 'income',
        value: 1,
        text: '1000以下'
    }, {
        key: 'income',
        value: 2,
        text: '1000-2999'
    }, {
        key: 'income',
        value: 3,
        text: '3000-5999'
    }, {
        key: 'income',
        value: 4,
        text: '6000-7999'
    }, {
        key: 'income',
        value: 5,
        text: '8000-10000'
    }, {
        key: 'income',
        value: 6,
        text: '10000以上'
    }],
    profession: [{
        key: 'profession',
        value: 1,
        text: '已工作'
    }, {
        key: 'profession',
        value: 2,
        text: '大学生'
    }, {
        key: 'profession',
        value: 3,
        text: '中学生'
    }, {
        key: 'profession',
        value: 4,
        text: '其他'
    }],
    shoppingHabits: [{
        type: 'radio',
        key: 'shopping',
        value: '1',
        text: '目的型购物'
    },
        {
            type: 'radio',
            key: 'shopping',
            value: '2',
            text: '冲动型购物'
        },
        {
            type: 'radio',
            key: 'shopping',
            value: '3',
            text: '保守型购物'
        }],

    // 着装习惯
    dressHabits: [{
        type: 'checkbox',
        key: 'dress',
        index: 'dress-1',
        value: '1',
        text: '正装'
    },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-2',
            value: '2',
            text: '商务'
        },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-3',
            value: '3',
            text: '街头流行'
        },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-4',
            value: '4',
            text: '运动休闲'
        },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-5',
            value: '5',
            text: '文艺气质'
        },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-6',
            value: '6',
            text: '甜美可爱'
        },
        {
            type: 'checkbox',
            key: 'dress',
            index: 'dress-7',
            value: '7',
            text: '另类'
        }],
    headDefaultImgIcon: '//img10.static.yhbimg.com/headimg/' +
        '2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100'
};

const getDays = (year, month) => {
    let m = (month - 1) % 7 % 2 ? 30 : 31,
        y400 = year % 400 ? 28 : 29,
        y100 = year % 100 ? 29 : y400,
        y4 = year % 4 ? 28 : y100;

    return month === 2 ? y4 : m;
};

const getBirthday = (birthday) => {
    let year = parseInt(birthday.split('-')[0], 0),
        month = parseInt(birthday.split('-')[1], 0),
        day = parseInt(birthday.split('-')[2], 0),
        nowYear = new Date().getFullYear(),
        yearArr = [{value: 0, text: '年'}],
        monthArr = [{value: 0, text: '月'}],
        dayArr = [{value: 0, text: '日'}],
        days = getDays(year, month);

    for (let i = 1950; i <= nowYear; i++) {
        let yearJson = {value: i, text: i};

        if (i === year) {
            yearJson.isChecked = true;
        }
        yearArr.push(yearJson);
    }

    for (let j = 1; j <= 12; j++) {
        let monthJson = {value: j, text: j};

        if (j === month) {
            monthJson.isChecked = true;
        }
        monthArr.push(monthJson);
    }

    for (let k = 1; k <= days; k++) {
        let dayJson = {value: k, text: k};

        if (k === day) {
            dayJson.isChecked = true;
        }
        dayArr.push(dayJson);
    }

    return {
        yearArr: yearArr,
        monthArr: monthArr,
        dayArr: dayArr
    };
};

const formatDate = (y, m, d) => {
    m = m < 10 ? ('0' + m) : m;
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d;
};

const isEmpty = (value) => {
    return (Array.isArray(value) && value.length === 0) ||
        (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
};

const getHotBrands = (brandList) => {
    let hotBrands = [],
        i = 20;

    if (brandList) {
        for (let j = 0; j < brandList.length; j++) {
            if (brandList[j].is_hot === 'Y') {
                hotBrands.push({
                    id: 'pp-' + brandList[j].id,
                    name: 'pp[]',
                    value: brandList[j].id,
                    text: brandList[j].brand_name
                });
                i--;
            }
            if (i <= 0) {
                break;
            }
        }
    }

    return hotBrands;
};

/**
 * 联动取地区信息
 */
const getProviceCityInfo = (parentId, checkValue) => {
    return co(function*() {
        let addressInfo = yield userApi.getProviceCityInfo(parentId),
            res = [{value: 0}],
            defaultText;

        if (parentId.length === 2) {
            defaultText = '请选择省份';
        } else if (parentId.length === 4) {
            defaultText = '请选择市';
        } else {
            defaultText = '请选择区县';
        }
        res[0].text = defaultText;
        if (addressInfo.code === 200) {
            for (let i = addressInfo.data.length - 1; i >= 0; i--) {
                let addressJson = {
                    value: addressInfo.data[i].id,
                    text: addressInfo.data[i].caption
                };

                if (!checkValue && addressInfo.data[i].id === checkValue) {
                    addressJson.isChecked = true;
                }
                res.push(addressJson);
            }
        }

        return res;
    })();
};

const getUserInfo = (channel, uid) => {

    return co(function*() {

        let result = yield api.all([
            headerModel.requestHeaderData(channel),
            userApi.getUserInfo(uid),
            userApi.getTipConfig(uid),
            userApi.getUserContactInfo(uid),
            userApi.getUserHabitsInfo(uid),
            userApi.getUserLikeBrand(uid),
            searchApi.get('/brand/list.json', {}, {cache: true})
        ]);

        let finalResult = {},
            headerData = {},
            userInfo = {};

        if (result[0].code === 200) {
            headerData = result[0];
        }

        if (result[1].code === 200) {
            let gender,
                genderArr,
                birthday,
                birthdayJson,
                yearArr,
                monthArr,
                dayArr,
                profession,
                professionArr,
                income,
                incomeArr;

            userInfo = result[1].data || {};

            finalResult.userThumb = userInfo.head_ico === '' ?
                configData.headDefaultImgIcon : Images.getImageUrl(userInfo.head_ico, 100, 100, 2);

            if (userInfo) {
                gender = userInfo.gender || 3;
                birthday = userInfo.birthday || '';
                profession = userInfo.profession || 0;
                income = userInfo.income || 0;
            }

            genderArr = configData.gender;
            for (let value of genderArr) {
                if (value.value === parseInt(gender, 0)) {
                    value.isChecked = true;
                }
            }

            professionArr = configData.profession;
            for (let value of professionArr) {
                if (value.value === parseInt(profession, 0)) {
                    value.isChecked = true;
                }
            }

            incomeArr = configData.income;
            for (let value of incomeArr) {
                if (value.value === parseInt(income, 0)) {
                    value.isChecked = true;
                }
            }

            birthdayJson = getBirthday(birthday);
            yearArr = birthdayJson.yearArr;
            monthArr = birthdayJson.monthArr;
            dayArr = birthdayJson.dayArr;

            finalResult.userPersonalInfo = {
                subTitle: '会员信息',
                firstBox: true,
                submitId: 'base-info',
                profileSrc: userInfo.head_ico,
                email: {
                    labelText: '登录邮箱:',
                    value: userInfo.email || ''
                },
                name: [
                    {
                        labelText: '昵称:',
                        value: userInfo.nickname,
                        key: 'nickname',
                        tips: '与Yoho!业务或商家品牌冲突的昵称,Yoho!将有可能收回'
                    },
                    {
                        labelText: '真实姓名:',
                        value: userInfo.username,
                        key: 'username',
                        tips: '' // 4.8去掉该提示
                    }
                ],
                gender: {
                    labelText: '性别:',
                    key: 'gender',
                    data: genderArr
                },
                birthday: {
                    labelText: '生日:',
                    key: 'birthday',
                    tips: '',
                    selects: [
                        {
                            key: 'year',
                            options: yearArr,
                            unit: '年'
                        },
                        {
                            key: 'month',
                            options: monthArr,
                            unit: '月'
                        },
                        {
                            key: 'day',
                            options: dayArr,
                            unit: '日'
                        }
                    ]
                },
                profession: {
                    labelText: '职业:',
                    key: 'profession',
                    data: professionArr
                },
                income: {
                    labelText: '收入状况:',
                    key: 'income',
                    data: incomeArr
                }
            };
        }

        if (result[2].code === 200) {
            finalResult.isShowTip = parseInt(result[2].data.config.sn, 10);
        }

        if (result[3].code === 200) {
            let contactInfo = result[3].data || '',
                areaCode = result[3].data.area_code || '';

            console.info(areaCode);

            let proviceOpts = yield getProviceCityInfo(0, areaCode.substr(0, 2)),
                cityOpts = [{value: 0, text: '请选择市'}],
                areaOpts = [{value: 0, text: '请选择区县'}];

            if (areaCode.length === 4) {
                cityOpts = yield getProviceCityInfo(areaCode.substr(0, 2), areaCode.substr(0, 4));
            }

            if (areaCode.length > 4) {
                areaOpts = yield getProviceCityInfo(areaCode.substr(0, 4), areaCode);
            }

            finalResult.contactInfo = {
                subTitle: '联系信息',
                submitId: 'contact-info',
                region: {
                    labelText: '来自:',
                    selects: [
                        {
                            key: 'province',
                            options: proviceOpts
                        },
                        {
                            key: 'city',
                            options: cityOpts
                        },
                        {
                            key: 'areaCode',
                            options: areaOpts
                        }
                    ]
                },
                details: [{
                    labelText: '固定电话:',
                    key: 'phone',
                    value: contactInfo.phone || '',
                    tips: '如: 010-82831245'
                },
                    {
                        labelText: '手机号码:',
                        key: 'mobile',
                        value: contactInfo.mobile || '',
                        tips: '填写手机号便于接收发货和收货通知'
                    },
                    {
                        labelText: 'QQ:',
                        key: 'qq',
                        value: contactInfo.qq || '',
                        tips: '填写QQ方便您的好友联系你'
                    },
                    {
                        longInput: true,
                        labelText: '联系地址:',
                        key: 'fullAddress',
                        value: contactInfo.fullAddress || '',
                        tips: '请填写详细地址'
                    },
                    {
                        labelText: '邮编:',
                        key: 'zipCode',
                        value: contactInfo.zipCode || '',
                        tips: '请输入收货人所在地邮编号'
                    }]

            };

        }

        if (result[4].code === 200) {
            let habitsInfo = result[4].data || '',
                dressHabitArr,
                shoppingHabitArr;

            shoppingHabitArr = configData.shoppingHabits;
            if (isEmpty(habitsInfo.data) && habitsInfo.data.shopping) {
                for (let value of shoppingHabitArr) {
                    if (value.value === parseInt(habitsInfo.data.shopping, 0)) {
                        value.isChecked = true;
                    }
                }
            }

            dressHabitArr = configData.dressHabits;
            if (isEmpty(habitsInfo.dress) && habitsInfo.data.dress) {
                for (let value of dressHabitArr) {
                    if (value.value === parseInt(habitsInfo.data.dress, 0)) {
                        value.isChecked = true;
                    }
                }
            }

            finalResult.habbit = {
                subTitle: '购物&着装习惯',
                submitId: 'shopping-info',
                details: [
                    {
                        labelText: '购物习惯:',
                        data: shoppingHabitArr
                    },
                    {
                        labelText: '着装习惯:',
                        data: dressHabitArr
                    }
                ]
            };
        }

        if (result[5].code === 200 && result[6].code === 200) {
            let likeBrandStr = result[5].data.brand || '',
                brandList = result[6].data,
                favBrands;

            let hotBrands = getHotBrands(brandList);

            if (likeBrandStr && likeBrandStr !== '') {
                let brandArr = likeBrandStr.split(',');

                for (let i; i < brandArr.length; i++) {
                    for (let j; j < brandList.length; j++) {
                        if (brandList.id === brandArr[i]) {
                            favBrands.push({
                                name: brandList.brand_name,
                                id: 'brand-' . brandList.id
                            });
                        }
                    }
                }
            }

            finalResult.favorite = {
                subTitle: '喜爱品牌',
                submitId: 'favorite-brand',
                likebrand: (likeBrandStr.substr(0, 1) === ',') ? likeBrandStr : ',' + likeBrandStr,
                favoriteBrands: favBrands,
                hotBrands: hotBrands
            };
        }

        finalResult.headerData = headerData;
        finalResult.meEditPage = true;
        return finalResult;
    })();
};

const editUserInfo = (req, uid) => {
    let userInfo = {},
        respData;

    userInfo.uid = uid;
    userInfo.nickname = req.body.nickname;
    userInfo.username = req.body.username;
    userInfo.gender = req.body.gender || 3;
    userInfo.birthday = req.body.year === '' ? '' : formatDate(req.body.year, req.body.month, req.body.day);
    userInfo.profession = req.body.profession || 0;
    userInfo.income = req.body.income || 0;

    if (!userInfo.nickname || !userInfo.username || userInfo.birthday === '') {
        respData = {
            code: 400,
            message: '缺失必填项',
            data: ''
        };
    } else {
        respData = userApi.editUserInfo(userInfo);
    }

    console.info(respData);
    return respData;
};

const editUserContactInfo = (req, uid) => {
    let contactInfo = {},
        respData;

    contactInfo.uid = uid;
    contactInfo.areaCode = req.body.areaCode;
    contactInfo.phone = req.body.phone;
    contactInfo.mobile = req.body.mobile;
    contactInfo.qq = req.body.qq;
    contactInfo.fullAddress = req.body.fullAddress;
    contactInfo.zipCode = req.body.zipCode;

    if (!contactInfo.areaCode || !contactInfo.fullAddress ||
        !contactInfo.zipCode || !contactInfo.phone || !contactInfo.mobile) {
        respData = {
            code: 400,
            message: '缺失必填项',
            data: ''
        };
    } else {
        respData = userApi.editUserContactInfo(contactInfo);
    }

    return respData;
};

const editUserHabitsInfo = (req, uid) => {
    let habitsInfo = {},
        dressArr = [],
        respData;

    habitsInfo.uid = uid;
    habitsInfo.shopping = req.body.shopping;

    if (req.body.dress) {
        dressArr = req.body.dress;
        habitsInfo.dress = dressArr.join(',');
    } else {
        habitsInfo.dress = '';
    }

    console.info(habitsInfo);
    if (!habitsInfo.shopping) {
        respData = {
            code: 400,
            message: '缺失必填项',
            data: ''
        };

        return respData;
    }

    return co(function*() {
        respData = yield userApi.editUserHabitsInfo(habitsInfo);

        return respData;
    })();
};

const editUserLikeBrand = (req, uid) => {
    let brand,
        respData;

    brand = req.body.brand;

    if (!brand) {
        respData = {
            code: 400,
            message: '缺失必填项',
            data: ''
        };

        return respData;
    }
    return co(function*() {
        respData = yield userApi.editUserLikeBrand(uid, brand);

        return respData;
    })();
};

const isBrandName = (req) => {
    return api.all([
        searchApi.get('/brand/list.json', {}, {cache: true})

    ]).then(result => {
        let brandList = result.data,
            brandName = req.body.brandName,
            brandInfo = {},
            flag = false;

        if (brandList) {
            for (let i = 0; i < brandList.length; i++) {
                if (brandList[i].brand_name === brandName) {
                    flag = true;
                    brandInfo.brand_name = brandName;
                    brandInfo.id = brandList[i].id;
                    break;
                }
            }
        }

        if (flag) {
            return {
                code: 200,
                message: '',
                data: brandInfo
            };
        } else {
            return {
                code: 400,
                message: '很抱歉,您要添加的品牌不在我们的品牌库中。',
                data: ''
            };
        }

    });
};

const getProviceList = (pid) => {
    return co(function*() {
        let proList = yield userApi.getProviceCityInfo(pid);

        return proList.data;
    })();
};

module.exports = {
    getUserInfo,
    editUserInfo,
    editUserContactInfo,
    editUserHabitsInfo,
    editUserLikeBrand,
    getProviceList,
    isBrandName
};