init method

Future<VisitorJsonResult> init(
  1. String orgUid,
  2. String uid,
  3. String nickname,
  4. String avatar,
)

Implementation

Future<VisitorJsonResult> init(
    String orgUid, String uid, String nickname, String avatar) async {
  //
  SpUtil.putString(BytedeskConstants.VISITOR_ORGUID, orgUid);
  String deviceUid = SpUtil.getString(BytedeskConstants.VISITOR_DEVICEUID)!;
  if (deviceUid == '') {
    deviceUid = BytedeskUuid.uuid();
    SpUtil.putString(BytedeskConstants.VISITOR_DEVICEUID, deviceUid);
  }
  //
  final initUrl = BytedeskUtils.getHostUri('/visitor/api/v1/init', {
    'orgUid': orgUid,
    'uid': uid,
    'nickname': nickname,
    'avatar': avatar,
    'client': client
  });
  final initResponse =
      await httpClient.get(initUrl, headers: getHeadersForVisitor());
  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson =
      json.decode(utf8decoder.convert(initResponse.bodyBytes));
  debugPrint("$initUrl, init:$responseJson");
  //
  if (responseJson['code'] == 200) {
    return VisitorJsonResult.fromJson(responseJson);
  } else {
    throw Exception('初始化失败');
  }
}