index.js 2.1 KB
Newer Older
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
1 2
const crypto = require('crypto');
const SymmetricEncryption = require('./SymmetricEncryption.class.js');
DCloud_JSON's avatar
DCloud_JSON 已提交
3 4 5
const db = uniCloud.database()
const dbCmd = db.command
const $ = dbCmd.aggregate
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
6
const md5 = str => crypto.createHash('md5').update(str).digest('hex')
DCloud_JSON's avatar
DCloud_JSON 已提交
7

8 9 10 11 12 13 14 15 16
function getConfig(key) {
  // 获取 uni-im 配置
  const createConfig = require("uni-config-center");
  const uniImConfig = createConfig({
    pluginId: 'uni-im', // 插件 id
  })
  return uniImConfig.config(key)
}

DCloud_JSON's avatar
DCloud_JSON 已提交
17 18 19 20 21 22 23 24 25 26 27 28
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())
}

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
29 30 31 32 33 34 35 36 37
function hideUsernameStr(username) {
  if (username == undefined) {
    return false
  }
  let length = username.length
  let n = parseInt(length / 2.5) * 2
  return username.substr(0, length - n) + '**' + username.substr(-1 * n / 2)
}

DCloud_JSON's avatar
DCloud_JSON 已提交
38 39 40 41 42 43 44 45 46
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) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
47 48 49
  if (mobile == undefined) {
    return ''
  }
DCloud_JSON's avatar
DCloud_JSON 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  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(''))
      }
    }
  }
}

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
75 76 77
const SEN = new SymmetricEncryption()
const encrypt = str => SEN.encrypt(str)
const decrypt = str => SEN.decrypt(str)
DCloud_JSON's avatar
DCloud_JSON 已提交
78 79 80 81 82
module.exports = {
  db,
  dbCmd,
  $,
  md5,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
83 84
  encrypt,
  decrypt,
DCloud_JSON's avatar
DCloud_JSON 已提交
85
  getConversationId,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
86
  hideUsernameStr,
DCloud_JSON's avatar
DCloud_JSON 已提交
87 88
  hideEmailStr,
  hideMobileStr,
89 90
  checkParam,
  getConfig
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
91
}