Source: lib/bank.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 = 'bank';
        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  bank
     * @param {Object} opts 配置属性
     * 格式:{uri:'网络地址'}
     */
    sdk.bank = {
        init: function (opts) {
            opts = opts || {};
            var uri = opts.uri;
            var prefix = opts.prefix || '/bank';
            this.uri = uri + prefix;
            jm.enableEvent(this);
            sdk.enableAjax(this,{timeout:opts.timeout||0});
        },

        /**
         * 获取账户信息
         * @function bank#query
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *  token: (可选, 如果不填,自动从storage中取值)
         *  userId: 用户id(sso.user.id, 可选,如果不填,取当前登陆用户)
         *  accountId: 账户id(可选, 如果不填,取用户默认帐户)
         *  ctCode: [要查询的币种编码数组](可选, 如果不填,取全部)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:{
         *  id: (帐户id)
         *  name: "账户名",
         *  holds: {
         *      (ctcode): {
         *           overdraw: 透支数量
         *           amount: 数量
         *           amountLocked: 被锁定数量
         *           amountValid: 有效数量
         *      }
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        query: 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 + '/query',
                data: opts
            }, cb);
        },

        /**
         * 获取账户信息列表
         * @function bank#accounts
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *  token: (可选, 如果不填,自动从storage中取值)
         *  userId: 用户id(可选,默认查询所有跟自己相关的账户)
         *  accountId: 账户id(可选,默认查询所有账户)
         *  page: 第几页(可选)
         *  rows: 一页显示几行(可选)
         *  search: 模糊搜索用户(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * cb参数格式:
         * result参数:{
         *      "page": 1,
         *      "pages": 2,
         *      "total": 3,
         *      "rows": [
         *          {
         *              "id": "账户id",
         *              "name": "账户名",
         *              "status": "账户状态",
         *               "user": {
         *                   "id": "用户id",
         *                   "userId": "用户ObjectId",
         *                   "name": "用户名",
         *                   .....
         *               },
         *               "holds": {
         *                   (ctcode): {
         *                       name: 币种名称
         *                       overdraw: 透支数量
         *                       amount: 数量
         *                       amountLocked: 被锁定数量
         *                       amountValid: 有效数量
         *                   }
         *               }
         *           }
         *      ]
         *     }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        accounts: 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 + '/accounts',
                data: opts
            }, cb);
        },

        /**
         * 更新账户状态
         * @function bank#status
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   userId: 用户ObjectId(必填).
         *   status: 状态(必填)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        status: 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 + '/account/status',
                data: opts
            }, cb);
        },

        /**
         * 预授权
         * @function bank#preauth
         * @param {Object} [opts={}] 参数
         * @example
         * userId,accountId必填之一
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   userId: 被预授权用户(可选,sso.user.id)
         *   accountId: 被预授权账户(可选,如果不填,取默认帐户),
         *   ctCode: 币种编码(必填)
         *   amount: 数量(必填,allAmount为1时可不填)
         *   allAmount: 转出全部(可选,为1时amount被忽略)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  ctCode: 币种,
         *  amount: 数量,
         *  amountValid: 有效余额,
         *  totalAmount: 总授权数量
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        preauth: 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 + '/preauth',
                data: opts
            }, cb);
        },

        /**
         * 预授权取消
         * @function bank#preauthCancel
         * @param {Object} [opts={}] 参数
         * @example
         * userId,accountId必填之一
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   userId: 被预授权用户(可选,sso.user.id)
         *   accountId: 被预授权账户(可选,如果不填,取默认帐户),
         *   ctCode: 币种编码(必填)
         *   amount: 数量(必填,allAmount为1时可不填)
         *   allAmount: 转出全部(可选,为1时amount被忽略)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  ctCode: 币种,
         *  amount: 数量,
         *  amountValid: 有效余额,
         *  totalAmount: 总授权数量
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        preauthCancel: 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 + '/preauth',
                data: opts
            }, cb);
        },

        /**
         * 预授权列表
         * @function bank#preauthList
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *  token: (可选, 如果不填,自动从storage中取值)
         *  page: 第几页(可选)
         *  rows: 一页显示几行(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        preauthList: 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 + '/preauth',
                data: opts
            }, cb);
        },

        /**
         * 转账
         * @function bank#transfer
         * @param {Object} [opts={}] 参数
         * @example
         * fromUserId,fromAccountId,toUserId,toAccountId必填之一
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   fromUserId: 转出用户(可选,sso.user.id,如果不填,取当前用户)
         *   fromAccountId: 转出账户(可选,如果不填,取默认帐户),
         *   toUserId: 转入用户(可选,sso.user.id,如果不填,取当前用户)
         *   toAccountId: 转入账户(可选,如果不填,取默认帐户),
         *   ctCode: 币种编码(必填)
         *   amount: 数量(必填,allAmount为1时可不填)
         *   allAmount: 转出全部(可选,为1时amount被忽略)
         *   payId: 支付单号(可选)
         *   attach: 附加信息(可选)
         *   orderId: 商户自编订单号(可选)
         *   memo: 备注信息(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  ctCode: 币种,
         *  amount: 数量
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        transfer: 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 + '/transfer',
                data: opts
            }, cb);
        },

        /**
         * 兑换
         * @function bank#exchange
         * @param {Object} [opts={}] 参数
         * @example
         * fromUserId,fromAccountId,toUserId,toAccountId都不填默认当前用户默认账户不同币种兑换
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   fromUserId: 转出用户(可选,sso.user.id,如果不填,取当前用户)
         *   fromAccountId: 转出账户(可选,如果不填,取默认帐户),
         *   toUserId: 转入用户(可选,sso.user.id,如果不填,取当前用户)
         *   toAccountId: 转入账户(可选,如果不填,取默认帐户),
         *   fromCTCode: 转出币种编码(必填)
         *   fromAmount: 转出多少(必填)
         *   toCTCode: 转入币种编码(必填)
         *   toAmount: 转入多少(必填)
         *   payId: 支付单号(可选)
         *   attach: 附加信息(可选)
         *   orderId: 商户自编订单号(可选)
         *   memo: 备注信息(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *   fromCTCode: 转出币种编码
         *   fromAmount: 转出多少
         *   toCTCode: 转入币种编码
         *   toAmount: 转入多少
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        exchange: 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 + '/exchange',
                data: opts
            }, cb);
        },

        /**
         * 更新账号密码
         * @function bank#updatePasswd
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   passwd: 旧密码(必填)
         *   newPasswd: 新密码(必填),
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        updatePasswd: 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 + '/updatePasswd',
                data: opts
            }, cb);
        },

        /**
         * 重置账号密码
         * @function bank#resetPasswd
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   key: 手机号(必填)
         *   code: 验证码(必填)
         *   passwd: 新密码(必填)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        resetPasswd: 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 + '/resetPasswd',
                data: opts
            }, cb);
        },

        /**
         * 获取交易历史
         * @function bank#history
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选, 如果不填,自动从storage中取值)
         *   userId: 用户id(可选,默认查询所有跟自己相关的交易记录,可传数组).
         *   startDate: 开始时间(可选).
         *   endDate: 结束时间(可选).
         *   ctCode: 币种(可选).
         *   statType: 统计类型(可选,0:按天,1:按月,2:按年).
         *   page: 第几页(可选).
         *   rows: 一页显示几行(可选).
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        history: 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 + '/history',
                data: opts
            }, cb);
        },

        /**
         * 密码是否有效
         * @function bank#isVaildPasswd
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选)
         *   userId: 用户id(可选,token不提供为必填)
         *   passwd: 密码(必填)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  ret:'true|false'
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        isVaildPasswd: function (opts, cb) {
            opts = opts || {};
            cb = cb || function () {};
            opts.token = opts.token || storage.getItem("token");
            //if (!opts.token) return cb(new Error('判断cp失败'), ERR.FA_NOAUTH);
            this.get({
                url: this.uri + '/isVaildPasswd',
                data: opts
            }, cb);
        },

        /**
         * 是否为cp用户
         * @function bank#isCP
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         *   token: (可选)
         *   userId: 用户id(可选)
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *  ret:'true|false'
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        isCP: function (opts, cb) {
            opts = opts || {};
            cb = cb || function () {};
            opts.token = opts.token || storage.getItem("token");
            //if (!opts.token) return cb(new Error('判断cp失败'), ERR.FA_NOAUTH);
            this.get({
                url: this.uri + '/isCP',
                data: opts
            }, cb);
        },

        /**
         * 币种间兑率
         * @function bank#exchangeRate
         * @param {Object} [opts={}] 参数
         * @example
         * opts参数:{
         * }
         * @param {callback} [cb=function(err,result){}] 回调
         * @example
         * result参数:{
         *   "cny:tb":10,
         *   "ny:jb":1000,
         *   "dbj:tb":10,
         *   "dbj:jb":10000
         *  }
         * err参数:{
         *  err: 错误码,
         *  msg: 错误信息
         * }
         */
        exchangeRate: 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 + '/exchangeRate',
                data: opts
            }, cb);
        }
    };

})();