日志工具类
import { hilog } from '@kit.PerformanceAnalysisKit'
const domain: number = 0x0000
const tag: string = 'mylog'
const format: string = '%{public}s,%{public}s'
export class Logger {
static debug(...args: string[]) {
hilog.debug(domain, tag, format, ...args)
}
static info(...args: string[]) {
hilog.info(domain, tag, format, ...args)
}
static warn(...args: string[]) {
hilog.warn(domain, tag, format, ...args)
}
static error(...args: string[]) {
hilog.error(domain, tag, format, ...args)
}
}
沉浸式模式类封装
import { window } from '@kit.ArkUI'
export class windowManager {
static async enableFullScreen() {
const win = await window.getLastWindow(getContext())
win.setWindowLayoutFullScreen(true)
}
static async disableFullScreen() {
const win = await window.getLastWindow(getContext())
win.setWindowLayoutFullScreen(false)
}
static async getAvoidAreaTop() {
const win = await window.getLastWindow(getContext())
const area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
const topHeight = px2vp(area.topRect.height)
AppStorage.setOrCreate('topHeight', topHeight)
return topHeight
}
static async getAvoidAreeBottom() {
const win = await window.getLastWindow(getContext())
const area = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
const bottomHeight = px2vp(area.bottomRect.height)
AppStorage.setOrCreate('bottomHeight', bottomHeight)
return bottomHeight
}
static async settingStatusBarContentColor(color: '#FFFFFF' | '#000000') {
const win = await window.getLastWindow(getContext())
win.setWindowSystemBarProperties({ statusBarContentColor: color })
}
}
封装request泛型方法
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from '@ohos/axios'
import { iLoginUserModel, iResponseModel } from '../models/datamodel'
import { promptAction, router } from '@kit.ArkUI'
export const req = axios.create({
baseURL: 'https://api-harmony-teach.itheima.net'
})
export class HdHttp {
static async post<T>(url: string, data?: object) {
return await HdHttp.request<T>('POST', url, data)
}
static async get<T>(url: string, params?: object) {
return await HdHttp.request<T>('GET', url, params)
}
private static async request<T>(method: string, url: string, paramsOrData?: object) {
try {
let reqConfig: AxiosRequestConfig = {
method: method,
url: url,
}
if (method == 'GET') {
reqConfig.params = paramsOrData
} else {
reqConfig.data = paramsOrData
}
let user = AppStorage.get<iLoginUserModel>('user')
if (user && user.token) {
reqConfig.headers = {
'Authorization': `Bearer ${user.token}`
}
}
let res: AxiosResponse<iResponseModel<T>> = await req.request(reqConfig)
if (res.data.code != 10000) {
promptAction.showToast({ message: res.data.message })
return Promise.reject(res.data.message)
}
return res.data
} catch (err) {
let errObj: AxiosError = err
if (errObj.response?.status == 401) {
promptAction.showToast({ message: '登录已失效,请重新登录' })
router.replaceUrl({ url: 'pages/LoginPage' })
} else {
promptAction.showToast({ message: '网络异常:' + errObj.message })
}
return Promise.reject(errObj.message)
}
}
}