smsOAuth method

Future<OAuth> smsOAuth(
  1. String? mobile,
  2. String? code
)

Implementation

Future<OAuth> smsOAuth(String? mobile, String? code) async {
  //
  final oauthUrl = BytedeskUtils.getHostUri('/mobile/token');
  Map<String, String> headers = {
    "Authorization": "Basic Y2xpZW50OnNlY3JldA=="
  };
  Map<String, String> bodyMap = {
    "mobile": "$mobile",
    "code": "$code",
    "grant_type": "mobile",
    "scope": "all"
  };
  //
  final oauthResponse =
      await httpClient.post(oauthUrl, headers: headers, body: bodyMap);
  // debugPrint('oauth result: $oauthResponse');
  int statusCode = oauthResponse.statusCode;
  // 200: 授权成功,否则授权失败
  final oauthJson = jsonDecode(oauthResponse.body);
  debugPrint('statusCode:$statusCode, smsOAuth:$oauthJson');
  if (statusCode == 200) {
    SpUtil.putBool(BytedeskConstants.isLogin, true);
    SpUtil.putBool(BytedeskConstants.isAuthenticated, true);
    SpUtil.putString(BytedeskConstants.mobile, mobile!);
    SpUtil.putString(
        BytedeskConstants.accessToken, oauthJson['access_token']);
    // 广播登录成功事件
    bytedeskEventBus.fire(SmsLoginSuccessEventBus(mobile));
  } else if (statusCode == 400) {
    // token过期 {error: invalid_grant, error_description: Bad credentials}
    bytedeskEventBus.fire(InvalidTokenEventBus());
  }

  return OAuth.fromJson(statusCode, oauthJson);
}