123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- const db_user = require('../database/d_user')
- const d_air = require('../database/d_air')
- const c_flight = require('./c_flight');
- const handle = require('../until/handle')
- const field = require('../maps/field')
- const codeMap = require('../maps/rcodeMap')
- const {userType} = require("../maps/field");
- const {getUnixTimeStamp} = require("../until/time");
- const {reloadOrder,payOrder} = require("./TimeUpdate");
- async function checkAccount(account,userType= field.userType){
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err){throw err}
-
- if (result.length >= 1 ) {throw {rcode:codeMap.notFound,msg:'该账户已经被注册'}}
- return true;
- }
- async function login(userType = field.userType,account,passwd){
- let [err,result] = await handle(db_user.login(userType,account,passwd));
- if(err){throw err}
-
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'账号或者密码错误'}}
-
- if (result[0].state == field.userFreezeState ){throw {rcode:codeMap.permissionDenied,msg:'账号被冻结'}}
- return result[0].account;
- }
- async function changePasswd(type,account,oldPasswd,newPasswd){
-
- let [err,result] = await handle(db_user.login(type,account,oldPasswd));
- if(err)throw err;
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
- let id = result[0].id;
- [err,result] = await handle(db_user.changePasswd(id,newPasswd));
- if(err)throw err;
- return;
- }
- async function changePhone(type,account,passwd,newPhone){
-
- let [err,result] = await handle(db_user.login(type,account,passwd));
- if(err)throw err;
- if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
-
- let id = result[0].id;
- [err,result] = await handle(db_user.findPhoneUser(type,newPhone));
- if(err)throw err;
- if(result[0].total){throw {rcode:codeMap.dataRepeat,msg:'手机号已经使用'}}
-
- [err,result] = await handle(db_user.changePhone(id,newPhone));
- if(err)throw err;
- return result;
- }
- async function register(type,account,passwd,nickName){
-
- let [err,result] = await handle(db_user.findAccountUser(type,account));
- if(err)throw err;
- if(result.length&&result[0].total){throw {rcode:codeMap.dataRepeat,msg:'账号重复'}}
-
- [err,result] = await handle(db_user.register(type,nickName,account,passwd));
- if(err)throw err;
-
- return result;
- }
- async function info(type,account){
-
- let [err,result] = await handle(db_user.info(type,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
-
- return result[0];
- }
- async function cars(account){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.cars(result[0].id));
- if(err)throw err;
-
- return result;
- }
- async function removeCar(carId){
- let [err,result] = await handle(db_user.removeCar(carId));
- if(err)throw err;
- return result;
- }
- async function removeTravel(travelId){
- let [err,result] = await handle(db_user.removeTravel(travelId));
- if(err)throw err;
- return result;
- }
- async function addTravel(name,card,phone,account,isDefault = field.travelState_isDefault){
- let userId;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,result] = await handle(db_user.addTravel(userId,name,card,phone,isDefault));
- if(err)throw err;
- return result;
- }
- async function updateTravel(account,travelId,params){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
-
- [err,result] = await handle(db_user.findUserTravel(result[0].id,travelId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法匹配用户的乘车人'}
- }
- [err,result] = await handle(db_user.changeTravel(travelId,{
- name:params.name,
- card:params.card,
- phone:params.phone,
- default:params.default
- }));
- if(err)throw err;
-
- return result;
- }
- async function travels(account){
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.travels(result[0].id));
- if(err)throw err;
-
- return result.map(val=>{
- let card = val.card.replace(val.card.substr(4,10),'**********')
- return {
- ...val,
- card:card
- }
- });
- }
- async function travelInfo(account,passwd,travelId){
-
- let [err,result] = await handle(db_user.login(userType,account,passwd));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.travelInfo(travelId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到乘车人'}
- }
- return result[0];
- }
- async function addCar(flightId,account){
- let userId;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,result] = await handle(d_air.flightInfo(flightId));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到航班,航班号错误'}
- }
- console.log(result[0]);
- if(result[0].flightState !== field.flightState_sail+''){
- throw {rcode:codeMap.customError,msg:'航班非销售状态'}
- }
- [err,result] = await handle(db_user.findCar(userId,flightId));
- if(err)throw err;
- if(result.length >= 1){
- throw {rcode:codeMap.customError,msg:'该航班已经在购物车中'}
- }
-
- [err,result] = await handle(db_user.addCar(flightId,userId));
- if(err)throw err;
- return result;
- }
- async function addOrder(account,flightId,travelIds){
- let flight;
- if(travelIds.length < 1){throw {rcode:codeMap.notParam,msg:'缺少乘车人'}}
-
- for(let i = 0;i<travelIds.length;i++){
- [err,result] = await handle(db_user.travelInfo(travelIds[0]));
- if(err)throw err;
- if(result.length<1){throw {rcode:codeMap.customError,msg:'不在数据表的乘机人'}}
- }
- let userId,unixTime = getUnixTimeStamp();
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
-
- [err,flight] = await handle(c_flight.flightInfo(flightId));
- if(err)throw err;
- if((parseInt(flight.pay) + travelIds.length) > parseInt(flight.totalVotes)){
- throw {rcode:codeMap.customError,msg:'航班机票不足'}
- }
-
- [err,result] = await handle(db_user.addOrder(userId,flightId,travelIds,unixTime));
- if(err)throw err;
- await reloadOrder();
- return result;
- }
- async function payOrder(account,orderId){
- let userId,order,flight,travels;
- order.travelIds = undefined;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到账户'}}
- userId = result[0].id;
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
- if(order.payState!=field.payState_create){throw {rcode:codeMap.notFound,msg:'无法支付非未支付订单'}}
- [err,flight] = await handle(c_flight.flightInfo(order.flightId));
- if(err)throw err;
- if(flight.flightState!=field.flightState_sail){throw {rcode:codeMap.notFound,msg:'航班已经开始检票或者起飞,无法支付'}}
-
- travels=order.travelIds.split(',');
- for(let i = 0;i<travels.length;i++){
-
- [err,result] = await handle(db_user.addTick(order.id,travels[i]));
- if(err){
-
- [err,result] = await handle(db_user.clearTick(order.id));
- }
- }
- await payOrder(orderId);
- return true;
- }
- async function chooseSit(tickId,row,col){
- let tick;
-
- let [err,result] = await handle(db_user.tickInfo(tickId))
- if(err)throw err;
- if(result.length < 1){throw {rcode:codeMap.notFound,msg:'无法找到机票'}}
- }
- async function orderInfo(account,orderId){
- let userId,order,travels;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- userId = result[0].id;
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
-
- travels=order.travelIds.split(',');
- order.travels = travels.map(async travelId=>{
- [err,result] = await handle(db_user.travelInfo(travelId));
- if(err) console.log('获取乘车人信息失败'); throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到乘车人'} }
-
- [err,result] = await handle(db_user.orderTick(order.id,travelId));
- return {
- id:result[0].id,
- name:result[0].name,
- }
- });
- return order;
- }
- async function changeOrderTravel(account,orderId,travelIds){
- let userId,order,travels;
-
- let [err,result] = await handle(db_user.findAccountUser(userType,account));
- if(err)throw err;
- if(result.length < 1){
- throw {rcode:codeMap.notFound,msg:'无法找到账户'}
- }
- [err,result] = await handle(db_user.userOrderInfo(userId,orderId));
- if(err)throw err;
- if(result.length < 1){ throw {rcode:codeMap.notFound,msg:'无法找到相关订单'} }
- order = result[0];
-
- if(order.payState == field.payState_create || order.payState == field.payState_pay){
- }else{
- throw {rcode:codeMap.notFound,msg:'当前订单不允许修改乘车人'}
- }
- }
- module.exports = {
- register,
- changePhone,
- changePasswd,
- checkAccount,
- login,
- info,
- removeCar,
- addCar,
- cars,
- removeTravel,
- addTravel,
- travels,
- travelInfo,
- updateTravel,
- addOrder
- }
|