DreamFactory是一个可以为移动端、Web端和物联网终端等应用提供RESTAPI的免费、开源平台。 Dreamfactory可将数据库直接发布为REST服务,节约服务端开发成本(服务端开发为零),提供标准的
Dreamfactory可将数据库直接发布为REST服务,节约服务端开发成本(服务端开发为零),提供标准的REST API,支持的常见的SQL、NSQL商业数据库和文件存储系统。
因为官方提供的api不是很好用(主要是不能控制是否异步),本人自己封装了一套登陆、增删改查的方法,经历过几个大项目的测试。
DreamFactory 官方网站:https://www.dreamfactory.com/。
DreamFactory Github:https://github.com/dreamfactorysoftware/dreamfactory。
1. [代码][JavaScript]代码
var DfServer = 'http://192.168.100.216:8888/'; var AppName = 'myApp1'; 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 = "用户密码出错!"; if (response.content && response.content.data && response.content.data.error) { msg += "详细信息:" + response.content.data.error[0].message; } msg = msg.replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/g, '\''); alert(msg); } }); return loginState; } function getToken() { return $.cookie('GEO_Token'); } 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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/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 = "发生未知错误,但是服务器没有提供相关错误信息,请联系系统管理员!"; 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(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&').replace(/'/g, '\''); if (typeof error !== 'undefined' && error) { error(msg); } } }); }