user-service.js 1.2 KB
/**
 * Created by TaoHuang on 2017/4/17.
 */

'use strict';

const Context = require('../common/context');
const Api = require('../common/api');
const apiDomain = global.yoho.apiDomain;
const co = global.yoho.co;

class loginModel extends Context {
    login(username, password) {
        let self = this;

        return co(function * () {
            let userInfo = yield self.instance(Api).post(
                apiDomain.auth.login.url,
                JSON.stringify([username, password, 2])
            );

            if (userInfo.code !== 200 || !userInfo.data.pid) {
                return Promise.reject({code: 500, message: '登录服务器错误'});
            }

            let shopInfo = yield self.profile(userInfo.data.pid)

            if (shopInfo.code !== 200) {
                return Promise.reject({code: 500, message: '用户获取店铺错误'});
            }

            let user = Object.assign(userInfo.data, {allowedShops: shopInfo.data});

            return {
                code: 200,
                data: user
            };
        })();
    }

    profile(pid) {
        return this.instance(Api).get(apiDomain.shop.profile.url, {userId: pid});
    }
}


module.exports = loginModel;