verify-code.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const {
  2. dbCmd,
  3. verifyCollection
  4. } = require('../../common/constants')
  5. const {
  6. ERROR
  7. } = require('../../common/error')
  8. const {
  9. getVerifyCode
  10. } = require('../../common/utils')
  11. async function setVerifyCode ({
  12. mobile,
  13. email,
  14. code,
  15. expiresIn,
  16. scene
  17. } = {}) {
  18. const now = Date.now()
  19. const record = {
  20. mobile,
  21. email,
  22. scene,
  23. code: code || getVerifyCode(),
  24. state: 0,
  25. ip: this.getClientInfo().clientIP,
  26. created_date: now,
  27. expired_date: now + expiresIn * 1000
  28. }
  29. await verifyCollection.add(record)
  30. return {
  31. errCode: 0
  32. }
  33. }
  34. async function setEmailVerifyCode ({
  35. email,
  36. code,
  37. expiresIn,
  38. scene
  39. } = {}) {
  40. email = email && email.trim()
  41. if (!email) {
  42. throw {
  43. errCode: ERROR.INVALID_EMAIL
  44. }
  45. }
  46. email = email.toLowerCase()
  47. return setVerifyCode.call(this, {
  48. email,
  49. code,
  50. expiresIn,
  51. scene
  52. })
  53. }
  54. async function setMobileVerifyCode ({
  55. mobile,
  56. code,
  57. expiresIn,
  58. scene
  59. } = {}) {
  60. mobile = mobile && mobile.trim()
  61. if (!mobile) {
  62. throw {
  63. errCode: ERROR.INVALID_MOBILE
  64. }
  65. }
  66. return setVerifyCode.call(this, {
  67. mobile,
  68. code,
  69. expiresIn,
  70. scene
  71. })
  72. }
  73. async function verifyEmailCode ({
  74. email,
  75. code,
  76. scene
  77. } = {}) {
  78. email = email && email.trim()
  79. if (!email) {
  80. throw {
  81. errCode: ERROR.INVALID_EMAIL
  82. }
  83. }
  84. email = email.toLowerCase()
  85. const {
  86. data: codeRecord
  87. } = await verifyCollection.where({
  88. email,
  89. scene,
  90. code,
  91. state: 0,
  92. expired_date: dbCmd.gt(Date.now())
  93. }).limit(1).get()
  94. if (codeRecord.length === 0) {
  95. throw {
  96. errCode: ERROR.EMAIL_VERIFY_CODE_ERROR
  97. }
  98. }
  99. await verifyCollection.doc(codeRecord[0]._id).update({
  100. state: 1
  101. })
  102. return {
  103. errCode: 0
  104. }
  105. }
  106. async function verifyMobileCode ({
  107. mobile,
  108. code,
  109. scene
  110. } = {}) {
  111. mobile = mobile && mobile.trim()
  112. if (!mobile) {
  113. throw {
  114. errCode: ERROR.INVALID_MOBILE
  115. }
  116. }
  117. const {
  118. data: codeRecord
  119. } = await verifyCollection.where({
  120. mobile,
  121. scene,
  122. code,
  123. state: 0,
  124. expired_date: dbCmd.gt(Date.now())
  125. }).limit(1).get()
  126. if (codeRecord.length === 0) {
  127. throw {
  128. errCode: ERROR.MOBILE_VERIFY_CODE_ERROR
  129. }
  130. }
  131. await verifyCollection.doc(codeRecord[0]._id).update({
  132. state: 1
  133. })
  134. return {
  135. errCode: 0
  136. }
  137. }
  138. module.exports = {
  139. verifyEmailCode,
  140. verifyMobileCode,
  141. setEmailVerifyCode,
  142. setMobileVerifyCode
  143. }