首次完整推送,

V:1.20240808.006
This commit is contained in:
fm453
2024-08-13 18:32:37 +08:00
parent 15be3e9373
commit c62d15b288
939 changed files with 111777 additions and 0 deletions

View File

@ -0,0 +1,5 @@
module.exports = {
refreshToken: require('./refresh-token'),
setPushCid: require('./set-push-cid'),
secureNetworkHandshakeByWeixin: require('./secure-network-handshake-by-weixin')
}

View File

@ -0,0 +1,24 @@
/**
* 刷新token
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#refresh-token
*/
module.exports = async function () {
const refreshTokenRes = await this.uniIdCommon.refreshToken({
token: this.getUniversalUniIdToken()
})
const {
errCode,
token,
tokenExpired
} = refreshTokenRes
if (errCode) {
throw refreshTokenRes
}
return {
errCode: 0,
newToken: {
token,
tokenExpired
}
}
}

View File

@ -0,0 +1,73 @@
const {
ERROR
} = require('../../common/error')
const {
initWeixin
} = require('../../lib/third-party/index')
const {
saveWeixinUserKey,
saveSecureNetworkCache
} = require('../../lib/utils/weixin')
const loginByWeixin = require('../login/login-by-weixin')
/**
* 微信安全网络握手
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
* @param {object} params
* @param {string} params.code 微信登录返回的code
* @param {boolean} params.callLoginByWeixin 是否同时调用一次微信登录
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
code: 'string',
callLoginByWeixin: {
type: 'boolean',
required: false
}
}
this.middleware.validate(params, schema)
let platform = this.clientPlatform
if (platform !== 'mp-weixin') {
throw new Error(`[secureNetworkHandshake] platform ${platform} is not supported`)
}
const {
code,
callLoginByWeixin = false
} = params
if (callLoginByWeixin) {
return loginByWeixin.call(this, {
code,
secureNetworkCache: true
})
}
const weixinApi = initWeixin.call(this)
let getWeixinAccountResult
try {
getWeixinAccountResult = await weixinApi.code2Session(code)
} catch (error) {
console.error(error)
throw {
errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const {
openid,
unionid,
sessionKey // 微信小程序用户sessionKey
} = getWeixinAccountResult
await saveSecureNetworkCache.call(this, {
code,
openid,
unionid,
sessionKey
})
await saveWeixinUserKey.call(this, {
openid,
sessionKey
})
return {
errCode: 0
}
}

View File

@ -0,0 +1,132 @@
const {
deviceCollection
} = require('../../common/constants')
const {
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.getUniversalClientInfo()
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.add(data)
}
/**
* 更新device表的push_clien_id
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#set-push-cid
* @param {object} params
* @param {string} params.pushClientId 客户端pushClientId
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
pushClientId: 'string'
}
this.middleware.validate(params, schema)
const {
deviceId,
appId,
osName
} = this.getUniversalClientInfo()
let platform = this.clientPlatform
if (platform === 'app') {
platform += osName
}
const {
uid,
exp
} = this.authInfo
const { pushClientId } = params
const tokenExpired = exp * 1000
const getDeviceRes = await deviceCollection.where({
device_id: deviceId
}).get()
// console.log(getDeviceRes)
if (getDeviceRes.data.length > 1) {
return {
errCode: ERROR.SYSTEM_ERROR
}
}
const deviceRecord = getDeviceRes.data[0]
await setOpendbDevice.call(this, {
pushClientId
})
if (!deviceRecord) {
await deviceCollection.add({
user_id: uid,
device_id: deviceId,
token_expired: tokenExpired,
push_clientid: pushClientId,
appid: appId
})
return {
errCode: 0
}
}
await deviceCollection.where({
device_id: deviceId
}).update({
user_id: uid,
token_expired: tokenExpired,
push_clientid: pushClientId,
appid: appId
})
return {
errCode: 0
}
}