{"size": 3673, "ext": "js", "max_stars_repo_path": "public/js/admin/util.js", "max_stars_repo_name": "aixxx/mp", "max_stars_repo_stars_event_min_datetime": "2018-05-03T04:31:52.000Z", "max_stars_repo_stars_event_max_datetime": "2018-05-03T04:31:52.000Z", "max_issues_repo_path": "public/js/admin/util.js", "max_issues_repo_name": "aixxx/mp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/js/admin/util.js", "max_forks_repo_name": "aixxx/mp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.0381679389, "max_line_length": 82, "alphanum_fraction": 0.4138306561}
/**
 * 工具库
 *
 * @author overtrue <anzhengchao@gmail.com>
 */
define(['jquery'], function(){
    window.__page = 0;

    // 后台基础 URI
    var baseURI = baseURI || '/admin/';
    var util = {
        // 生成后台uri
        // example: /admin/user
        makeUri: function(uri){
            return baseURI.trim('/') + uri;
        },

        // 表单错误
        formError: function($obj, $errors) {
            $obj.find('.has-error').removeClass('has-error');
            _.mapObject($errors, function(msg, field) {
                var formInput = $obj.find('[name='+field+']');
                formInput.siblings('span.help-block').remove();
                formInput.after('<span class="help-block red">'+msg+'</span>');
                formInput.closest('.form-group').addClass('has-error');
            });
        },

        // ajax 请求
        request: function($method, $uri, $data, $success, $error){
            var params = {
                url: util.makeUri($uri),
                type: $method || 'GET',
                dataType: 'json',
                data: $data,
            };

            var success = function($resp){
                window.last_response = $resp;

                console.log('request success:' + params.url, $resp);

                if (typeof $resp['current_page'] != 'undefined') {
                    window.__page = $resp.current_page;
                    $resp = $resp.data;
                };

                if (typeof $success == 'function') {
                    $success($resp);
                };
            };

            var err = function($err){
                window.last_response = $err;

                if($err.status == 401){
                    window.location.href = util.makeUri('admin/auth/login');
                }

                error('网络错误...', $err.status + ' ' + $err.statusText);

                console.log('request error:', $err);

                if (typeof $error == 'function') {
                    $error($err);
                }
            };

            $('.loading').show();

            console.log('request begin:', params);

            $.ajax(params).done(success).fail(err).always(function() {
                console.log('request finished:', params.url);
                $('.loading').hide();
            });
        },

        // 获取表单内的值对象
        parseForm: function (form) {
            var formdata = form.serializeArray();

            var data = {};

            _.each(formdata, function (element) {

                var value = _.values(element);

                // Parsing field arrays.
                if (value[0].indexOf('[]') > 0) {
                    var key = value[0].replace('[]', '');

                    if (!data[key])
                        data[key] = [];

                    data[value[0].replace('[]', '')].push(value[1]);
                } else

                // Parsing nested objects.
                if (value[0].indexOf('.') > 0) {

                    var parent = value[0].substring(0, value[0].indexOf("."));
                    var child = value[0].substring(value[0].lastIndexOf(".") + 1);

                    if (!data[parent])
                        data[parent] = {};

                    data[parent][child] = value[1];
                } else {
                    data[value[0]] = value[1];
                }
            });

            return data;
        },

        // 反转对象
        reverseObject: function ($obj) {
            var $temp = {};

            Object.keys($obj).reverse().forEach(function($key){
                $temp[$key] = $obj[$key];
            });;

            return $temp;
        }
    };


    return util;
});