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

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

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

/**
 * 切换店铺调用老服务
 * @param {Number} shopId 店铺ID
 * @param {String} cookie 
 */
exports.change = function(shopId, cookie, callback) {
  util.httpCall({
    'method': 'GET',
    'headers': {
      'cookie': cookie
    },
    'url': oldService.changeShops + '?shops_id=' + shopId
  }, callback);
}

/**
 * 登陆调用,返回菜单项和用户信息
 * @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:'["'+user+'","'+password+'",'+WEBSITE+']'//'["zhiyuan","lzy111111",1]'//
    }

  //调用登陆
  util.httpCall(options, function(err, ret) {
    if (err) {
      res.json(errorMessage);
      return;
    } else {
      //调用菜单
      _callGetMenu(ret.data.pid,ret.data.role_id, function(err, data) {
        if (err) {
          res.json(errorMessage);
        } else {
          var userData = ret.data ? ret.data : {};
          var userInfo = {
              auth: userData,
              menu: data.menu,
              allRight: data.allRight
            }
            //获取店铺信息
          _getShopList(userData.pid, function(err, data) {
            userInfo.auth.uid = userData.pid;
            userInfo.auth.name = userData.truename;
            console.log("-----------------------------------");
            console.log(data);

            //如果店铺信息存在,默认设置第一个店铺为当前的店铺
            if (data && data.length > 0) {
              userInfo.auth.shopName = data[0].name;
              userInfo.auth.shopId = data[0].id;
            }

            //设置店铺列表
            userInfo.shopList = data;

            res.json({
              code: 20003,
              data: userInfo
            });
          });
        }
      });
    }
  });
}

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

  util.httpCall(options, function(err, ret) {
    if (err) {
      callback(err);
    } else {
      var menuData  = {menu:[],right:{}};
      if(ret.data) {
        menuData = _makeMenu(ret.data);
      }
      _getAllRight(function(ret){
          menuData.allRight = ret;
          callback(null,menuData);
        });

    }
  });
}

/**
 * 获取当前操作员的店铺信息
 * @param {int} pid 操作员ID
 * @param {function} callback 回调
 */
function _getShopList(pid, callback) {
  util.httpCall({
    'url': oldService.getShopList,
    'headers': {
      "x-user-id": pid
    }
  }, function(err, data) {
    if (err) {
      callback(err);
    } else {
      var ret = [];
      if (!data.data) {
        callback(ret);
        return;
      }

      //格式化店铺信息
      _.forEach(data.data, function(v, k) {
        ret.push({
          name: v.shopName,
          id: v.shopsId
        });
      });
      callback(null, ret);
    }
  });
}

/**
 * 筛选出没有的权限
 * @param  {[type]} ret [description]
 * @param {Object} menuData [description]
 * @return {[type]}     [description]
 */
function filterRight(ret,menuData) {
  var noRight = {};
  //匹配没有权限
  for(var key in ret){
    var item=ret[key];
    for(var i in item.sub){
      var v=item.sub[i];
      if(v.menu_url!=='') {
        if(!menuData.right[v.menu_url]) {
          noRight[v.menu_url] = true;
        }
      }
    }
  }
  return noRight;
}

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

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


/**
 * 组装菜单
 * @param  {Array} data 菜单数据
 */
function _makeMenu (data) {
    var menu = [];
    var right = {};
    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'
                }
                right[val.menu_url] = true;
                itemSubs.push(sub);
            });

            item.menu = itemSubs;
        }
        menu.push(item);
    }
    return {
      menu:menu,
      right:right
    };
} 


/**
* 验证权限
*/
exports.validateAuthor=function(pid,roleid,path,callback){
  var options = {
    url:oldService.isUsedMenuAuth,
    form:'['+pid+','+roleid+', "'+path+'", "", "", '+WEBSITE+']'
  }
  console.log(options);
  util.httpCall(options,function(err,ret){
    if(err) {
      callback({});
    } else {
      callback(ret.data);
    }
  });
}

function _getAllRight(callback){
  var options = {
    url:oldService.allRight,
    form:'[false]'
  }
   console.log(options);
  util.httpCall(options,function(err,ret){
    if(err) {
      callback({});
    } else {
      var map={};
      ret.data.forEach(function(data){
        console.log(data);
        if(data.platform_id==WEBSITE){
          map[data.path]=true;
        }
        
      });
      callback(map);
    }
  });
}