index.js 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
'use strict';
const uniADConfig = require('uni-config-center')({
	pluginId: 'uni-ad'
}).config()
const signIn = require('sign-in')
let ip = null
async function nextFn(data) {
	//写自己的业务逻辑
	switch (data.extra) {
		case "uniSignIn": //签到
			let {
				user_id
			} = data;
			await signIn({
				user_id,
				ip
			})
			break;
		default:
			break;
	}
	return {
		"isValid": true //如果不返回,广点通会2次调用本云函数
	}
}

const crypto = require('crypto');
const db = uniCloud.database();
exports.main = async (event, context) => {
	ip = context.CLIENTIP;
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		path,
		queryStringParameters
	} = event;
	const data = {
		adpid: event.adpid,
		platform: event.platform,
		provider: event.provider,
		trans_id: event.trans_id,
		sign: event.sign,
		user_id: event.user_id,
		extra: event.extra,
	}
	// 注意::必须验签请求来源
	const trans_id = event.trans_id;
	//去uni-config-center通过adpid获取secret
	const secret = uniADConfig[event.adpid]
	const sign2 = crypto.createHash('sha256').update(`${secret}:${trans_id}`).digest('hex');
	if (event.sign !== sign2) {
		console.log('验签失败');
		return null;
	}
	//自己的逻辑
	try {
		return await nextFn(data)
	} catch (e) {
		console.error(e)
		return {
			"isValid": false
		}
	}
};