字数太多,我腰斩一下...
因为是API后台,
没有V只剩MC, 我们先看看Controller吧.也是简单的处理代码.
由require看出,我们需要在JsModule下再建一个文件夹Controll,再在里面新建GuestControll.js
var
Guest=require("../Model/Guest.js");
function
GuestControll(app,Mysql)
{
var
GuestModel=Object.create(Guest.GuestModel);
app.get('/guest/gettodayguest', GetTodayGuests);
//查询今天客人
app.get('/guest/getroombooking',
GetRoomBooking); //查询房间预订信息
app.get('/guest/getguestinfobyid',
GetGuestInfoById); //根据客人id获取信息
app.post('/guest/bookroom', NewGuest);
//预订客房
app.post('/guest/checkin', Checkin);
//入住客房
app.post('/guest/checkout', Checkout);
//退房
app.post('/guest/cancelbooking', CancelBooking);
//取消预订
app.post('/guest/overstay', OverStay);
//续住
function
GetTodayGuests(req, res, next) {
console.log("GetTodayGuests
query:"+JSON.stringify(req.query));
var
Today=req.params.today;
GuestModel.GetTodayGuests(Mysql,Today,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
function
NewGuest(req, res, next){
res.send('0');
}
function
Checkin(req, res, next) {
console.log("Checkin
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
GuestModel.Checkin(Mysql,RoomId,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
Checkout(req, res, next) {
console.log("Checkout
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
GuestModel.Checkout(Mysql,RoomId,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
CancelBooking(req, res, next) {
console.log("CancelBooking
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
var
GuestStr=req.params.guestStr;
var
GuestArr=eval_r(GuestStr);
console.log(GuestArr);
GuestModel.CancelBooking(Mysql,RoomId,GuestArr,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
OverStay(req, res, next) {
console.log("OverStay
param:"+JSON.stringify(req.params));
var
RoomId=req.params.roomid;
var
StayTime=req.params.staytime;
GuestModel.OverStay(Mysql,RoomId,StayTime,function(Rst){
res.charSet('utf-8');
var
SR={iRst:Rst};
res.send(SR);
});
}
function
GetRoomBooking(req, res, next)
{
console.log("GetRoomBooking
query:"+JSON.stringify(req.query));
var
RoomId=req.params.roomid;
var
NowTime=req.params.thistime;
GuestModel.GetRoomBooking(Mysql,RoomId,NowTime,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
function
GetGuestInfoById(req, res, next)
{
console.log("GetGuestInfoById
query:"+JSON.stringify(req.query));
var
UserId=req.params.uid;
GuestModel.GetGuestInfoById(Mysql,UserId,function(Data){
res.charSet('utf-8');
res.send(Data);
});
}
}
exports.GuestControll=GuestControll;
Controller都是中转控制啦, 业务逻辑都在Module层里,
第一句就告诉我们又要再建一个Module文件夹和Guest.js 的Module.
唯一让我觉得不够爽的地方是注册各个请求的接收参数名, 这个必须由后台私下和前端事先约定, 在nodejs上无法自描述...
var
JsOper=require("../Tool/JsOper.js");
var
DataTable=require("../Tool/CDataTableName.js");
var
GuestModel={
GetTodayGuests:function(MySql,aUTCDate,aResponse)
{
var QueryCmd=['SELECT
t1.ROOM_ID,t1.GASTNR,t1.GASTNAME,t2.SEX,t2.VIP,t1.ANREISE,t1.ABREISE,t1.CHECK_FLAG
'];
QueryCmd.push(' FROM
',DataTable.TableGuestCheckIn,' t1,
',DataTable.TableGuestInfo,' t2 ' );
QueryCmd.push(' WHERE
t1.GASTNR=t2.NUMMER AND ((t1.ANREISE