Flutter插件flutter_yii_qiyu的介绍与使用方法详解
Qiyu

Flutter 目前官方没有支持,因此我们通过实现原生 SDK 来制作 Flutter 插件。
如何使用?
初始化
需要设置以下参数:
appKey
:七鱼后台给予的 APP 接入密钥。appName
:七鱼后台设置的接入 APP 名称。deviceIdentifier
:用户设备的 ID,主要用于客服推送时携带该数据。
String appKey = 'd433ac25ced4170d98a66b3eda15es12';
String appName = 'qiyu-plugin-example';
String deviceIdentifier = 'AAAAAABBBBBBBBCCCCDDDDD';
await Qiyu.initialize(
appKey: appKey, appName: appName, deviceIdentifier: deviceIdentifier);
依赖工具:获取设备信息 -> device_info
用户信息
为用户设置多种个人信息,每个信息为一个对象,最终以 JSON 字符串形式传递。
默认字段包括:姓名、手机号、邮箱。其他字段需要自行补充。
参数 | 描述 |
---|---|
index | 用于排序,默认值为 null,数据项按 index 升序排列 |
key | 数据项名称,用于区分不同数据 |
value | 显示的值,类型不限 |
label | 数据显示的名称 |
href | 超链接地址,如果设置此值,则该项将显示为超链接 |
hidden | 是否隐藏该条目,默认为 false |
class QiyuUserInfoData {
static const keyName = 'real_name';
static const keyPhone = 'mobile_phone';
static const keyEmail = 'email';
int? index;
String key;
String value;
String label;
String? href;
bool hidden;
QiyuUserInfoData({
this.index,
required this.key,
required this.value,
required this.label,
this.href,
this.hidden = false,
});
factory QiyuUserInfoData.fromJson(Map<String, dynamic> json) =>
_$QiyuUserInfoDataFromJson(json);
Map<String, dynamic> toJson() => _$QiyuUserInfoDataToJson(this);
}
登录并设置用户信息
登录前需确保用户已登出。
参数 | 描述 |
---|---|
userId | 用户唯一标识 |
userInfoDataList | 用户信息列表 |
await Qiyu.setUserInfo(userId: 'yii555', userInfoDataList: list);
注意:切换用户时需要先登出。
登出
final bool isLogoutSuccess = await Qiyu.logoutUser();
设置设备 ID
Qiyu.setDeviceIdentifier(deviceIdentifier: deviceIdentifier);
打开客服页面
Qiyu.showCustomerService();
示例代码
以下是完整的示例代码:
[@override](/user/override)
void initState() {
super.initState();
runQiyu();
}
Future<void> runQiyu() async {
// 获取设备 ID
String deviceIdentifier = '';
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
final IosDeviceInfo iosDevice = await deviceInfo.iosInfo;
deviceIdentifier = iosDevice.identifierForVendor!;
} else if (Platform.isAndroid) {
final AndroidDeviceInfo androidDevice = await deviceInfo.androidInfo;
deviceIdentifier = androidDevice.androidId!;
}
// 初始化
await Qiyu.initialize(
appKey: 'd433ac25ced4170d98a66b3eda15es12',
appName: 'qiyu-plugin-example',
deviceIdentifier: deviceIdentifier);
// 确保登出后设置用户信息
final bool isLogoutSuccess = await Qiyu.logoutUser();
if (isLogoutSuccess) {
// 设置用户信息
List<QiyuUserInfoData> list = [];
list.add(QiyuUserInfoData(key: QiyuUserInfoData.keyName, value: 'Test(555)', label: '名字'));
list.add(QiyuUserInfoData(key: QiyuUserInfoData.keyPhone, value: '0939552555', label: '电话'));
list.add(QiyuUserInfoData(key: QiyuUserInfoData.keyEmail, value: '555@gmail.com', label: '邮箱'));
list.add(QiyuUserInfoData(key: 'city', value: 'Taipei', label: '城市', index: 0));
await Qiyu.setUserInfo(userId: 'yii555', userInfoDataList: list);
}
}