Source: lib/depot.js

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

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

    sdk.on('init', function (opts) {
        var model = 'depot';
        opts[model] = opts[model] || {};
        opts[model].uri = opts[model].uri || opts.uri;
        opts[model].timeout = opts[model].timeout || opts.timeout;
        sdk[model].init(opts[model]);
    });

    /**
     * 用户仓库对象
     * @class  depot
     * @param {Object} opts 配置属性
     * 格式:{uri:'网络地址'}
     */
    sdk.depot = {
        init: function(opts) {
            opts = opts || {};
            var uri = opts.uri;
            var prefix = opts.prefix || '/depot';
            this.uri = uri + prefix;
            jm.enableEvent(this);
            sdk.enableAjax(this,{timeout:opts.timeout||0});
        },

        /**
         * 获取用户道具信息
         * @function depot#listProps
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   page: 第几页(可选),
         *   rows: 一页几行(可选,默认10),
         *   appId: 应用id(可选)
         *   mode: (可选,默认分页方式)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * 结果结构一:{page:'第几页',rows:[{}],total:'总数',pages:'页数}
         * 结果结构二:{(code):{}}//以道具code为键,值为它的具体信息
         * {
         *   _id:'唯一id'
         *   userId:'用户ID',
         *   used: '使用状态',//该值不是必填项,只有useMode=0时才有效(0:未使用,1:使用中)
         *   prop:{ //道具具体信息
         *     logo:'图标地址',
         *     code:'道具编码',
         *     app:'应用id',//该值存在,代表只有在指定应用下才能使用
         *     name:'道具名称',
         *     description:'描述',
         *     type:'类型道具',//(0:礼包,1:虚拟币,2:道具)
         *     useMode:'使用方式',//(0:驻留品,1:消耗品,2:收集品)
         *     isStack:'是否堆叠的'
         *   }
         *   expire:'有效使用时间',
         *   amount:'可用数量'
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        listProps: function(opts, cb) {
            var self = this;
            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 + '/props',
                data: opts
            }, cb);
        },

        /**
         * 获取指定道具信息
         * @function depot#getProp
         * @param {Object} [opts={}] 参数
         * @example
         * id|propId|propCode必填之一
         * opts参数:{
         *   id: 唯一id(可选),
         *   propId: 道具id(可选),
         *   propCode: 道具编码(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * {
         *   _id:'唯一id'
         *   userId:'用户ID',
         *   used: '使用状态',//该值不是必填项,只有useMode=0时才有效(0:未使用,1:使用中)
         *   prop:{ //道具具体信息
         *     logo:'图标地址',
         *     code:'道具编码',
         *     app:'应用id',//该值存在,代表只有在指定应用下才能使用
         *     name:'道具名称',
         *     description:'描述',
         *     type:'类型道具',//(0:礼包,1:虚拟币,2:道具)
         *     useMode:'使用方式',//(0:驻留品,1:消耗品,2:收集品)
         *     isStack:'是否堆叠的'
         *   }
         *   expire:'有效使用时间',
         *   amount:'可用数量'
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        getProp: function(opts, cb) {
            var self = this;
            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 + '/prop',
                data: opts
            }, cb);
        },

        /**
         * 给予某用户道具
         * @function depot#giveProp
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   userId: 用户id(必填),
         *   prop: 道具id(可选,prop|ary必填之一),
         *   amount: 数量(可选,默认1),
         *   expire: 失效时间戳(可选,默认不失效,单位:毫秒)
         *   ary: 数组对象(可选,如该项提供优先采用;prop,amount,expire将不起作用,格式:[{prop:'id',amount:'数量',expire:''}]).
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * {
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        giveProp: function(opts, cb) {
            var self = this;
            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 + '/props',
                data: opts
            }, cb);
        },

        /**
         * 使用道具
         * @function depot#useProp
         * @param {Object} [opts={}] 参数
         * @example
         * id|propId|propCode必填之一
         * opts参数:{
         *   id: 唯一id(可选),
         *   propId: 道具id(可选),
         *   propCode: 道具编码(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * {
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        useProp: function(opts, cb) {
            var self = this;
            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 + '/useProp',
                data: opts
            }, cb);
        },

        /**
         * 销毁单个道具
         * @function depot#delProp
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   id: 唯一id(必填),
         *   amount: 指定数量(可选,默认全部)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * {
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        delProp: function(opts, cb) {
            var self = this;
            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 + '/prop',
                data: opts
            }, cb);
        },

        /**
         * 提取用户道具
         * @function depot#takeProp
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   userId: 用户id(必填),
         *   id: 唯一id(可选,id|ary必填之一),
         *   amount: 数量(可选,默认1),
         *   ary: 数组对象(可选,如该项提供优先采用;id,amount将不起作用,格式:[{id:'id',amount:'数量'}]).
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:
         * {
         * }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        takeProp: function(opts, cb) {
            var self = this;
            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 + '/take',
                data: opts
            }, cb);
        }
    };

})();