dreamfactory开源平台的使用
dreamfactory开源平台的使用
仰望星空 发表于1年前
dreamfactory开源平台的使用
  • 发表于 1年前
  • 阅读 67
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: DreamFactory是一个可以为移动端、Web端和物联网终端等应用提供REST API的免费、开源平台。Dreamfactory可将数据库直接发布为REST服务,节约服务端开发成本(服务端开发为零),提供标准的REST API,支持的常见的SQL、NSQL商业数据库和文件存储系统。因为官方提供的api不是很好用(主要是不能控制是否异步),本人自己封装了一套登陆、增删改查的方法,并经历过几个大项目的测试。DreamFactory 官方网站:https://www.dreamfactory.com/。

代码如下,感兴趣的可以直接QQ我(744757242):

var DfServer = 'http://192.168.100.216:8888/';
var AppName = 'myApp1';

function getToken() {
    return $.cookie('GEO_Token');
}

function loginUser(name, pwd) {
    var url = DfServer + 'rest/user/session';
    var data = {
        'email': name,
        'password': pwd
    };
    var loginState = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        url: url,
        data: JSON.stringify(data),
        cache: false,
        async: false,
        type: 'POST',
        headers: {
            "X-DreamFactory-Application-Name": AppName
        },
        success: function (response) {
            $.cookie("GEO_USERNAME", name, { expires: 1, path: '/' });
            $.cookie("GEO_Token", response.session_id, { expires: 1, path: '/' });
            loginState = true;
            $.cookie("first_name", response.first_name, { expires: 1, path: '/' });
            $.cookie("last_name", response.last_name, { expires: 1, path: '/' });
            $.cookie("display_name", response.display_name, { expires: 1, path: '/' });
        },
        error: function (response) {
            var msg = "account erro!";
            if (response.content && response.content.data && response.content.data.error) {
                msg += "detail::" + response.content.data.error[0].message;
            }
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            alert(msg);
        }
    });
    return loginState;
}

function getRecordsByFilter(database, table, filter, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    var data = null;
    if (filter == null || filter == "" || filter == undefined)
        data = null;
    else
        data = { filter: filter };
    if (async == null)
        async = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        url: url,
        data: data,
        cache: false,
        async: async,
        type: 'GET',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

function getRecordsByFilterOrder(database, table, filter, order, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    var data = null;
    if (!filter)
        filter = "";
    data = { filter: filter, order: order };
    if (async == null)
        async = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        url: url,
        data: data,
        cache: false,
        async: async,
        type: 'GET',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

function getPageRecordsByFilterOrder(database, table, filter, order, limit, offset, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    var data = null;
    if (!filter)
        filter = "";
    data = { filter: filter, order: order, include_count: true, limit: limit, offset: offset };
    if (async == null)
        async = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        url: url,
        data: data,
        cache: false,
        async: async,
        type: 'GET',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

function addRecords(database, table, data, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    if (data == null || data == "" || typeof data == 'undefined')
        return;
    if (async == null)
        async = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json;',
        url: url,
        data: JSON.stringify(data),
        async: async,
        type: 'POST',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

function updateRecords(database, table, data, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    if (data == null || data == "" || typeof data == 'undefined')
        return;
    if (async == null)
        async = false;
    $.ajax({
        dataType: 'json',
        contentType: 'application/json;',
        url: url,
        data: JSON.stringify(data),
        cache: false,
        async: false,
        type: 'PUT',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

function deleteRecordsByFilter(database, table, filter, success, error, async) {
    var url = DfServer + 'rest/' + database + '/' + table;
    var token = getToken();
    if (filter == null || filter == "" || typeof filter == 'undefined')
        return;
    if (async == null)
        async = false;
    var data = { filter: filter };
    $.ajax({
        dataType: 'json',
        contentType: 'application/json;',
        url: url,
        data: JSON.stringify(data),
        cache: false,
        async: false,
        type: 'DELETE',
        headers: {
            "X-DreamFactory-Application-Name": AppName,
            "X-DreamFactory-Session-Token": token
        },
        success: function (response) {
            if (typeof success !== 'undefined') {
                success(response);
            }
        },
        error: function (response) {
            var msg = "unknown error!";
            if (response.content && response.content.data && response.content.data.error)
                msg = response.content.data.error[0].message;
            else if (response.responseText)
                msg = JSON.stringify(response.responseText);
            msg = msg.replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&').replace(/&apos;/g, '\'');
            if (typeof error !== 'undefined' && error) {
                error(msg);
            }
        }
    });
}

 

共有 人打赏支持
仰望星空
粉丝 3
博文 3
码字总数 2017
作品 1
×
仰望星空
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: