国产编程语言CBrother的初见

国产编程语言CBrother脚本

前段时间一直纠结使用何种编程语言开发自己的后台程序。因为干程序猿也好几个年头了,搞过C++、Java、Js等, 所以最初想选择Java框架或者NodeJs(Php语法不熟悉就忽略不计),但是作为一个初学者,Java框架开发难度大并不适合我这等初学者、同时也了解到NodeJs在开发方面CPU 和内存上有先天性的缺陷。所以经过一段时间的寻找,终于找个一个国内大神写的编程语言CBrother, 因为有语法基础,在加上大神详细的文档,让我的开发事半功倍。
官方网址

创建工程

首先,从官网上下载对应的版本,解压到自己的目录。如我解压到了C:\webwork(因为我使用腾讯云Window2008R2, 下载的是cbrother-64.zip)

启动服务

在C:\webwork项目根目录,并新建txt文件,重命名为main.cb, 在main.cb文件里写入下面的代码, 启动服务。

import CBHttp.code
function main(parm) {
	var httpServer = new HttpServer();
	httpServer.startServer();
	
	while(1)				//主线程不能退出
	{
		Sleep(1000);
	}
}

在命令行窗口输入如下:
C:\webwork\cbrother.exe -run -rootpath=C:\webwork\main-code=main.cb -parm=1
服务启动成功!!

实现动态登录接口

function main(parm)
{
	var httpServer = new HttpServer();
	httpServer.addAction("login.cb",new LoginAction());	//注册登录接口logIn.cb,响应类是LoginAction
	/*
	* 如上所示
	*/
}

接下来实现LoginAction接口**(用于微信sdk登录)**

class LoginAction
{
	var m_HttpServer = null;	

	function DoAction(req,respon)
	{	
		m_HttpServer.Trace("LoginAction in!!!");
		var data = req.GetData();		
		if (data == null) 
		{
			var idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();
			return;
		}
		
		/* 微信登录 */
		
		//解析json
		var logjson = new Json(data);
		var wxcode = logjson.get("code");
		var rawData = logjson.get("rawData");
		var signature = logjson.get("signature");
		var encryptedData = logjson.get("encryptedData");
		var iv = logjson.get("iv");
		if(wxcode == null || wxcode == null || rawData == null || signature == null || encryptedData == null || iv == null)
		{
			idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();			
			m_HttpServer.Trace("LoginAction out!!!");
			return;
		}
		
		/* HttpClientRequest来主动访问微信第三方接口 */
		
		var clientReq = new HttpClientRequest();
		clientReq.SetMethod(HTTP_GET);
		
		var requrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret=" + APP_SECRET + "&js_code=" + wxcode + "&grant_type=authorization_code";
		clientReq.SetUrl(requrl);
		var clientResponse = clientReq.Flush();		
		var staus = clientResponse.GetStaus();
	
		if(staus != HTTP_OK)
		{
			idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();			
			m_HttpServer.Trace("LoginAction out!!!");
			return;
		}
		
		var wxdata = clientResponse.GetData();
		var wxjson = new Json(wxdata);
		
		var errcode = wxjson.get("errcode");
		if(errcode != null)
		{
			idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();		
			m_HttpServer.Trace("LoginAction out!!!");
			return;
		}
		
		var openid = wxjson.get("openid");
		var session_key = wxjson.get("session_key");
		var unionid = wxjson.get("unionid");

		/* 使用OpenSSL 加密算法 */
		
		var sha1str = openssl_sha1(rawData + session_key);
		if(sha1str != signature)
		{
			idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();		
			m_HttpServer.Trace("LoginAction out!!!");
			return;		
		}

		var userdatajson = decryptData(APP_ID,session_key,iv,encryptedData);
		if(userdatajson == null)
		{
			idata = new Json();
			idata.add("status", SERVER_READ_FAILED);
			respon.Write(idata.toJsonString());			
			respon.Flush();	
			m_HttpServer.Trace("LoginAction out!!!");
			return;			
		}
		
		var userjson = new Json(userdatajson);
		var res = m_HttpServer.Command(m_UserDataIdx, LOGIN_USERDATA, userjson);
		var result = new Json();

		if(res > 0)
		{
			/* 服务器Cookie */
			
			respon.SetCookie("" + res);
			result.add("status", SERVER_CODE_SUCCESS);
			result.add("uid",res);
			respon.Write(result.toJsonString());
		}
		else
		{
			result.add("status", SERVER_CODE_FAILED);
			respon.Write(result.toJsonString());
		}

		respon.Flush();		
		m_HttpServer.Trace("LoginAction out!!!");
	}

是不是很简单!!!

MySQL的支持

之前对MySQL的了解不是很多,为了学习后台开发,专门花费了些许时间系统学习了MySQL,对一个新手来说困难可想而知,有一段时间都想放弃。但是自从遇到CBrother后,它对MySQL开发了扩展库,让我的学习成本一下降低了很多(强大的扩展库

初学者建议

这是一门很不错的国产编程大神之作,也希望推荐给和我一样的初学者。

本人愚见,还请各位大神多多指教!!!
同时也感谢CBrother作者对于我项目的支持!!!

期待和大家分享下一篇关于CBrother脚本开发经验。

猜你喜欢

转载自blog.csdn.net/brewchen/article/details/88317245