index.js 1.3 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
const db = uniCloud.database()
const dbCmd = db.command
const $ = dbCmd.aggregate

const md5 = require('./md5');

function getConversationId({
  group_id,
  from_uid,
  to_uid,
}) {
  if (group_id) {
    return `group_${group_id}`
  }
  let arr = [from_uid, to_uid]
  return 'single' + '_' + md5(arr.sort().toString())
}

function hideEmailStr(email) {
  if (email == undefined) {
    return ''
  }
  const content = email.split("@")
  return content[0].substr(0, content[0].length - 2) + '**' + content[1]
}

function hideMobileStr(mobile) {
  return mobile.substr(0, 3) + '****' + mobile.substr(-1 * 4)
}

function checkParam(params, rule) {
  rule.required.forEach(item => {
    if (!params[item]) {
      throw new Error('错误,参数:' + item + "的值不能为空")
    }
  })
  for (let key in rule.type) {
    if (key in rule.type) {
      let val = params[key]
      let types = rule.type[key]

      function parseType(data) {
        return Object.prototype.toString.call(data).replace(/[\[\]]/g, '').split(' ')[1]
      }

      if (val && !types.includes(parseType(val))) {
        throw new Error('错误,参数:' + key + '的数据类型必须为:' + types.join(''))
      }
    }
  }
}

module.exports = {
  db,
  dbCmd,
  $,
  md5,
  getConversationId,
  hideEmailStr,
  hideMobileStr,
  checkParam
}