123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 'use strict';
- const crypto = require('crypto');
- exports.main = async ({ secretId, secretKey, module, extraInfo }) => {
- if (!secretId || !secretKey || !module) {
- throw new Error('secretId,secretKey和module不能为空');
- }
- const db = uniCloud.database();
-
- try {
- await db.createCollection('tencentcloud_plugin_report');
- } catch (error) {
- if (/DATABASE_COLLECTION_ALREADY_EXIST/.test(error.message)) {
-
- } else {
-
- throw error;
- }
- }
- const collection = db.collection('tencentcloud_plugin_report');
-
- let {
- data: [record]
- } = await collection.limit(1).get();
-
- if (!record) {
- const { id } = await collection.add({});
- record = { _id: id };
- }
-
- if (record[module]) {
- return;
- }
-
- const userUin = await getUserUin(secretId, secretKey);
-
- await uniCloud.httpclient.request('https://appdata.qq.com/upload', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- data: {
- action: 'activate',
- plugin_type: module.toLowerCase(),
- data: {
- site_id: `uniapp_${record._id}`,
- site_app: 'uni-app',
- site_url: 'uni-app',
- uin: userUin,
- cust_sec_on: 1,
- others: JSON.stringify(extraInfo)
- }
- }
- });
-
- await collection.doc(record._id).update({
- [module]: JSON.stringify(extraInfo)
- });
- };
- async function getUserUin(secretId, secretKey) {
- const payloadHash = crypto.createHash('sha256').update('{}').digest('hex');
- const requestString = `POST\n/\n\ncontent-type:application/json\nhost:ms.tencentcloudapi.com\n\ncontent-type;host\n${payloadHash}`;
- const currentDate = new Date();
- const timestamp = `${Math.floor(currentDate.getTime() / 1000)}`;
- const dateString = currentDate.toISOString().substr(0, 10);
- const requestStringHash = crypto.createHash('sha256').update(requestString).digest('hex');
- const stringToSign = `TC3-HMAC-SHA256\n${timestamp}\n${dateString}/ms/tc3_request\n${requestStringHash}`;
- const secretDate = crypto.createHmac('sha256', `TC3${secretKey}`).update(dateString).digest();
- const secretService = crypto.createHmac('sha256', secretDate).update('ms').digest();
- const secretSigning = crypto.createHmac('sha256', secretService).update('tc3_request').digest();
- const signature = crypto.createHmac('sha256', secretSigning).update(stringToSign).digest('hex');
- const authorization = `TC3-HMAC-SHA256 Credential=${secretId}/${dateString}/ms/tc3_request, SignedHeaders=content-type;host, Signature=${signature}`;
- const { res } = await uniCloud.httpclient.request('https://ms.tencentcloudapi.com', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-TC-Action': 'DescribeUserBaseInfoInstance',
- 'X-TC-Version': '2018-04-08',
- 'X-TC-Timestamp': timestamp,
- Authorization: authorization
- },
- data: {},
- dataType: 'json'
- });
- const { status, statusMessage, data } = res;
- if (status !== 200) {
- throw new Error('获取UserUin失败');
- }
- if (data.Response.Error) {
- throw new Error(data.Response.Error.Message);
- }
- return data.Response.UserUin;
- }
|