提交 fbfc8066 编写于 作者: 雪洛's avatar 雪洛

feat: save device info into opendb-device

上级 8b15df03
...@@ -160,49 +160,39 @@ function validate (value = {}, schema = {}) { ...@@ -160,49 +160,39 @@ function validate (value = {}, schema = {}) {
} }
function checkClientInfo (clientInfo) { function checkClientInfo (clientInfo) {
const schema = { const stringNotRequired = {
uniPlatform: 'string',
appId: 'string',
deviceId: {
required: false,
type: 'string'
},
osName: {
required: false,
type: 'string'
},
locale: {
required: false,
type: 'string'
},
clientIP: {
required: false,
type: 'string'
},
appName: {
required: false,
type: 'string'
},
appVersion: {
required: false,
type: 'string'
},
appVersionCode: {
required: false,
type: 'string'
},
channel: {
required: false, required: false,
type: 'string' type: 'string'
}, }
userAgent: { const numberNotRequired = {
required: false,
type: 'string'
},
uniIdToken: {
required: false, required: false,
type: 'string' type: 'string'
} }
const schema = {
uniPlatform: 'string',
appId: 'string',
deviceId: stringNotRequired,
osName: stringNotRequired,
locale: stringNotRequired,
clientIP: stringNotRequired,
appName: stringNotRequired,
appVersion: stringNotRequired,
appVersionCode: stringNotRequired,
channel: stringNotRequired,
userAgent: stringNotRequired,
uniIdToken: stringNotRequired,
deviceBrand: stringNotRequired,
deviceModel: stringNotRequired,
osVersion: stringNotRequired,
osLanguage: stringNotRequired,
osTheme: stringNotRequired,
romName: stringNotRequired,
romVersion: stringNotRequired,
devicePixelRatio: numberNotRequired,
windowWidth: numberNotRequired,
windowHeight: numberNotRequired,
screenWidth: numberNotRequired,
screenHeight: numberNotRequired
} }
const validateRes = validate(clientInfo, schema) const validateRes = validate(clientInfo, schema)
if (validateRes) { if (validateRes) {
......
...@@ -87,7 +87,7 @@ module.exports = { ...@@ -87,7 +87,7 @@ module.exports = {
break break
case 'web': case 'web':
case 'h5': case 'h5':
clientPlatform = 'h5' clientPlatform = 'web'
break break
default: default:
break break
......
...@@ -5,6 +5,67 @@ const { ...@@ -5,6 +5,67 @@ const {
ERROR ERROR
} = require('../../common/error') } = require('../../common/error')
async function setOpendbDevice ({
pushClientId
} = {}) {
// 仅新增,如果存在进行更新操作
const {
appId,
deviceId,
deviceBrand,
deviceModel,
osName,
osVersion,
osLanguage,
osTheme,
devicePixelRatio,
windowWidth,
windowHeight,
screenWidth,
screenHeight,
romName,
romVersion
} = this.getClientInfo()
const platform = this.clientPlatform
const now = Date.now()
const db = uniCloud.database()
const opendbDeviceCollection = db.collection('opendb-device')
const getDeviceRes = await opendbDeviceCollection.where({
device_id: deviceId
}).get()
const data = {
appid: appId,
device_id: deviceId,
vendor: deviceBrand,
model: deviceModel,
uni_platform: platform,
os_name: osName,
os_version: osVersion,
os_language: osLanguage,
os_theme: osTheme,
pixel_ratio: devicePixelRatio,
window_width: windowWidth,
window_height: windowHeight,
screen_width: screenWidth,
screen_height: screenHeight,
rom_name: romName,
rom_version: romVersion,
last_update_date: now,
push_clientid: pushClientId
}
if (getDeviceRes.data.length > 0) {
await opendbDeviceCollection.where({
device_id: deviceId
}).update(data)
return
}
data.create_date = now
await opendbDeviceCollection.where({
device_id: deviceId
}).add(data)
}
/** /**
* 更新device表的push_clien_id * 更新device表的push_clien_id
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
...@@ -19,12 +80,11 @@ module.exports = async function (params = {}) { ...@@ -19,12 +80,11 @@ module.exports = async function (params = {}) {
this.middleware.validate(params, schema) this.middleware.validate(params, schema)
const { const {
deviceId, deviceId,
"appId":dcloud_appid, appId,
uniPlatform,
osName osName
} = this.getClientInfo() } = this.getClientInfo()
let platform = uniPlatform let platform = this.clientPlatform
if(platform == "app"){ if (platform === 'app') {
platform += osName platform += osName
} }
...@@ -44,14 +104,16 @@ module.exports = async function (params = {}) { ...@@ -44,14 +104,16 @@ module.exports = async function (params = {}) {
} }
} }
const deviceRecord = getDeviceRes.data[0] const deviceRecord = getDeviceRes.data[0]
await setOpendbDevice({
pushClientId
})
if (!deviceRecord) { if (!deviceRecord) {
await deviceCollection.add({ await deviceCollection.add({
user_id: uid, user_id: uid,
device_id: deviceId, device_id: deviceId,
token_expired: tokenExpired, token_expired: tokenExpired,
push_clientid: pushClientId, push_clientid: pushClientId,
dcloud_appid, dcloud_appid: appId
platform
}) })
return { return {
errCode: 0 errCode: 0
...@@ -68,8 +130,7 @@ module.exports = async function (params = {}) { ...@@ -68,8 +130,7 @@ module.exports = async function (params = {}) {
user_id: uid, user_id: uid,
token_expired: tokenExpired, token_expired: tokenExpired,
push_clientid: pushClientId, push_clientid: pushClientId,
dcloud_appid, dcloud_appid: appId
platform
}) })
return { return {
errCode: 0 errCode: 0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册