uni-id-log.js 857 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const db = uniCloud.database()
  2. module.exports = async function ({
  3. data = {},
  4. success = true,
  5. type = 'login'
  6. } = {}) {
  7. const now = Date.now()
  8. const uniIdLogCollection = db.collection('uni-id-log')
  9. const requiredDataKeyList = ['user_id', 'username', 'email', 'mobile']
  10. const dataCopy = {}
  11. for (let i = 0; i < requiredDataKeyList.length; i++) {
  12. const key = requiredDataKeyList[i]
  13. if (key in data && typeof data[key] === 'string') {
  14. dataCopy[key] = data[key]
  15. }
  16. }
  17. const {
  18. appId,
  19. clientIP,
  20. deviceId,
  21. userAgent
  22. } = this.getClientInfo()
  23. const logData = {
  24. appid: appId,
  25. device_id: deviceId,
  26. ip: clientIP,
  27. type,
  28. ua: userAgent,
  29. create_date: now,
  30. ...dataCopy
  31. }
  32. if (success) {
  33. logData.state = 1
  34. } else {
  35. logData.state = 0
  36. }
  37. return uniIdLogCollection.add(logData)
  38. }