back-api.js 4.71 KB
/**
 * Created by TaoHuang on 2016/6/15.
 */

'use strict';

const aes = require('./aes-pwd');
const YOHOBUY_URL = 'https//www.yohobuy.com/';

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取地区数据
     */
    getAreaDataAsync() {
        return this.get({
            data: {
                method: 'app.passport.getArea'
            }
        }).then(result => {
            result.data = result.data.map(value => {
                value.areaCode = `+${value.area}`;

                if (value.areaCode === '+86') {
                    value.selected = true;
                } else {
                    value.selected = false;
                }

                delete value.area;
                return value;
            });

            return result;
        });
    }

    /**
     * 通过邮箱找回密码
     *
     * @param  string  mail  邮箱地址
     */
    sendCodeToEmailAsync(email) {
        return this.get({
            data: {
                method: 'app.register.backpwdByEmail',
                email: email
            }
        });
    }

    /**
     * 根据邮箱验证码修改密码(调用www.yohobuy.com接口)
     *
     * @param  string  pwd       新密码
     * @param  string  code    邮箱验证码
     */
    modifyPasswordByEmailAsync(pwd, code) {
        const options = {
            url: `${YOHOBUY_URL}passport/back/update`,
            form: {
                pwd: pwd,
                're-input': pwd,
                code: code
            },
            timeout: 3000
        };

        return this._requestFromAPI(options);
    }

    /**
     * 通过手机找回密码
     *
     * @param  string  mobile 手机号
     * @param  integer area   地区码ID
     */
    sendCodeToMobileAsync(mobile, area) {
        return this.get({
            data: {
                mobile: mobile,
                area: area,
                method: 'app.register.sendBackpwdCodeToMobile'
            }
        });
    }

    /**
     * 校验密码修改手机验证码
     *
     * @param  string  mobile 手机号
     * @param  string  code   验证码
     * @param  integer area   地区码ID
     */
    validateMobileCodeAsync(mobile, code, area) {
        area = area || 86;
        return this.get({
            data: {
                mobile: mobile,
                code: code,
                area: area,
                method: 'app.register.validBackpwdCode'
            }
        });
    }

    /**
     * 根据手机验证码修改密码
     *
     * @param  string  mobile 手机号
     * @param  string  token   验证手机验证码返回的token
     * @param  integer area   地区码ID
     */
    modifyPasswordByMobileAsync(mobile, token, newpwd, area) {
        return this.get({
            data: {
                mobile: mobile,
                token: token,
                newpwd: newpwd,
                area: area,
                method: 'app.register.changepwdByMobileCode'
            }
        });
    }

    /**
     * 根据手机验证码修改密码(密码AES加密)
     *
     * @param  string  mobile 手机号
     * @param  string  token   验证手机验证码返回的token
     * @param  integer area   地区码ID
     */
    modifyPasswordByMobileAsyncAes(mobile, token, newpwd, area) {
        return this.get({
            data: {
                mobile: mobile,
                token: token,
                newpwd: aes.aesPwd(newpwd),
                area: area,
                method: 'app.register.changepwdByMobileCodeAES'
            }
        });
    }

    /**
     * 验证找回邮件code
     */
    checkEmailCodeAsync(code) {
        return this.get({
            data: {
                code: code,
                method: 'web.passport.checkCodeValid'
            }
        });
    }

    /**
     * 根据邮箱code修改密码
     */
    modifyPasswordByEmailCodeAsync(code, password) {
        return this.get({
            data: {
                code: code,
                newPwd: password,
                method: 'app.register.resetPwdByCode'
            }
        });
    }

    /**
     * 根据邮箱code修改密码(AES密码加密)
     */
    modifyPasswordByEmailCodeAsyncAes(code, password) {
        return this.get({
            data: {
                code: code,
                newPwd: aes.aesPwd(password),
                method: 'app.register.resetPwdByCodeAES'
            }
        });
    }

    modPwdByCodeAsync(params) {
        return this.get({
            data: Object.assign(params, {
                method: 'app.password.modPwdByCode',
                oldPwd: aes.aesPwd(params.oldPwd),
                newPwd: aes.aesPwd(params.newPwd)
            })
        });
    }
};