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

  if (success) {
    logData.state = 1
  } else {
    logData.state = 0
  }
  return uniIdLogCollection.add(logData)
}