
'use strict';

const crypto = require('crypto')

exports.main = async (event) => {
	const secret = 'zhhealth2022.'
	const hmac = crypto.createHmac('sha256', secret);
	
	let params = event.queryStringParameters
	const sign = params.sign
	delete params.sign
	
	const signStr = Object.keys(params).sort().map(key => {
		return `${key}=${params[key]}`
	}).join('&')

	hmac.update(signStr);
	
	if (sign !== hmac.digest('hex')) {
		throw new Error('非法访问')
	}

	const {
	    access_token,
	    openid
	} = params

	return await uniCloud.getPhoneNumber({
		appid: '__UNI__C29473D',
		provider: 'univerify',
		apiKey: '16fa20236696596869759d3a81541901',
		apiSecret: 'fca97287360c2e8f8259d8877a601887',
		access_token: access_token,
		openid: openid,
	})
};
