user_auth_repository.dart 2.2 KB
import 'dart:io';

import 'package:appframe/config/locator.dart';
import 'package:appframe/services/api_service.dart';
import 'package:dio/dio.dart';

class UserAuthRepository {
  late final ApiService _appService;

  UserAuthRepository() {
    _appService = getIt<ApiService>(instanceName: 'appApiService');
  }

  ///
  /// {
  ///   "code": 0,
  ///   "error": "操作成功"
  /// }
  ///
  Future<dynamic> updateUser(String userid, String name, String nickName, String avatar) async {
    try {
      Response resp = await _appService.post(
        '/api/v1/comm/user/update',
        {
          "userid": userid,
          "name": name,
          "nickName": nickName,
          "avatar": avatar,
        },
      );
      return resp.statusCode == HttpStatus.ok ? resp.data : null;
    } on DioException {
      return null;
    }
  }

  Future<dynamic> appleLogin(String userid, String authorizationCode, String identityToken) async {
    try {
      Response resp = await _appService.post(
        '/api/v1/comm/user/applelogin',
        {
          "user": userid,
          "authorizationCode": authorizationCode,
          "identityToken": identityToken,
        },
      );
      return resp.statusCode == HttpStatus.ok ? resp.data : null;
    } on DioException {
      return null;
    }
  }

  ///
  /// {
  ///   "code":  1,
  ///   "data":"bxe userid" // 存在会返回
  ///   "error": "",
  /// }
  Future<dynamic> exchangeId(String userid) async {
    try {
      Response resp = await _appService.post(
        '/api/v1/comm/user/exchangeid',
        {
          "userId": userid,
          "type": "apple",
        },
      );
      return resp.statusCode == HttpStatus.ok ? resp.data : null;
    } on DioException {
      return null;
    }
  }

  ///
  /// {
  ///   "error": "",
  ///   "code":  0,
  /// }
  Future<dynamic> newBinding(String userid, String bxeUserId, String type) async {
    try {
      Response resp = await _appService.post(
        '/api/v1/comm/user/newbinding',
        {
          "userId": userid,
          "bxeUserId": bxeUserId,
          "type": type,
        },
      );
      return resp.statusCode == HttpStatus.ok ? resp.data : null;
    } on DioException {
      return null;
    }
  }
}