HttpUtils.ets 2.58 KB
import { SpConstants } from 'wdConstant/Index';
import { DeviceUtil, SPHelper, StringUtils } from 'wdKit/Index';
import { HttpParams } from '../http/HttpCommonParams';
import { HttpRequest } from '../http/HttpRequest';

const TAG: string = '[HttpUtils]'

/**
 * http相关工具类,对外暴露
 */
export class HttpUtils {
  private static userId = ''
  private static userType = ''
  private static token = ''

  /**
   * 添加公共参数,如登录后,添加登录信息
   */
  static addCommonHeader() {
    HttpRequest.addGlobalHeaderProvider(() => {
      let headers: Record<string, string> = {};
      return headers;
    })
  }

  /**
   * 添加公共参数,如登出后,移除登录信息
   */
  static removeCommonHeader() {

  }

  static getRefreshToken() {
    let refreshToken = SPHelper.default.getSync(SpConstants.USER_REFRESH_TOKEN, "")
    if (StringUtils.isNotEmpty(refreshToken)) {
      return refreshToken as string;
    }
    return '';
  }

  /**
   * 设备id,uuid
   */
  public static getDeviceId(): string {
    return DeviceUtil.clientId();
  }

  /**
   * 定位,城市code
   */
  public static getCityCode(): string {
    // TODO
    // 城市编码
    return '340100';
  }

  /**
   * 定位,省份code
   */
  public static getProvinceCode(): string {
    // TODO
    return '340000';
  }

  /**
   * 定位,地区code
   */
  public static getDistrictCode(): string {
    // TODO
    return '340103';
  }

  public static getImei(): string {
    return DeviceUtil.clientId();
  }

  public static getUserId(): string {
    // TODO 对接登录
    if (StringUtils.isNotEmpty(HttpUtils.userId)) {
      return HttpUtils.userId
    }
    HttpUtils.userId = SPHelper.default.getSync(SpConstants.USER_ID, "") as string
    return HttpUtils.userId;
  }

  public static getUserType(): string {
    if (StringUtils.isNotEmpty(HttpUtils.userType)) {
      return HttpUtils.userType
    }
    HttpUtils.userType = SPHelper.default.getSync(SpConstants.USER_Type, "") as string
    return HttpUtils.userType;
  }

  static getXToken(): string {
    if (StringUtils.isNotEmpty(HttpUtils.token)) {
      return HttpUtils.token
    }
    HttpUtils.token = SPHelper.default.getSync(SpConstants.USER_JWT_TOKEN, "") as string
    if (StringUtils.isNotEmpty(HttpUtils.token)) {
      return HttpUtils.token
    }
    return ''
  }

  public static setUserId(userId: string) {
    // TODO 优化赋值
    HttpUtils.userId = userId;
  }

  public static setUserType(userType: string) {
    HttpUtils.userType = userType;
  }

  public static setUserToken(token: string) {
    HttpUtils.token = token;
  }
}