bmob后端云简单封装

By matresnan at 2020-12-07 • 0人收藏 • 1114人看过
// bmob后端云
import inet.http;
import web.json;
import inet.url;
import console;


class bmob{
	ctor(applicationId, apiKey){
		if(applicationId){
			this.applicationId = applicationId
		}else{error(
			"applicationId不能为空,获取应用密钥请访问网址:http://doc.bmob.cn/"
		)}
		if(apiKey){
			this.apiKey = apiKey
		}else{error(
			"apiKey不能为空,获取应用密钥请访问网址:http://doc.bmob.cn/"
		)}
	};
	
	requests = function(method, url, json){
		var url = "https://api2.bmob.cn/1" + url
		var http = ..inet.http()
		var headers = {
			["X-Bmob-Application-Id"] = this.applicationId;
			["X-Bmob-REST-API-Key"] = this.apiKey;
			["Content-Type"] = "application/json"
		}
		var data, errorMsg, statusCode = http.down(url, json, headers,,, method)
		if(data){
			return ..web.json.parse(data);
		}else{
			return false; 
		}
	};
	// 获取服务器时间
	timestamp = function(){
		return this.requests("GET", "/timestamp", null);
	};
	//----------查询相关----------
	// 根据对象id查询
	getObject = function(className, odjectId){
		return this.requests("GET", "/classes/" + className + "/" + odjectId, null);
	}
	// 查询表
	getObjects = function(className){
		return this.requests("GET", "/classes/" + className, null);
	}
	// 条件查询
	queryObject = function(className, data){
		var str = ..inet.url.encodeMbcs(..string.format("/classes/%s/?where=%s", className, data))
		return this.requests("GET", str, null);
	}
	//----------添加数据----------
	// 添加单条数据
	createObject = function(className, data){
		return this.requests("POST", "/classes/" + className, data);
	}
	// 批量添加数据
	createObjects = function(items){
		return this.requests("POST", "/batch", items);
	}
	//----------更新数据----------
	updateObject = function(className, odjectId, data){
		return this.requests("PUT", "/classes/" + className + "/" + odjectId, data);
	}
	//----------删除数据----------
	deleteObject = function(className, odjectId){
		return this.requests("DELETE", "/classes/" + className + "/" + odjectId);
	}
	
}


/**intellisense()
bmob() = 创建bmob对象\n简单实现了bmob后端云添加、删除、更新等功能\n其他功能请自行修改,细节问题请参考文档:http://doc.bmob.cn/data/restful/develop_doc/
end intellisense**/

/**intellisense(bmob)
timestamp() = 获取bmob后端云服务器时间
getObject(tableName, odjectId) = 根据对象id查询数据
getObjects(tableName) = 获取指定表全部数据
queryObject(tableName, queryData) = 条件查询,条件格式为json
createObject(tableName, data) = 添加单条数据\n提交的数据格式为json,可用web.json.stringify()函数把aardio表转化成json文本
createObjects(data) = 批量添加数据\n提交的数据格式为json,可用web.json.stringify()函数把aardio表转化成json文本
updateObject(tableName, odjectId, data) = 更新数据\nodjectId可为对象id,也可以为where条件,条件也为json格式\n例如:?where={...}
deleteObject(tableName, odjectId) = 删除数据\nodjectId可为对象id,也可以为where条件,条件也为json格式\n例:?where={...}
end intellisense**/

近期由于业务需要用到这个云数据库,所以简单封装成库,同时分享出来

本人第一次写库文件,代码写的不好,各位见笑了,也希望各位大佬指正,如果有更好的想法,也可以反馈给我


2 个回复 | 最后更新于 2020-12-09
2020-12-07   #1

作者偶尔路过,多谢分享。

2020-12-09   #2
import web.rest.jsonClient;

class bmob{
	ctor( appId,apiKey ){
		var http = ..web.rest.jsonClient();
		
		http.addHeaders = {
			["X-Bmob-Application-Id"] = appId;
			["X-Bmob-REST-API-Key"] = apiKey
		}
		
		this = http.api("https://api2.bmob.cn/1/")
		this["http"] = http; 
	};
}

这样子就搞定了!web.rest 就是把rest api 转化为 aardio函数的 

标准库和扩展库有很多rest使用的例子

使用:

import console;

_appId = "1jkhsajasl983klkjlk34k555nkjaskdjk" //Your Application ID
_apiKey = "sakjhksajkakshkhkjhkhkhk726939897" //Your REST API Key

var bmob = bmob(_appId, _apiKey); 

var timestamp = bmob.timestamp.get() 
console.dumpJson( timestamp )

console.pause(true);


当然常用的函数 你可以进一步封装

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...