text: 
  var models  = require('../models');
  var User    = models.User;
  var utility = require('utility');
  var uuid    = require('node-uuid');
  
  /**
   * 根据用户名列表查找用户列表
   * Callback:
   * - err, 数据库异常
   * - users, 用户列表
   * @param {Array} names 用户名列表
   * @param {Function} callback 回调函数
   */
  exports.getUsersByNames = function (names, callback) {
    if (names.length === 0) {
      return callback(null, []);
    }
    User.find({ loginname: { $in: names } }, callback);
  };
  
  /**
   * 根据登录名查找用户
   * Callback:
   * - err, 数据库异常
   * - user, 用户
   * @param {String} loginName 登录名
   * @param {Function} callback 回调函数
   */
  exports.getUserByLoginName = function (loginName, callback) {
    User.findOne({'loginname': new RegExp('^'+loginName+'$', "i")}, callback);
  };
  
  /**
   * 根据用户ID，查找用户
   * Callback:
   * - err, 数据库异常
   * - user, 用户
   * @param {String} id 用户ID
   * @param {Function} callback 回调函数
   */
  exports.getUserById = function (id, callback) {
    if (!id) {
      return callback();
    }
    User.findOne({_id: id}, callback);
  };
  
  /**
   * 根据邮箱，查找用户
   * Callback:
   * - err, 数据库异常
   * - user, 用户
   * @param {String} email 邮箱地址
   * @param {Function} callback 回调函数
   */
  exports.getUserByMail = function (email, callback) {
    User.findOne({email: email}, callback);
  };
  
  /**
   * 根据用户ID列表，获取一组用户
   * Callback:
   * - err, 数据库异常
   * - users, 用户列表
   * @param {Array} ids 用户ID列表
   * @param {Function} callback 回调函数
   */
  exports.getUsersByIds = function (ids, callback) {
    User.find({'_id': {'$in': ids}}, callback);
  };
  
  /*
  *@param:
  *@description:通过openid搜索
  *@param:2017/3/18
  *@author：yiweiguo
  */
  exports.getUsersByOpenId = function (id, callback) {
    User.findOne({'openId': id}, callback);
  };
  /**
   * 根据关键字，获取一组用户
   * Callback:
   * - err, 数据库异常
   * - users, 用户列表
   * @param {String} query 关键字
   * @param {Object} opt 选项
   * @param {Function} callback 回调函数
   */
  exports.getUsersByQuery = function (query, opt, callback) {
    User.find(query, '', opt, callback);
  };
  
  /**
   * 根据查询条件，获取一个用户
   * Callback:
   * - err, 数据库异常
   * - user, 用户
   * @param {String} name 用户名
   * @param {String} key 激活码
   * @param {Function} callback 回调函数
   */
  exports.getUserByNameAndKey = function (loginname, key, callback) {
    User.findOne({loginname: loginname, retrieve_key: key}, callback);
  };
  /*
  *@param:根据sessionid获取用户
  *@description:
  *@param:2017/3/18
  *@author：yiweiguo
  */
  exports.getUserBySessionId = function (sessionId, callback) {
    User.findOne({sessionId: sessionId}, callback);
  };
  
  exports.newAndSave = function (nickName, gender, language, city, province, country,avatarUrl,openId,SessionId, callback) {
    var user      = new User();
    user.nickName = nickName;
    user.gender   = gender;
    user.language = language;
    user.city     = city;
    user.province = province;
    user.country  = country;
    user.avatarUrl= avatarUrl;
    user.openId   = openId;
    user.sessionId= SessionId;
    user.save(callback);
  };
  
  /*
  *@param:
  *@description:更新sessionId或者新建用户
  *@param:2017/3/18
  *@author：yiweiguo
  */
  exports.updateOrSave=function (nickName, gender, language, city, province, country,avatarUrl,openId,sessionId,callback) {
      this.getUsersByOpenId(openId,function (err,user) {
        if(err){
          console.log(err)
        }
        else {
          if(user){
            user.sessionId=sessionId;
          }
          else{
            var user      = new User();
            user.nickName = nickName;
            user.gender   = gender;
            user.language = language;
            user.city     = city;
            user.province = province;
            user.country  = country;
            user.avatarUrl= avatarUrl;
            user.openId   = openId;
            user.sessionId= sessionId;
          }
          user.save(callback)
        }
      })
  }
  
  var makeGravatar = function (email) {
    return 'http://www.gravatar.com/avatar/' + utility.md5(email.toLowerCase()) + '?size=48';
  };
  exports.makeGravatar = makeGravatar;
  
  exports.getGravatar = function (user) {
    return user.avatar || makeGravatar(user);
  };
  