ini myapp and review rest api
This commit is contained in:
parent
58ee459459
commit
ff8c5714d9
23
app/src/main/ets/MyApp.ets
Normal file
23
app/src/main/ets/MyApp.ets
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { Context } from '@ohos.arkui.UIContext';
|
||||||
|
import window from '@ohos.window';
|
||||||
|
|
||||||
|
export class MyApp {
|
||||||
|
|
||||||
|
static appContext: Context;
|
||||||
|
static uiContext: UIContext;
|
||||||
|
static mainWindow: window.Window;
|
||||||
|
static uiAbilityContext: Context;
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static initAbility(uiAbilityContext: Context) {
|
||||||
|
MyApp.uiAbilityContext = uiAbilityContext;
|
||||||
|
MyApp.appContext = uiAbilityContext.getApplicationContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
static initWindow(window: window.Window) {
|
||||||
|
MyApp.mainWindow = window;
|
||||||
|
MyApp.uiContext = window.getUIContext();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
|
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
|
||||||
import { hilog } from '@kit.PerformanceAnalysisKit';
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
||||||
import { window } from '@kit.ArkUI';
|
import { window } from '@kit.ArkUI';
|
||||||
|
import { MyApp } from '../MyApp';
|
||||||
|
|
||||||
const TAG = '[AppAbility]'
|
const TAG = '[AppAbility]'
|
||||||
|
|
||||||
@ -21,12 +22,13 @@ export default class AppAbility extends UIAbility {
|
|||||||
onWindowStageCreate(windowStage: window.WindowStage): void {
|
onWindowStageCreate(windowStage: window.WindowStage): void {
|
||||||
// Main window is created, set main page for this ability
|
// Main window is created, set main page for this ability
|
||||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||||
|
MyApp.initAbility(this.context);
|
||||||
windowStage.loadContent('pages/Index', (err, data) => {
|
windowStage.loadContent('pages/Index', (err, data) => {
|
||||||
if (err.code) {
|
if (err.code) {
|
||||||
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
MyApp.initWindow(windowStage.getMainWindowSync());
|
||||||
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
|
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import { BaseLocalStorage, ScreenUtil } from '@devwiki/base';
|
|||||||
import { HomeItem, HomeItemGroup } from '../model/Home';
|
import { HomeItem, HomeItemGroup } from '../model/Home';
|
||||||
import { Context } from '@kit.AbilityKit';
|
import { Context } from '@kit.AbilityKit';
|
||||||
import { Calculator } from '@devwiki/hmcalculate';
|
import { Calculator } from '@devwiki/hmcalculate';
|
||||||
|
import { MyApp } from '../MyApp';
|
||||||
|
|
||||||
import('./animation/LoadingPage')
|
import('./animation/LoadingPage')
|
||||||
|
|
||||||
@ -68,8 +69,8 @@ struct Index {
|
|||||||
];
|
];
|
||||||
|
|
||||||
aboutToAppear(): void {
|
aboutToAppear(): void {
|
||||||
ScreenUtil.getInstance().initScreenSize();
|
ScreenUtil.getInstance().initScreenSize(MyApp.appContext);
|
||||||
BaseLocalStorage.getInstance().init(getContext(this));
|
BaseLocalStorage.getInstance().init(MyApp.appContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
onPageShow(): void {
|
onPageShow(): void {
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import { Log } from '@devwiki/base/Index';
|
import { Log } from '@devwiki/base/Index';
|
||||||
import axios, { AxiosError, AxiosResponse } from '@ohos/axios'
|
import axios, { AxiosError, AxiosResponse } from '@ohos/axios'
|
||||||
|
import http from '@ohos.net.http';
|
||||||
|
|
||||||
|
const TAG = '[RestAPIPage]'
|
||||||
|
|
||||||
@Entry({routeName: "RestAPIPage"})
|
@Entry({routeName: "RestAPIPage"})
|
||||||
@Component
|
@Component
|
||||||
@ -8,29 +11,37 @@ export struct RestAPIPage {
|
|||||||
@State viewModel: RestAPIViewModel = new RestAPIViewModel();
|
@State viewModel: RestAPIViewModel = new RestAPIViewModel();
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Row() {
|
Column() {
|
||||||
Flex({
|
Flex({
|
||||||
justifyContent: FlexAlign.SpaceBetween
|
justifyContent: FlexAlign.Start,
|
||||||
|
direction: FlexDirection.Column,
|
||||||
|
alignItems: ItemAlign.Center
|
||||||
}) {
|
}) {
|
||||||
Button("Get").onClick(() =>{
|
Button("GetByAxios").onClick(() =>{
|
||||||
this.viewModel.getServerVersion();
|
this.viewModel.getServerVersionByAxios();
|
||||||
})
|
}).margin({top: 20})
|
||||||
|
|
||||||
Text(this.viewModel.serverVersion).backgroundColor(Color.Blue).flexGrow(1);
|
Button("GetByHttp").onClick(() =>{
|
||||||
}
|
this.viewModel.getServerVersionByHttp();
|
||||||
}.justifyContent(FlexAlign.Start).alignItems(VerticalAlign.Center).width('80%').backgroundColor(Color.Green)
|
}).margin({top: 20})
|
||||||
|
|
||||||
|
Text(this.viewModel.serverVersion).margin({top: 20})
|
||||||
|
}.width('80%')
|
||||||
|
}.width('100%')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class RestAPIViewModel {
|
class RestAPIViewModel {
|
||||||
|
|
||||||
|
readonly viewUrl: string = 'https://music.devwiki.net/rest/ping.view?v=1.16.1&c=myapp&f=json';
|
||||||
serverVersion: string = '123'
|
serverVersion: string = '123'
|
||||||
|
|
||||||
async getServerVersion() {
|
async getServerVersionByAxios() {
|
||||||
|
this.serverVersion = ''
|
||||||
await axios({
|
await axios({
|
||||||
method: "post",
|
method: "post",
|
||||||
url: 'https://music.devwiki.net/rest/ping.view?v=1.16.1&c=myapp&f=json'
|
url: this.viewUrl
|
||||||
}).then((response: AxiosResponse) => {
|
}).then((response: AxiosResponse) => {
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
let version:string = response.data['subsonic-response']['serverVersion'];
|
let version:string = response.data['subsonic-response']['serverVersion'];
|
||||||
@ -41,4 +52,22 @@ class RestAPIViewModel {
|
|||||||
Log.e(error.message);
|
Log.e(error.message);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getServerVersionByHttp() {
|
||||||
|
this.serverVersion = ''
|
||||||
|
let httpRequest = http.createHttp();
|
||||||
|
let option:http.HttpRequestOptions = {
|
||||||
|
method: http.RequestMethod.GET
|
||||||
|
};
|
||||||
|
httpRequest.request(this.viewUrl, option, (error, resp) =>{
|
||||||
|
if (!error) {
|
||||||
|
Log.i(TAG, 'request http code:' + resp.responseCode)
|
||||||
|
let version:string = resp.result['subsonic-response']['serverVersion'];
|
||||||
|
this.serverVersion = version;
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, `request failed, code:${error.code}, message:${error.message}`)
|
||||||
|
}
|
||||||
|
httpRequest.destroy();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,8 @@
|
|||||||
import { TitleBar } from '@devwiki/common_ui';
|
import { TitleBar } from '@devwiki/common_ui';
|
||||||
import { i18n } from '@kit.LocalizationKit';
|
import { i18n } from '@kit.LocalizationKit';
|
||||||
|
import { Log } from '@devwiki/base';
|
||||||
|
|
||||||
|
const TAG = '[SetLanguagePage]'
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
@Component
|
@Component
|
||||||
@ -7,6 +10,13 @@ struct SetLanguagePage {
|
|||||||
private readonly languageGroup = "languageGroup"
|
private readonly languageGroup = "languageGroup"
|
||||||
@State selectedLanguage: string = 'zh-Hans';
|
@State selectedLanguage: string = 'zh-Hans';
|
||||||
|
|
||||||
|
aboutToAppear(): void {
|
||||||
|
let languages:string[] = i18n.System.getSystemLanguages();
|
||||||
|
languages.forEach((value, index) => {
|
||||||
|
Log.i(TAG, `${index.toString()}:${value}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Column() {
|
Column() {
|
||||||
TitleBar({
|
TitleBar({
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
import hilog from '@ohos.hilog';
|
import hilog from '@ohos.hilog';
|
||||||
|
|
||||||
let domain: number = 0xFF00;
|
let domain: number = 0xFF00;
|
||||||
let prefix: string = 'HM4Demo';
|
let prefix: string = 'HMDemo';
|
||||||
let format: string = `%{public}s`;
|
let format: string = `%{public}s, %{public}s`;
|
||||||
|
|
||||||
export class Log {
|
export class Log {
|
||||||
|
|
||||||
|
@ -25,11 +25,11 @@ export class ScreenUtil {
|
|||||||
return ScreenUtil.instance;
|
return ScreenUtil.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
initScreenSize(): void {
|
initScreenSize(context: Context): void {
|
||||||
this.portraitListener.on('change', (result)=> {
|
this.portraitListener.on('change', (result)=> {
|
||||||
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, result.matches)
|
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, result.matches)
|
||||||
})
|
})
|
||||||
window.getLastWindow(getContext(this))
|
window.getLastWindow(context)
|
||||||
.then((windowClass: window.Window) => {
|
.then((windowClass: window.Window) => {
|
||||||
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
|
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
|
||||||
let avoidArea = windowClass.getWindowAvoidArea(type);
|
let avoidArea = windowClass.getWindowAvoidArea(type);
|
||||||
|
Loading…
Reference in New Issue
Block a user