unionIdOAuth method

Future<OAuth> unionIdOAuth(
  1. String? unionid
)

Implementation

Future<OAuth> unionIdOAuth(String? unionid) async {
  //
  final oauthUrl = BytedeskUtils.getHostUri('/wechat/token');
  // debugPrint("http api client: oauthUrl $oauthUrl");
  Map<String, String> headers = {
    "Authorization": "Basic Y2xpZW50OnNlY3JldA=="
  };
  Map<String, String> bodyMap = {
    "unionid": "$unionid",
    "grant_type": "wechat",
    "scope": "all"
  };
  //
  final oauthResponse =
      await httpClient.post(oauthUrl, headers: headers, body: bodyMap);
  // debugPrint('oauth result: $oauthResponse');
  // check the status code for the result
  int statusCode = oauthResponse.statusCode;
  // debugPrint("statusCode $statusCode");
  // 200: 授权成功,否则授权失败
  final oauthJson = jsonDecode(oauthResponse.body);
  debugPrint('statusCode:$statusCode, unionIdOAuth:$oauthJson');
  if (statusCode == 200) {
    SpUtil.putBool(BytedeskConstants.isLogin, true);
    SpUtil.putBool(BytedeskConstants.isAuthenticated, true);
    SpUtil.putString(BytedeskConstants.unionid, unionid!);
    SpUtil.putString(
        BytedeskConstants.accessToken, oauthJson['access_token']);
  } else if (statusCode == 400) {
    // token过期 {error: invalid_grant, error_description: Bad credentials}
    bytedeskEventBus.fire(InvalidTokenEventBus());
  }
  return OAuth.fromJson(statusCode, oauthJson);
}