account.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const db_user = require('../database/d_user')
  2. const handle = require('../until/handle')
  3. const field = require('../maps/field')
  4. const codeMap = require('../maps/rcodeMap')
  5. const {userType} = require("../maps/field");
  6. // 处理账号的注册登录,
  7. async function checkAccount(account,userType= field.userType){
  8. let [err,result] = await handle(db_user.findAccountUser(userType,account));
  9. if(err){throw err}
  10. // 查看是否有结果
  11. if (result.length >= 1 ) {throw {rcode:codeMap.notFound,msg:'该账户已经被注册'}}
  12. return true;
  13. }
  14. /**
  15. * 用户登录,返回用户名
  16. * @param userType
  17. * @param account
  18. * @param passwd
  19. * @returns {Promise<*>}
  20. */
  21. async function login(userType = field.userType,account,passwd){
  22. let [err,result] = await handle(db_user.login(userType,account,passwd));
  23. if(err){throw err}
  24. // 查看是否有结果,没有结果自动告知账户或者密码错误
  25. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'账号或者密码错误'}}
  26. // 账号被冻结
  27. if (result[0].state == field.userFreezeState ){throw {rcode:codeMap.permissionDenied,msg:'账号被冻结'}}
  28. return result[0].account;
  29. }
  30. /**
  31. * 修改账户密码
  32. * @param type
  33. * @param account
  34. * @param oldPasswd
  35. * @param newPasswd
  36. * @returns {Promise<void>}
  37. */
  38. async function changePasswd(type,account,oldPasswd,newPasswd){
  39. // 根据账号和原密码查找id
  40. let [err,result] = await handle(db_user.login(type,account,oldPasswd));
  41. if(err)throw err;
  42. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
  43. let id = result[0].id;
  44. [err,result] = await handle(db_user.changePasswd(id,newPasswd));
  45. if(err)throw err;
  46. return;
  47. }
  48. /**
  49. * 修改指定账户的手机号
  50. * @param type
  51. * @param account
  52. * @param passwd
  53. * @param newPhone
  54. * @returns {Promise<void>}
  55. */
  56. async function changePhone(type,account,passwd,newPhone){
  57. // 根据账号和原密码查找id
  58. let [err,result] = await handle(db_user.login(type,account,passwd));
  59. if(err)throw err;
  60. if (result.length < 1 ) {throw {rcode:codeMap.notFound,msg:'密码错误'}}
  61. // 查看是否有对应的手机号
  62. let id = result[0].id;
  63. [err,result] = await handle(db_user.findPhoneUser(type,newPhone));
  64. if(err)throw err;
  65. if(result[0].total){throw {rcode:codeMap.dataRepeat,msg:'手机号已经使用'}}
  66. // 修改手机号
  67. [err,result] = await handle(db_user.changePhone(id,newPhone));
  68. if(err)throw err;
  69. return result;
  70. }
  71. /**
  72. * 用户注册
  73. * @param type
  74. * @param account
  75. * @param passwd
  76. * @param nickName
  77. * @returns {Promise<void>}
  78. */
  79. async function register(type,account,passwd,nickName){
  80. // 根据账号和原密码查找id
  81. let [err,result] = await handle(db_user.findAccountUser(type,account));
  82. if(err)throw err;
  83. if(result.length&&result[0].total){throw {rcode:codeMap.dataRepeat,msg:'账号重复'}}
  84. // 新增用户
  85. [err,result] = await handle(db_user.register(type,nickName,account,passwd));
  86. if(err)throw err;
  87. // 注册成功
  88. return result;
  89. }
  90. /**
  91. * 加载用户信息
  92. * @param type
  93. * @param account
  94. * @returns {Promise<void>}
  95. */
  96. async function info(type,account){
  97. // 根据账号和原密码查找id
  98. let [err,result] = await handle(db_user.info(type,account));
  99. if(err)throw err;
  100. if(result.length < 1){
  101. throw {rcode:codeMap.notFound,msg:'无法找到账户'}
  102. }
  103. // 注册成功
  104. return result[0];
  105. }
  106. module.exports = {
  107. register,
  108. changePhone,
  109. changePasswd,
  110. checkAccount,
  111. login,
  112. info
  113. }