个推 push-single
import cloud from '@lafjs/cloud'
import * as axios from 'axios'
const config = cloud.shared.get('unipush.config')
const { baseUrl } = config
exports.main = async function (ctx: FunctionContext) {
const { data: { token } } = await cloud.invoke('get-token', {})
if (!token) return { error: '获取token失败', code: -1 }
const requestParam: any = {
"request_id": getRnd32(),
"settings": {
"ttl": -1,
"strategy": {
"default": 3
}
},
"audience": {
"cid": [
body.cid
]
},
"push_message": {
"notification": {
"title": body.title,
"body": body.body,
"click_type": body.click_type,
"channel_level": body.channel_level
}
},
"push_channel": {
"android": {
"ups": {
"notification": {
"title": body.title,
"body": body.body,
"click_type": body.click_type,
"channel_level": body.channel_level
}
}
}
}
}
if (body.click_type == 'url') {
requestParam.push_message.url = body.url
requestParam.push_channel.android.ups.url = body.url
}
if (body.click_type == 'intent') {
requestParam.push_message.intent = body.intent
requestParam.push_channel.android.ups.intent = body.intent
}
if (body.payload) {
requestParam.push_message.notification.payload = body.payload
requestParam.push_channel.android.ups.notification.payload = body.payload
}
let data;
await axios.default.post(`${baseUrl}/push/single/cid`, requestParam, {
headers: {
token: token,
},
})
.then(value => {
console.log(value, 'value')
data = {
code: 0, message: 'success'
}
})
.catch(err => {
console.log(err, 'error')
data = {
code: -1, error: statusCodeMap(err.response.data.code)
}
})
return data
}
function statusCodeMap(code) {
switch (code) {
case 10001:
return "token错误/失效";
case 30019:
return '系统繁忙,请稍后重试';
case 30009:
return '推送次数超过每日推送总数限制';
case 20001:
return '参数不合法';
case 10005:
return '每分钟调用频率超限';
case 30015:
return 'list推送 频率超过每分钟频率限制';
case 30002:
return '没有推送定时任务的权限';
case 22002:
return '任务无效或定时任务时间不合法';
case 10003:
return '每分钟鉴权频率超限';
default:
return 0
}
}
function getRnd32() {
let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let s = ''
for (let i = 0; i < 32; i++) {
s += str[Math.floor(Math.random() * 32)]
}
return s
}
批量推 push-batch
import cloud from '@lafjs/cloud'
import * as axios from 'axios'
const config = cloud.shared.get('unipush.config')
const { baseUrl } = config
exports.main = async function (ctx: FunctionContext) {
const { auth, body, query } = ctx
let taskId;
const { code, data: { token } } = await cloud.invoke('get-token', {})
const tokenResult = statusCodeMap(code)
if (tokenResult) return { error: tokenResult, code: -1 }
const createResult = await createMessageBody(body, token)
const codeResult = statusCodeMap(createResult.code)
if (codeResult) {
return { error: createResult.msg, code: -1 }
} else {
taskId = createResult.data.taskid
}
const pusResult = await multiPushToList(token, taskId, body.cid)
const pushCodeResult = statusCodeMap(pusResult)
if (pushCodeResult) {
return { error: pushCodeResult, code: -1 }
}
return { code: 0, message: 'success', taskId }
}
async function createMessageBody(body, token) {
const id = getRnd32()
const pushInfo: any = {
"request_id": id,
"group_name": id,
"settings": {
"ttl": -1,
"strategy": {
"default": 3,
},
"speed": 100,
},
"push_message": {
"notification": {
"title": body.title,
"body": body.body,
"click_type": body.click_type,
"channel_level": body.channel_level
}
},
"push_channel": {
"android": {
"ups": {
"notification": {
"title": body.title,
"body": body.body,
"click_type": body.click_type,
"channel_level": body.channel_level
}
}
}
}
}
if (body.payload) {
pushInfo.push_message.notification.payload = body.payload
pushInfo.push_channel.android.ups.notification.payload = body.payload
}
let r;
await axios.default.post(`${baseUrl}/push/list/message`, pushInfo, {
headers: {
token: token,
}
}).then(value => {
r = value.data
}).catch(err => {
r = err.response.data
})
return r
}
async function multiPushToList(token, taskid, cids) {
const multiPushParam = {
"audience": {
"cid": cids
},
"taskid": taskid,
"is_async": true
}
let code;
await axios.default.post(`${baseUrl}/push/list/cid`, multiPushParam, {
headers: {
token: token,
}
})
.then(value => {
code = value.data.code
})
.catch(err => {
code = err.response.data.code
})
return code
}
function statusCodeMap(code) {
switch (code) {
case 30019:
return '系统繁忙,请稍后重试';
case 30009:
return '推送次数超过每日推送总数限制';
case 20001:
return '参数不合法';
case 10005:
return '每分钟调用频率超限';
case 30015:
return 'list推送 频率超过每分钟频率限制';
case 30002:
return '没有推送定时任务的权限';
case 22002:
return '任务无效或定时任务时间不合法';
case 10003:
return '每分钟鉴权频率超限';
default:
return 0
}
}
function getRnd32() {
let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let s = ''
for (let i = 0; i < 32; i++) {
s += str[Math.floor(Math.random() * 32)]
}
return s
}