common.login.js 9.99 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:{
 		login:function(req,callback){
 			var user = req.body.user;
	        var password = req.body.password;
	        var userInfo = {},result={code:400,message:"登录失败"};
            //调用登陆
            console.log(Iaccount.login);
	       return request.post({
                url: Iaccount.login,
        		form:'["'+user+'","'+password+'",'+Iaccount.WEBSITE+']'
	        },function(error, httpResponse, rebody){
                if (!error && httpResponse.statusCode == 200) {
                    var userData = JSON.parse(rebody).data;
                    console.log("登录接口成功");
                    userInfo = {
                        auth: userData,
                        uid: userData.pid,
                        name: userData.truename
                    };
                    //调用菜单
                    request.post({
                        url: Iaccount.getResourceByPid,
                        form: '[' + userData.pid + ',' + userData.role_id + ',' + Iaccount.WEBSITE + ']'
                    }, function (error1, httpResponse1, rebody1) {
                        if (!error1 && httpResponse1.statusCode == 200) {
                            userInfo.menu = Utils.createrMenus(JSON.parse(rebody1).data);
                            console.log("调用菜单成功,开始调用权限:"+Iaccount.allRight);
                            //调用权限
                            request.post({
                                url: Iaccount.allRight,
                                form: '[false]'
                            }, function (error2, httpResponse2, rebody2) {
                                console.log("调用权限"+rebody2);
                                if (!error2 && httpResponse2.statusCode == 200&&rebody2) {
                                    
                                    userInfo.right = {};
                                    JSON.parse(rebody2).data.forEach(function (data) {
                                        if (data.platform_id == Iaccount.WEBSITE) {
                                            userInfo.right[data.path] = true;
                                        }
                                    });
                                    //获取店铺
                                    console.log("店铺URL:"+Iaccount.getShopList);
                                    request.post({
                                        'url': Iaccount.getShopList,
                                        'headers': {
                                            "x-user-id": userInfo.uid
                                        }
                                    }, function (error3, httpResponse3, rebody3) {
                                        console.log("获取店铺");
                                        console.log(rebody3);
                                        if (!error3 && httpResponse3.statusCode == 200) {
                                            userInfo.shopList = [];
                                            if (JSON.parse(rebody3).data) {
                                                JSON.parse(rebody3).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
                                                    });
                                                });
                                            } else { 
                                                result = { code: 400, message: "该用户没有店铺" };
                                            }
                                            
                                            
                                            /*保存session 成功*/
                                            req.session.user = userInfo;
                                            console.log("保存session 成功");
                                            
                                            request.post({
                                                'url': Iaccount.url+'/login/sessions',
                                                form: {
                                                    'account': user,
                                                    'password': password,
                                                    'refer': '%2Faccount%2Fprofile%2Fdisplay'
                                                }
                                            }, function (error4, httpResponse4, rebody4) {
                                                if (!error4) {
                                                    var cookie = httpResponse4.caseless.dict["set-cookie"];
                                                    if (cookie && cookie.length > 0) {
                                                        req.session.gray = cookie[0];
                                                    }
                                                    result = { code: 200, message: "登录成功" }
                                                    return callback(null, result);
                                                } else { 
                                                    return callback(null, result);
                                                }
                                            });
                                        } else {
                                            console.log(error3);
                                            return callback(null, result);
                                        }
                                    });
                                } else { 
                                    return callback(null, result);
                                }
                            });
                        } else {
                            return callback(null, result);
                        }
                    });
                } else { 
                    return callback(null, result);
                }
	        });
 		},
        gray: function (req, callback){
            var result = { code: 400, 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,
                    'x-site-type': Iaccount.WEBSITE,
                    'x-client-ip': req.ip,
                    'x-shop-id': user.auth.shopId
                };
                if (path && user.right[path]) {
                    request({
                        url: Iaccount.isUsedMenuAuth,
                        method: 'POST',
                        form: '[' + user.auth.pid + ',' + user.auth.role_id + ', "' + path + '", "", "", ' + Iaccount.WEBSITE + ']'
                    }, function (error, httpResponse, rebody) {
                        if (!error && httpResponse.statusCode == 200) {
                            result = { code: 200, message: "具有权限" };
                            return callback(error, result);
                        } else {
                            return callback(error, result);
                        }
                    });
                } else {
                    result = { code: 200, message: "不受权限控制" };
                    return callback(null, result);
                }
            } else { 
                return callback(null,result);
            }
        },
        changeShop: function (req, callback) {
            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;
                    };
                });
            }
            //调用老系统的切换店铺的接口
            request.post({
                'method': 'GET',
                'headers': {
                    'cookie': cookie
                },
                'url': Iaccount.changeShops + '?shops_id=' + shopId
            }, function (error, httpResponse, rebody) {
                if (!error && httpResponse.statusCode == 200) {
                    result = { code: 200, message: "success" };
                    return callback(null, result);
                } else { 
                    return callback(null, result);
                }
            });
        }
 	}
 }