account.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const db_user = require('../database/d_user')
  2. const d_air = require('../database/d_air')
  3. const handle = require('../until/handle')
  4. const field = require('../maps/field')
  5. const codeMap = require('../maps/rcodeMap')
  6. const {userType} = require("../maps/field");
  7. // 处理账号的注册登录,
  8. async function checkAccount(account,userType= field.userType){
  9. let [err,result] = await handle(db_user.findAccountUser(userType,account));
  10. if(err){throw err}
  11. // 查看是否有结果
  12. if (result.length >= 1 ) {throw {rcode:codeMap.notFound,msg:'该账户已经被注册'}}
  13. return true;
  14. }
  15. /**
  16. * 用户登录,返回用户名
  17. * @param userType
  18. * @param account
  19. * @param passwd
  20. * @returns {Promise<*>}
  21. */
  22. async function login(userType = field.userType,account,passwd){
  23. let [err,result] = await handle(db_user.login(userType,account,passwd));
  24. if(err){throw err}
  25. // 查看是否有结果,没有结果自动告知账户或者密码错误
  26. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'账号或者密码错误'}}
  27. // 账号被冻结
  28. if (result[0].state == field.userFreezeState ){throw {rcode:codeMap.permissionDenied,msg:'账号被冻结'}}
  29. return result[0].account;
  30. }
  31. /**
  32. * 修改账户密码
  33. * @param type
  34. * @param account
  35. * @param oldPasswd
  36. * @param newPasswd
  37. * @returns {Promise<void>}
  38. */
  39. async function changePasswd(type,account,oldPasswd,newPasswd){
  40. // 根据账号和原密码查找id
  41. let [err,result] = await handle(db_user.login(type,account,oldPasswd));
  42. if(err)throw err;
  43. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
  44. let id = result[0].id;
  45. [err,result] = await handle(db_user.changePasswd(id,newPasswd));
  46. if(err)throw err;
  47. return;
  48. }
  49. /**
  50. * 修改指定账户的手机号
  51. * @param type
  52. * @param account
  53. * @param passwd
  54. * @param newPhone
  55. * @returns {Promise<void>}
  56. */
  57. async function changePhone(type,account,passwd,newPhone){
  58. // 根据账号和原密码查找id
  59. let [err,result] = await handle(db_user.login(type,account,passwd));
  60. if(err)throw err;
  61. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
  62. // 查看是否有对应的手机号
  63. let id = result[0].id;
  64. [err,result] = await handle(db_user.findPhoneUser(type,newPhone));
  65. if(err)throw err;
  66. if(result[0].total){throw {rcode:codeMap.dataRepeat,msg:'手机号已经使用'}}
  67. // 修改手机号
  68. [err,result] = await handle(db_user.changePhone(id,newPhone));
  69. if(err)throw err;
  70. return result;
  71. }
  72. /**
  73. * 用户注册
  74. * @param type
  75. * @param account
  76. * @param passwd
  77. * @param nickName
  78. * @returns {Promise<void>}
  79. */
  80. async function register(type,account,passwd,nickName){
  81. // 根据账号和原密码查找id
  82. let [err,result] = await handle(db_user.findAccountUser(type,account));
  83. if(err)throw err;
  84. if(result.length&&result[0].total){throw {rcode:codeMap.dataRepeat,msg:'账号重复'}}
  85. // 新增用户
  86. [err,result] = await handle(db_user.register(type,nickName,account,passwd));
  87. if(err)throw err;
  88. // 注册成功
  89. return result;
  90. }
  91. /**
  92. * 加载用户信息
  93. * @param type
  94. * @param account
  95. * @returns {Promise<void>}
  96. */
  97. async function info(type,account){
  98. // 根据账号和原密码查找id
  99. let [err,result] = await handle(db_user.info(type,account));
  100. if(err)throw err;
  101. if(result.length < 1){
  102. throw {rcode:codeMap.notFound,msg:'无法找到账户'}
  103. }
  104. // 注册成功
  105. return result[0];
  106. }
  107. /**
  108. * 获取用户购物车
  109. * @param account 用户账号
  110. * @returns {Promise<*>}
  111. */
  112. async function cars(account){
  113. // 根据账号查找id
  114. let [err,result] = await handle(db_user.findAccountUser(userType,account));
  115. if(err)throw err;
  116. if(result.length < 1){
  117. throw {rcode:codeMap.notFound,msg:'无法找到账户'}
  118. }
  119. [err,result] = await handle(db_user.cars(result[0].id));
  120. if(err)throw err;
  121. // 注册成功
  122. return result;
  123. }
  124. async function removeCar(carId){
  125. let [err,result] = await handle(db_user.removeCar(carId));
  126. if(err)throw err;
  127. return result;
  128. }
  129. async function addCar(flightId,account){
  130. let userId;
  131. // 根据账号查找id
  132. let [err,result] = await handle(db_user.findAccountUser(userType,account));
  133. if(err)throw err;
  134. if(result.length < 1){
  135. throw {rcode:codeMap.notFound,msg:'无法找到账户'}
  136. }
  137. userId = result[0].id;
  138. // 查看航班是否已经结束
  139. [err,result] = await handle(d_air.flightInfo(flightId));
  140. if(err)throw err;
  141. if(result.length < 1){
  142. throw {rcode:codeMap.notFound,msg:'无法找到航班,航班号错误'}
  143. }
  144. console.log(result[0]);
  145. if(result[0].flightState !== field.flightState_sail+''){
  146. throw {rcode:codeMap.customError,msg:'航班非销售状态'}
  147. }
  148. // 开始添加购物车
  149. [err,result] = await handle(db_user.addCar(flightId,userId));
  150. if(err)throw err;
  151. return result;
  152. }
  153. module.exports = {
  154. register,
  155. changePhone,
  156. changePasswd,
  157. checkAccount,
  158. login,
  159. info,
  160. removeCar,
  161. addCar,
  162. cars
  163. }