auth.js 3.81 KB
var oldService = require('./oldService');
var request = require('request');
var util = require('../../util/common');
var _ = require('lodash');

var WEBSITE = 1;//1表示平台端,2表示商户端 

/**
 * 登陆异常信息
 * @type {Object}
 */
var errorMessage = {
        code:50003,
        message:"登录失败"
};

/**
 * 登陆调用,返回菜单项和用户信息
 * @param  {String}   user     用户名
 * @param  {String}   password 密码
 * @param  {Object} res 返回对象
 * @return {void}
 */
 exports.callLogin = function(user,password,res) {
    util.setLogger(res.app.logger);
    var options = {
        url:oldService.login,
        form:{
               account:user,
               password:password,
               website:WEBSITE 
        }
    }

    //调用登陆
    util.httpCall(options,function(err,ret){
        if(err) {
          res.json(errorMessage);
          return;
        } else {
           //调用菜单
           _callGetMenu(ret.data.pid,function(err,data){
              if(err) {
                res.json(errorMessage);
              } else {
                var userData = ret.data?ret.data:{};
                var userInfo = {
                    auth:userData,
                    menu:data.menu,
                    noRight:data.noRight
                }
                userInfo.auth.uid = userData.pid;
                userInfo.auth.name = userData.truename;
                res.json({
                  code:20003,
                  data:userInfo
                });
              }
           });
        }
    });
}

/**
 * 根据pid获取菜单权限
 * @param  {Number}   pid     操作员ID
 * @param  {Function} callback 回调
 */
function _callGetMenu(pid,callback) {
    var options = {
        url:oldService.getResourceByPid,
        form:{
               pid:pid,
               website:WEBSITE 
        }
    }

    util.httpCall(options,function(err,ret){
        if(err) {
          callback(err);
        } else {
          var menuData  = {menu:[],right:{}};
          if(ret.data && ret.data.length>0) {
            menuData = _makeMenu(ret.data);
          }
          _getAllMenu(function(ret){
            menuData.noRight = filterRight(ret,menuData);
            delete menuData.right;
            callback(null,menuData);
          });
          
        }    
    });
}

/**
 * 筛选出没有的权限
 * @param  {[type]} ret [description]
 * @param {Object} menuData [description]
 * @return {[type]}     [description]
 */
function filterRight(ret,menuData) {
  var noRight = {};
  //匹配没有权限
  _.forEach(ret,function(v,k){
    if(v.module_url!=='') {
      if(!menuData.right[v.module_url]) {
        noRight[v.module_url] = true;
      }
    }
  });

  return noRight;
}

/**
 * 获取全部菜单
 * @param  {Function} callback 结果回调
 */
function _getAllMenu (callback) {
  var options = {
    url:oldService.getAllResByWebsite+'?website='+WEBSITE+'&sort=',
    method:'GET'
  }

  util.httpCall(options,function(err,ret){
    if(err) {
      callback({});
    } else {
      callback(ret.data);
    }
  })
}


/**
 * 组装菜单
 * @param  {Array} data 菜单数据
 */
function _makeMenu (data) {
    var menu = [];
    var right = {};
    _.forEach(data,function(v){
        var item = {
            title:v.resource_name,
        }
        if(v.parent_id === "0") {
            item.parent = 'menu-template';
            var itemSubs = [];
            _.forEach(v.sub,function(val) {
                var sub = {
                    title:val.resource_name,
                    href:val.module_url,
                    icon: 'list-alt'
                }
                right[val.module_url] = true;
                itemSubs.push(sub);
            });

            item.menu = itemSubs;
        }
        menu.push(item);
    });

    return {
      menu:menu,
      right:right
    };
}