device.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @class Device 设备模型
  3. */
  4. const BaseMod = require('./base')
  5. const Platform = require('./platform')
  6. const {
  7. DateTime
  8. } = require('../lib')
  9. module.exports = class Device extends BaseMod {
  10. constructor() {
  11. super()
  12. this.tableName = 'opendb-device'
  13. this.tablePrefix = false
  14. this.cacheKeyPre = 'uni-stat-device-'
  15. }
  16. /**
  17. * 通过设备编号获取设备信息
  18. * @param {Object} deviceId 设备编号
  19. */
  20. async getDeviceById(deviceId) {
  21. const cacheKey = this.cacheKeyPre + deviceId
  22. let deviceData = await this.getCache(cacheKey)
  23. if (!deviceData) {
  24. const deviceRes = await this.getCollection().where({
  25. device_id: deviceId
  26. }).get()
  27. deviceData = []
  28. if (deviceRes.data.length > 0) {
  29. deviceData = deviceRes.data[0]
  30. await this.setCache(cacheKey, deviceData)
  31. }
  32. }
  33. return deviceData
  34. }
  35. /**
  36. * 设置设备信息
  37. * @param {Object} params 上报参数
  38. */
  39. async setDevice(params) {
  40. // 设备信息
  41. if (!params.did) {
  42. return {
  43. code: 200,
  44. msg: 'Parameter "did" not found'
  45. }
  46. }
  47. const deviceData = await this.getDeviceById(params.did)
  48. //不存在则添加
  49. if(deviceData.length === 0) {
  50. return await this.addDevice(params)
  51. } else {
  52. return await this.updateDevice(params, deviceData)
  53. }
  54. }
  55. /**
  56. * 添加设备信息
  57. * @param {Object} params 上报参数
  58. */
  59. async addDevice(params) {
  60. const dateTime = new DateTime()
  61. const platform = new Platform()
  62. const fillParams = {
  63. device_id: params.did,
  64. appid: params.ak,
  65. vendor: params.brand ? params.brand : '',
  66. push_clientid: params.cid ? params.cid : '',
  67. imei: params.imei ? params.imei : '',
  68. oaid: params.oaid ? params.oaid : '',
  69. idfa: params.idfa ? params.idfa : '',
  70. imsi: params.imsi ? params.imsi : '',
  71. model: params.md ? params.md : '',
  72. uni_platform: params.up ? params.up : '',
  73. os_name: params.on ? params.on : platform.getOsName(params.p),
  74. os_version: params.sv ? params.sv : '',
  75. os_language: params.lang ? params.lang : '',
  76. os_theme: params.ot ? params.ot : '',
  77. pixel_ratio: params.pr ? params.pr : '',
  78. network_model: params.net ? params.net : '',
  79. window_width: params.ww ? params.ww : '',
  80. window_height: params.wh ? params.wh : '',
  81. screen_width: params.sw ? params.sw : '',
  82. screen_height: params.sh ? params.sh : '',
  83. rom_name: params.rn ? params.rn : '',
  84. rom_version: params.rv ? params.rv : '',
  85. location_ip: params.ip ? params.ip : '',
  86. location_latitude: params.lat ? parseFloat(params.lat) : 0,
  87. location_longitude: params.lng ? parseFloat(params.lng) : 0,
  88. location_country: params.cn ? params.cn : '',
  89. location_province: params.pn ? params.pn : '',
  90. location_city: params.ct ? params.ct : '',
  91. create_date: dateTime.getTime(),
  92. last_update_date: dateTime.getTime()
  93. }
  94. const res = await this.insert(this.tableName, fillParams)
  95. if (res && res.id) {
  96. return {
  97. code: 0,
  98. msg: 'success',
  99. }
  100. } else {
  101. return {
  102. code: 500,
  103. msg: 'Device data filled error'
  104. }
  105. }
  106. }
  107. async updateDevice(params, deviceData) {
  108. //最新的参数
  109. const dateTime = new DateTime()
  110. const platform = new Platform()
  111. console.log('device params', params)
  112. const newDeviceParams = {
  113. appid: params.ak,
  114. push_clientid: params.cid ? params.cid : '',
  115. imei: params.imei ? params.imei : '',
  116. oaid: params.oaid ? params.oaid : '',
  117. idfa: params.idfa ? params.idfa : '',
  118. imsi: params.imsi ? params.imsi : '',
  119. uni_platform: params.up ? params.up : '',
  120. os_name: params.on ? params.on : platform.getOsName(params.p),
  121. os_version: params.sv ? params.sv : '',
  122. os_language: params.lang ? params.lang : '',
  123. pixel_ratio: params.pr ? params.pr : '',
  124. network_model: params.net ? params.net : '',
  125. window_width: params.ww ? params.ww : '',
  126. window_height: params.wh ? params.wh : '',
  127. screen_width: params.sw ? params.sw : '',
  128. screen_height: params.sh ? params.sh : '',
  129. rom_name: params.rn ? params.rn : '',
  130. rom_version: params.rv ? params.rv : '',
  131. location_ip: params.ip ? params.ip : '',
  132. location_latitude: params.lat ? parseFloat(params.lat) : '',
  133. location_longitude: params.lng ? parseFloat(params.lng) : '',
  134. location_country: params.cn ? params.cn : '',
  135. location_province: params.pn ? params.pn : '',
  136. location_city: params.ct ? params.ct : '',
  137. }
  138. //检查是否有需要更新的数据
  139. const updateData = {}
  140. for(let key in newDeviceParams) {
  141. if(newDeviceParams[key] && newDeviceParams[key] !== deviceData[key]) {
  142. updateData[key] = newDeviceParams[key]
  143. }
  144. }
  145. console.log('Device need to update', updateData)
  146. if(Object.keys(updateData).length) {
  147. if(this.debug) {
  148. console.log('Device need to update', updateData)
  149. }
  150. //数据更新
  151. updateData.last_update_date = dateTime.getTime()
  152. await this.update(this.tableName, updateData, {device_id: params.did})
  153. } else {
  154. if(this.debug) {
  155. console.log('Device not need update', newDeviceParams)
  156. }
  157. }
  158. return {
  159. code: 0,
  160. msg: 'success'
  161. }
  162. }
  163. async bindPush(params) {
  164. if (!params.cid) {
  165. return {
  166. code: 200,
  167. msg: 'Parameter "cid" not found'
  168. }
  169. }
  170. return await this.setDevice(params)
  171. }
  172. }