|
本帖最后由 qw874038752 于 2018-5-2 16:50 编辑
最近搞服务端 有的时候瞎整哈哈感谢Jacen以及阿尔法的指导!!
废话不多说主要功能:
1.用户注册登录
2.服务端返回状态码
我自己是在IIS下弄得,数据库用的是mysql:
先上客户端很简单:
import win.ui;
import inet.whttp
import sys.storage;
import web.json
import console
/*DSG{{*/
mainForm = win.form(text="aardio form";right=591;bottom=463)
mainForm.add(
button={cls="button";text="登录";left=48;top=104;right=248;bottom=136;z=1};
button2={cls="button";text="注册";left=48;top=160;right=248;bottom=192;z=2}
)
/*}}*/
var whttp =inet.whttp() ;
var devices = sys.storage.getDevices()
for(idx,drives in devices){
property = sys.storage.queryProperty( idx );
if(!property) continue ;
}
mainForm.button.oncommand = function(id,event){
//这个是登录
var username ="qw874038752" ;
var password ="qw123123" ;
var mac =property.serialNumber ;
var html = whttp.post("http://localhost/api/login.aardio","username="+username+"&password="+password+"&mac="+mac) ;
var tab =web.json.parse(html) ;
if(tab.code==0){
win.msgbox(tab.data)
}else {
win.msgbox(tab.msg)
}
}
mainForm.button2.oncommand = function(id,event){
var username ="qw874038752" ;
var password ="qw123123" ;
var mac =property.serialNumber ;
var html = whttp.post("http://localhost/api/regin.aardio","username="+username+"&password="+password+"&mac="+mac) ;
var tab =web.json.parse(html) ;
if(tab.code==0){
win.msgbox(tab.msg)
}else {
win.msgbox(tab.msg)
}
}
mainForm.enableDpiScaling();
mainForm.show();
return win.loopMessage();
再来注册部分服务端:
import web.json
import carl.mysql
var server,database,uid,pwd = `xxxx`,`xxx`,`xx`,`xxxx`
var MysqlDB = carl.mysql( server,database,uid,pwd )
if(!MysqlDB.conn){
return '链接数据库失败';
}
if(request.post.password and request.post.mac ){
var username = request.post["username"]
var password = request.post["password"]
var mac = request.post["mac"] ;
if(!password or !username ){
return false; //这里是输入的账号和密码空号
}
var sql =`select * from wptsp where username='`+username+`' or mac='`+mac+`'`
var z = MysqlDB.getTable(sql)
if(#z){
//账号存在
r = {
code = 404;
msg = "注册失败";
}
response.write(web.json.stringify(r))
return ;
}
else {
//账号不存在
var tm=tostring(time())
var tt=tostring(time().addhour(3))
var sql = `insert into wptsp(username,password,mac,start,zzzz) values('`++username++`','`++password++`','`++mac++`','`++tm++`','`++tt++`');`
var ok,err = MysqlDB.exec(sql)
if(ok){
r = {
code = 0;
msg = "注册成功,免费体验3个小时";
}
}
else {
code = 100;
msg = "注册失败";
}
}
response.write(web.json.stringify(r))
}
再来服务端中登录部分:
import web.json
import carl.mysql
var server,database,uid,pwd = `xxxx`,`xx`,`xx`,`xx`
var MysqlDB = carl.mysql( server,database,uid,pwd )
if(!MysqlDB.conn){
return '链接数据库失败';
}
if(request.post.password and request.post.mac ){
var username = request.post["username"]
var password = request.post["password"]
var mac = request.post["mac"] ;
if(!password or !username ){
return false; //这里是输入的账号和密码空号
}
var sql =`select * from wptsp where username='`+username+`' and password='`+password+`'`
var z = MysqlDB.getTable(sql)
if(#z){
//账号存在
if(!z[1].mac==mac){
r = {
code = 404;
msg = "对不起已绑定其他电脑";
}
response.write(web.json.stringify(r))
return ;
}
var endtm=time(MysqlDB.getTable(sql)[1].zzzz,"%Y/%m/%d %H:%M:%S");
var tm=time.now()
if(endtm>=tm){
//未过期
r = {
code = 0;
msg = "登录成功,到期时间";
data=MysqlDB.getTable(sql)[1].zzzz
}
} else {
//过期
r = {
code = 100;
msg = "登录失败,已到期";
}
}
}
else {
//账号不存在
r = {
code = 900;
msg = "账号不存在或密码输入错误,登录失败";
}
}
response.write(web.json.stringify(r))
}
|
|