#include "bs_util.h" #include "handler.h" using namespace std; map> _handlers; int RequestDispatch(Request& req,Session& se,Response& res,json& j,int Key) { if(_handlers.find(Key)!=_handlers.end()) { if(_handlers[Key]) { j["RDKey"]=Key; _handlers[Key](req,se,res,j); return 0; } else { jsonfail_func(j,err_general,"No action is associated to key."); return -1; } } else { jsonfail_func(j,err_general,"Failed to find appropriate action to key."); return -1; } } #define REG_HANDLER(fn,key) _handlers.insert(make_pair(key,fn)) void InitHandlers() { REG_HANDLER(UserLogin,1); /// 用户登录 REG_HANDLER(UserLogout,2); /// 用户登出 REG_HANDLER(CheckLogin,3); /// 检查是否已登录 REG_HANDLER(CheckLoginX,4); /// 检查是否已登录(专用于登录页面,用于快速登录) REG_HANDLER(UserReaderAdd,5); /// 添加用户(同时添加读者信息) REG_HANDLER(UserReaderEdit,6); /// 修改用户(除管理员外) REG_HANDLER(UserReaderRemove,7); /// 删除用户(仅能删除读者账户) REG_HANDLER(UserSearch,8); /// 用户搜索 REG_HANDLER(UserReaderGet,9); /// 账户读者信息获取 //REG_HANDLER(UserReaderGetLow,10); /// 账户读者信息获取(低权限) REG_HANDLER(BookSearch,11); /// 图书搜索 REG_HANDLER(BookGetInfo,12); /// 获取图书信息 REG_HANDLER(BookGetObjInfo,13); /// 获取图书实体信息 REG_HANDLER(BookGetBorrowed,14); /// 获取已借阅的书籍 REG_HANDLER(BookObjAdd,15); REG_HANDLER(BookObjEdit,16); REG_HANDLER(BookObjRemove,17); REG_HANDLER(BookBorrow,21); /// 图书借阅 REG_HANDLER(BookReturn,22); /// 图书归还 REG_HANDLER(BookTypeGet,30); /// 查询已存在的图书种类 REG_HANDLER(BookTypeAdd,31); /// 添加图书种类 REG_HANDLER(BookTypeEdit,32); /// 修改图书种类 REG_HANDLER(BookTypeRemove,33); /// 删除加图书种类 REG_HANDLER(BookAdd,41); /// 添加图书信息 REG_HANDLER(BookEdit,42); /// 修改图书信息 REG_HANDLER(BookRemove,43); /// 删除图书信息 } int main() { InitHandlers(); Request req; Session se(req); Response res; res.showOnDestroy(true); json j; default_jsonfail(); do { /// Must be post if(req.requestMethod!="POST") { jsonfail(err_method_not_supported); break; } postval(action); int key_real=ParseInt(action); int ret; if((ret=RequestDispatch(req,se,res,j,key_real))>=0) { j["RD"]="success"; } else { j["RD"]="fail"; } j["RDCode"]=ret; }while(0); se.writeToResponse(res); res.content.append(j.dump()); return 0; }