Source: lib/sso.js

var jm = jm || {};
if ((typeof exports !== 'undefined' && typeof module !== 'undefined')) {
    require('jm-sso/dist/js/jm-sdk-sso');
    jm = require('jm-sdk-core');
}

(function () {
    var sdk = jm.sdk;
    var storage = sdk.storage;
    var ERR = sdk.consts.ERR;
    var sso = sdk.sso;

    /**
     * 获取验证码
     * @function sso#verifyCode
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  mobile:'手机号(必填)'
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     *  time:'验证码过期时间'
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.verifyCode = function (opts, cb) {
        cb = cb || function () {};
        this.get({
                url: this.uri + '/verifycode',
                data: opts
            },
            cb
        );
    };

    /**
     * 重置密码
     * @function sso#resetPasswd
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  key:'手机号(必填)',
     *  code:'验证码(必填)',
     *  passwd:'密码(必填)'
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.resetPasswd = function (opts, cb) {
        cb = cb || function () {};
        var self = this;
        this.post({
            url: this.uri + '/resetPasswd',
            data: opts
        }, cb);
    };


    /**
     * 绑定手机号
     * @function sso#bindMobile
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  code:'验证码(必填)',
     *  mobile:'手机号(必填)'
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.bindMobile = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        if (!opts.token) return cb(new Error('绑定手机号失败'), ERR.FA_NOAUTH);
        this.post({
            url: this.uri + '/bindMobile',
            data: opts
        }, cb);
    };

    /**
     * 获取用户列表
     * @function sso#getUsers
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  page: '第几页',
     *  rows: '一页几行',
     *  ids:['ObjectId'],
     *  keyword: '模糊查询关键字',
     *  fields: {}, //过滤字段
     *  sort: {} //按什么排序
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     *  rows:[]
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.getUsers = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        if (!opts.token) return cb(new Error('获取用户列表失败'), ERR.FA_NOAUTH);
        this.get({
                url: this.uri + '/users',
                data: opts
            },
            cb
        );
    };

    /**
     * 获取单个用户信息
     * @function sso#findUser
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  id: 'id',//(可选)
     *  account: '账户',//(可选)
     *  mobile: '手机号'//(可选)
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.findUser = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        //if (!opts.token) return cb(new Error('查找用户失败'), ERR.FA_NOAUTH);
        this.get({
                url: this.uri + '/findUser',
                data: opts
            },
            cb
        );
    };

    /**
     * 判断是否登录
     * @function sso#isSignon
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  token: '令牌',//(必填)
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     *  ret:true|false
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.isSignon = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        this.get({
                url: this.uri + '/isSignon',
                data: opts
            },
            cb
        );
    };

    /**
     * 是否开启效验码
     * @function sso#isCheckCode
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  token: '令牌',//(可选)
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     *  ret:true|false
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.isCheckCode = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        this.get({
                url: this.uri + '/isCheckCode',
                data: opts
            },
            cb
        );
    };

    /**
     * 移除指定用户
     * @function sso#removeUsers
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  id: ['用户ObjectId'],//(必填)
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.removeUsers = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        if (!opts.token) return cb(new Error('删除用户失败'), ERR.FA_NOAUTH);
        this.delete({
                url: this.uri + '/users',
                data: opts
            },
            cb
        );
    };

    /**
     * 创建用户
     * @function sso#createUser
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  token: token(可选, 如果不填,自动从localStorage获取),
     *  passwd: 密码(可选),
     *  account: 账号(可选),
     *  mobile: 手机号(可选),
     *  email: 邮箱(可选),
     *  nick: 昵称(可选),
     *  gender: 性别(可选),
     *  country: 国家(可选),
     *  province: 省份(可选),
     *  city: 城市(可选),
     *  area: 地区(可选),
     *  birthday: 生日(可选),
     *  signature: 签名(可选),
     *  headimgurl: 头像(可选)
     * }
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.createUser = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        if (!opts.token) return cb(new Error('创建用户失败'), ERR.FA_NOAUTH);
        this.post({
                url: this.uri + '/users',
                data: opts
            },
            cb
        );
    };

    /**
     * 更新用户信息
     * @function sso#updateUser
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  token: token(可选, 如果不填,自动从localStorage获取),
     *  userId: 指定用户OjbectId(可选,需要当前操作用户有相应权限)
     *  account: 账号(可选),
     *  mobile: 手机号(可选),
     *  email: 邮箱(可选),
     *  nick: 昵称(可选),
     *  gender: 性别(可选),
     *  country: 国家(可选),
     *  province: 省份(可选),
     *  city: 城市(可选),
     *  area: 地区(可选),
     *  birthday: 生日(可选),
     *  signature: 签名(可选),
     *  headimgurl: 头像(可选)
     * }
     * @param {callback} [cb=function(err, doc){}] 回调
     * @example
     * cb参数格式:
     * 成功, doc参数:{
     * }
     * 失败, doc参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.updateUser = function (opts, cb) {
        var self = this;
        cb = cb || function () {};
        opts = opts || {};
        var token = opts.token || storage.getItem('token');
        if (!token) return cb(new Error('更新用户信息失败'), ERR.FA_NOAUTH);
        opts.token = token;
        var p = '/user';
        if(opts.userId){
            p = '/users/'+opts.userId;
        }
        this.post({
                url: this.uri + p,
                data: opts
            }, cb
        );
    };

    /**
     * 排名
     * @function sso#ranking
     * @param {Object} [opts={}] 参数
     * @example
     * opts参数:{
     *  page:'第几页',
     *  rows:'一页几行',
     *  sort:'排名规则'
     * }
     * sort例子:
     * sort:{
     * 'record.jb':-1,
     * 'record.cny':-1,
     * 'record.tb':-1,
     * 'record.dbj':-1,
     * }
     * 注:-1代表降序,1代表升序,可组合
     * @param {callback} [cb=function(err,result){}] 回调
     * @example
     * cb参数格式:
     * result参数:{
     *  rows:[{}],
     *  me:'自己排名信息'
     * }
     * err参数:{
     *    'err': '错误码',
     *    'msg': '错误信息'
     * }
     */
    sso.ranking = function (opts, cb) {
        opts = opts || {};
        cb = cb || function () {};
        opts.token = opts.token || storage.getItem("token");
        this.get({
                url: this.uri + '/ranking',
                data: opts
            },
            cb
        );
    };

})();