index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const { appId, appSecretKey } = require('./config.js');
  18. const { request } = require('./utils.js');
  19. const getExtraReportInfo = () => ({ captcha_appid: appId });
  20. /**
  21. * 获取配置在云函数的短信appid
  22. */
  23. function getAppId() {
  24. return appId;
  25. }
  26. /**
  27. * 核查验证码票据结果
  28. * @param {*} params
  29. * @param {integer} params.CaptchaType - 固定填值:9。可在控制台配置不同验证码类型。
  30. * @param {string} params.Ticket - 前端回调函数返回的用户验证票据
  31. * @param {string} params.UserIp - 用户操作来源的外网 IP
  32. * @param {string} params.Randstr - 前端回调函数返回的随机字符串
  33. * @param {integer} [params.BusinessId] - 业务 ID,网站或应用在多个业务中使用此服务,通过此 ID 区分统计数据
  34. * @param {integer} [params.SceneId] - 场景 ID,网站或应用的业务下有多个场景使用此服务,通过此 ID 区分统计数据
  35. * @param {string} [params.MacAddress] - mac 地址或设备唯一标识
  36. * @param {string} [params.Imei] - 手机设备号
  37. * @param {integer} [params.NeedGetCaptchaTime] - 是否返回前端获取验证码时间,取值1:需要返回
  38. */
  39. async function describeCaptchaResult(params) {
  40. // 配置校验
  41. if (!appId || !appSecretKey) {
  42. throw new Error('请在云函数CAPTCHA模块中配置appId和AppSecretKey');
  43. }
  44. const auth = uniCloud.auth();
  45. const userIp = auth.getClientIP();
  46. // 删除掉云函数带上来的额外参数
  47. delete params.uniIdToken;
  48. // 调用核查验证码票据接口
  49. const result = await request('DescribeCaptchaResult', {
  50. AppSecretKey: appSecretKey,
  51. CaptchaAppId: Number(appId),
  52. UserIp: userIp,
  53. ...params
  54. });
  55. return result;
  56. }
  57. module.exports = {
  58. getAppId,
  59. describeCaptchaResult,
  60. getExtraReportInfo
  61. };