login.js 8.21 KB
var request = require('request');
var _ = require('lodash');
var fs = require('fs');

var Iaccount = global.Register.system;

var Utils = {
    createrMenus: function (data) {
        var menu = [];
        for (var key in data) {
            
            var v = data[key];
            var item = {
                title: v.menu_name,
            }
            if (v.parent_id === "0") {
                item.parent = 'menu-template';
                var itemSubs = [];
                _.forEach(v.sub, function (val) {
                    var sub = {
                        title: val.menu_name,
                        href: val.menu_url,
                        icon: 'list-alt'
                    }
                    itemSubs.push(sub);
                });
                
                item.menu = itemSubs;
            }
            menu.push(item);
        }
        return menu;
    }
}

module.exports = {
    namespace: "common",
    apis: {
        Ilogin: {
            title:'登录接口',
            url: Iaccount.login,
            form: '["{{user}}","{{password}}",{{website}}]',
            params: {
                user: { type: String },
                password: { type: String },
                website: {type:Number,default: Iaccount.WEBSITE}
            }
        },
        getResourceByPid: {
            title:'菜单接口',
            url: Iaccount.getResourceByPid,
            form: '[{{pid}},{{role_id}},{{website}}]',
            params: {
                pid: { type: Number },
                role_id: { type: Number },
                website: { type: Number, default: Iaccount.WEBSITE }
            }
        },
        allRight: {
            title: '获取权限',
            url: Iaccount.allRight,
            form: '[false]'
        },
        getShopList: {
            title:'获取当前用户所有店铺',
            url: Iaccount.getShopList,
            form:true,
            headers: {
                "x-user-id": {type:Number}
            }
        },
        sessionAsync: {
            title:'与老系统session同步',
            url: Iaccount.url + '/login/sessions',
            outobj: "response",
            form: {'account':'{{user}}','password':'{{password}}','refer': '%2Faccount%2Fprofile%2Fdisplay'},
            params: {
                user: { type: String },
                password: { type: String }
            }
        },
        login: function (req, callback, common) {
            var userInfo = {},result = { code: 400, message: "登录失败" },isSuccess=false;
            var user = req.body.user;
            var password = req.body.password;
            var errcallback = function (err) {
                return callback(err, result);
            };
            var loginErrcallback = function (err) {
                result.needCaptcha = true;
                return callback(null, result);
            };
            //promise.all(Ilogin,allRight,sessionAsync).then(a,b,c);

            common.Ilogin(user, password).done(function (IloginRes) {
                var userData = IloginRes.data;
                userInfo = { auth: userData, uid: userData.pid, name: userData.truename, right: {},shopList:[]};

                common.allRight().sessionAsync(user, password).done(function (allRes,sesRes) {
                    var cookie = sesRes.caseless.dict["set-cookie"];
                    allRes.data.forEach(function (data) {
                        if (data.platform_id == Iaccount.WEBSITE) {
                            userInfo.right[data.path] = true;
                        }
                    });

                    common.getResourceByPid(userData.pid, userData.role_id)
                        .getShopList({ "@x-user-id": userInfo.uid }).done(function (getRRes, shopRes) {
                            userInfo.menu = Utils.createrMenus(getRRes.data);
                            if (shopRes.data) {
                                shopRes.data.forEach(function (data, index) {
                                    if (!index) {
                                        userInfo.auth.shopName = data.shopName;
                                        userInfo.auth.shopId = data.shopsId;
                                    }
                                    userInfo.shopList.push({
                                        name: data.shopName,
                                        id: data.shopsId
                                    });
                                });
                                req.session.user = userInfo;
                                if (cookie && cookie.length > 0) {
                                    req.session.gray = cookie[0];
                                    result = { code: 200, message: "登录成功" };
                                }
                                console.log(result);

                            } else {
                                result = { code: 400, message: "该用户没有店铺" };
                            }
                            return callback(null, result);
                        },errcallback);

                }, errcallback);

            }, loginErrcallback);

        },

        isUsedMenuAuth:{
            title:'检查有没有权限',
            url:Iaccount.isUsedMenuAuth,
            form:'[{{pid}},{{role_id}},"{{path}}","","",' + Iaccount.WEBSITE + ']',
            params:{
                pid:{type:Number},
                role_id:{type:Number},
                path:{type:String}
            }
        },
        gray: function (req, callback,common) {
            var result = { code: 201, message: "没有权限" };
            if (req.session && req.session.user) {
                var user = req.session.user;
                var path = req.route?req.route.path[0]:req.originalUrl.replace(/\?.+/, '');
                req._yoheaders = {
                    'x-user-id': user.auth.pid,
                    'x-user-name': user.auth.account,//防止header中包含中文
                    'x-site-type': Iaccount.WEBSITE,
                    'x-client-ip': req.ip,
                    'x-shop-id': user.auth.shopId
                };
                if (path && user.right[path]) {
                    common.isUsedMenuAuth(user.auth.pid,user.auth.role_id,path).done(function(isURes){
                        result = { code: 200, message: "具有权限" };
                        if(isURes.data){
                            result={code:isURes.data.code,message:isURes.data.errmsg};
                        }
                        return callback(null, result);
                    },function(error){
                        return callback(error, result);
                    });
                } else {
                    result = { code: 200, message: "不受权限控制" };
                    return callback(null, result);
                }
            } else {
                return callback(null, result);
            }
        },
        IchangeShops:{
            title:'调用老系统的切换店铺的接口',
            url: Iaccount.changeShops + '?shops_id={{shops_id}}',
            method:'GET',
            outobj:'response',
            headers: {
                cookie: { type: String }
            },
            params: {
                shops_id: { type: Number },
                '@cookie': {type:String}
            }
        },
        changeShop: function (req, callback,common) {
            var shopId = Number(req.query.shops_id);
            var cookie = req.session.gray;
            var result = { code: 500, message: "something wrong!" };
            
            //设置当前的店铺信息
            if (req.session.user && req.session.user.auth) {
                _.forEach(req.session.user.shopList, function (v, k) {
                    if (v.id === shopId) {
                        req.session.user.auth.shopName = v.name;
                        req.session.user.auth.shopId = shopId;
                    };
                });
            }
            common.IchangeShops(shopId,cookie).done(function(IchRes){
                if(IchRes.statusCode == 200){
                    result = { code: 200, message: "success" };
                    return callback(null, result);
                }else{
                    return callback(null, result);
                }
            });
        }
    }
}