uniIDUsers.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @class UniIDUsers uni-id 用户模型
  3. */
  4. const BaseMod = require('./base')
  5. module.exports = class UniIDUsers extends BaseMod {
  6. constructor() {
  7. super()
  8. this.tableName = 'uni-id-users'
  9. this.tablePrefix = false
  10. }
  11. /**
  12. * 获取用户数
  13. * @param {String} appid DCloud-appid
  14. * @param {String} platform 平台
  15. * @param {String} channel 渠道
  16. * @param {String} version 版本
  17. * @param {Object} registerTime 注册时间范围 例:{$gte:开始日期时间戳, $lte:结束日期时间戳}
  18. * @return {Number}
  19. */
  20. async getUserCount(appid, platform, channel, version, registerTime) {
  21. if(!appid || !platform) {
  22. return false
  23. }
  24. const condition = this.getCondition(appid, platform, channel, version, registerTime)
  25. let userCount = 0
  26. const userCountRes = await this.getCollection(this.tableName).where(condition).count()
  27. if(userCountRes && userCountRes.total > 0) {
  28. userCount = userCountRes.total
  29. }
  30. return userCount
  31. }
  32. /**
  33. * 获取用户编号列表
  34. * @param {String} appid DCloud-appid
  35. * @param {String} platform 平台
  36. * @param {String} channel 渠道
  37. * @param {String} version 版本
  38. * @param {Object} registerTime 注册时间范围 例:{$gte:开始日期时间戳, $lte:结束日期时间戳}
  39. * @return {Array}
  40. */
  41. async getUserIds(appid, platform, channel, version, registerTime) {
  42. if(!appid || !platform) {
  43. return false
  44. }
  45. const condition = this.getCondition(appid, platform, channel, version, registerTime)
  46. let uids = []
  47. const uidsRes = await this.selectAll(this.tableName, condition, {
  48. _id: 1
  49. })
  50. for (const u in uidsRes.data) {
  51. uids.push(uidsRes.data[u]._id)
  52. }
  53. return uids
  54. }
  55. /**
  56. * 获取查询条件
  57. * @param {String} appid DCloud-appid
  58. * @param {String} platform 平台
  59. * @param {String} channel 渠道
  60. * @param {String} version 版本
  61. * @param {Object} registerTime 注册时间范围 例:{$gte:开始日期时间戳, $lte:结束日期时间戳}
  62. */
  63. getCondition(appid, platform, channel, version, registerTime) {
  64. let condition = {
  65. 'register_env.appid': appid,//DCloud appid
  66. 'register_env.uni_platform': platform,//平台
  67. 'register_env.channel': channel ? channel : '1001', //渠道或场景值
  68. 'register_env.app_version' : version //应用版本区分
  69. }
  70. //原生应用平台
  71. if(['android', 'ios'].includes(platform)) {
  72. condition['register_env.uni_platform'] = 'app'//systemInfo中uniPlatform字段android和ios都用app表示,所以此处查询需要用osName区分一下
  73. condition['register_env.os_name'] = platform //系统
  74. }
  75. //兼容vue2
  76. if(channel === '1001') {
  77. condition['register_env.channel'] = {$in:['', '1001']}
  78. }
  79. //注册时间
  80. if(registerTime) {
  81. condition.register_date = registerTime
  82. }
  83. return condition
  84. }
  85. }