本文档详细介绍了设备风险SDK(微信小程序版)的接入流程,帮助开发者在需要保护的页面中快速集成小程序SDK,实现设备风险识别功能。
背景信息
微信小程序SDK是专为小程序环境设计的设备风险识别工具,通过嵌入SDK,开发者可以快速集成设备指纹能力,实时获取设备信息并联动风险识别服务。该SDK支持多种场景,如注册、登录、营销活动等,有效防范黑产攻击,提升业务安全性。
SDK集成指南
下载并引入小程序 SDK
下载微信小程序SDK,建议在小程序的首屏加载index页面引入SDK,并将AliyunFP对象全局挂载,方便其他页面调用。
import * as AliyunFP from './feilin.weixin.xxxx.js'; const app = getApp(); app.AliyunFP = AliyunFP;
添加配置信息
在调用小程序SDK 接口前,需在对应的 axml 中加入feilin-view节点:
<view> <view> 业务的 UI 布局 </view> <view id = "feilin-view" style="position:fixed;top:99999rpx;"> <canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas> <canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas> </view> </view>
初始化SDK
引入小程序 SDK之后,在页面加载onReady时初始化SDK。
Page({ onReady() { AliyunFP.init( { appKey: '这里填写风险识别控制台申请的AppKey', appName: '这里填小程序应用名称,客户自定义', openId: '微信小程序用户ID,非必填,建议传入', endpoints: [ 'https://cloudauth-device.aliyuncs.com', 'https://cn-shanghai.device.saf.aliyuncs.com' ], }, (initStatus, deviceToken) => { console.log("initStatus:" + initStatus + " deviceToken:" + deviceToken); } ); } });
重要如果不指定服务地址,则endpoints会读取默认配置。请务必将endpoints的域名添加至小程序后台白名单,以确保相关功能正常运行。
endpoints默认服务地址:
中国内地(默认)服务地址:https://cloudauth-device.aliyuncs.com,https://cn-shanghai.device.saf.aliyuncs.com
国际/中国香港服务地址:https://cloudauth-device.ap-southeast-1.aliyuncs.com,https://ap-southeast-1.device.saf.aliyuncs.com
获取deviceToken
获取deviceToken,建议用 AliyunFP.init 接口回调返回的值,或者在发起业务行为(如注册、登录、订购等)时主动调用 AliyunFP.getToken 获取,AliyunFP.getToken 和 AliyunFP.init 之间至少预留1s左右的时间差。
AliyunFP.getToken()
接入示例
页面结构 (axml)
<view>
<view class="page-description">
{{ message }}
</view>
<button type="primary" onTap="onLogin">
登录
</button>
<view id="feilin-view" style="position: fixed; top: 99999rpx;">
<canvas id="feilin-canvas" type="2d" style="width: 150px; height: 150px;"></canvas>
<canvas id="feilin-webgl" type="webgl" style="width: 150px; height: 150px;"></canvas>
</view>
</view>
逻辑实现 (js)
import * as AliyunFP from './feilin.weixin.xxxx.js';
const app = getApp();
app.AliyunFP = AliyunFP;
Page({
onReady() {
// 页面加载完成
console.info("onReady");
AliyunFP.init({
appKey: '这里填写风险识别控制台申请的AppKey',
appName: '这里填小程序应用名称,客户自定义',
openId: '微信小程序用户ID,非必填,建议传入',
endpoints: ['https://cloudauth-device.aliyuncs.com', 'https://cn-shanghai.device.saf.aliyuncs.com'],
}, (initStatus, deviceToken) => {
console.log("initStatus:" + initStatus + " deviceToken:" + deviceToken);
});
},
onLogin() {
// 某行为点击,这里举例为登录。
console.info("onLogin!");
const deviceToken = AliyunFP.getToken();
// 将deviceToken传入客户的服务后台
const data = {
"deviceToken": deviceToken,
"其他业务参数": ""
};
// 发送后台请求...后台通过deviceToken查询风险信息
}
});
调用风险识别API接口
根据风险识别服务的事件参数文档说明,将deviceToken与其他必要参数组合,调用风险识别API接口进行设备风险检测。详情请参见: