diff --git a/README.md b/README.md index 8095e38..1c73a4e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # 基础开发工具包 + ```shell ohpm install @szyx/sdk_base ``` @@ -6,6 +7,7 @@ ohpm install @szyx/sdk_base ## 1.[utils](./src/main/ets/utils) ### 1.1.[AppStorageHelper](./src/main/ets/utils/AppStorageHelper.ets) + > 缓存工具类,运行时存储,应用停止运行后清空 ```typescript @@ -13,72 +15,108 @@ import { AppStorageHelper } from '@szyx/sdk_base/Index' // 存储string数据 AppStorageHelper.save(StorageKeys.CLIENT_ID, d) - - // 获取存储的strign数据 + +// 获取存储的strign数据 let d = AppStorageHelper.get(StorageKeys.CLIENT_ID) // 删除指定存储 AppStorageHelper.delete(StorageKeys.CLIENT_ID) ``` ### 1.2.[PreferencesHelper](./src/main/ets/utils/PreferencesHelper.ets) + > 永久存储类,应用停止后也不会清空 > 需要验证,更新应用会不会被清理 > 可存储类型 `number | string | boolean | Array | Array | Array | Uint8Array` ```typescript import { PreferencesHelper } from '@szyx/sdk_base/Index' + // 存储数据 PreferencesHelper.put(StorageKeys.CLIENT_ID, value) - - // 获取存储的数据 + +// 获取存储的数据 PreferencesHelper.get(StorageKeys.CLIENT_ID).then(res => { console.log('>>>>>', res) }) - // 删除存储的数据 +// 删除存储的数据 PreferencesHelper.delete(StorageKeys.CLIENT_ID).then(() => { console.log('>>>>>') }) ``` ### 1.3.[ToolsHelper](./src/main/ets/utils/ToolsHelper.ets) + > 常用方法工具栏 > 基础方法 + #### 1.3.1.弹出Toast提示 ```typescript import { ToolsHelper } from '@szyx/sdk_base'; - + ToolsHelper.showMessage('Hello Word!') ``` +#### 1.3.2.打印日志 + +> 打印格式:`========>${顶层调用栈}::` + +```typescript +import { ToolsHelper } from '@szyx/sdk_base'; + +ToolsHelper.log('Hello Word!') +``` + +#### 1.3.3.获取调用栈第一个 + +```typescript +import { ToolsHelper } from '@szyx/sdk_base'; + +ToolsHelper.getStackKey() +``` + +#### 1.3.4.获取设备信息 + +```typescript +import { ToolsHelper } from '@szyx/sdk_base'; + +ToolsHelper.getDeviceInfo() +``` + ### 1.4.[ValidatorHelper](./src/main/ets/utils/ValidatorHelper.ets) + > 常用正则 #### 1.4.1.验证手机号 ```typescript import { ValidatorHelper } from '@szyx/sdk_base'; + ValidatorHelper.isPhone('13800000000') ``` ### 1.5.[AlgorithmHelper](./src/main/ets/utils/AlgorithmHelper.ets) + > 计算相关 #### 1.5.1.验证手机号 ```typescript import { AlgorithmHelper } from '@szyx/sdk_base'; + AlgorithmHelper.calculateDistance(x1, y1, x2, y2) ``` ### 1.6.[XWebHelper](./src/main/ets/utils/XWebHelper.ets) + > 打开webview页面 ```typescript import { XWebHelper } from '@szyx/sdk_base'; + // 必须先引入,否则无法跳转 const XWebview = import('../pages/XWebview'); - + XWebHelper.openWeb({ url: 'https://www.baidu.com', title: '百度一下', @@ -86,27 +124,38 @@ XWebHelper.openWeb({ ``` ## 2.[Dialog](./src/main/ets/dialog) + ### 2.1.弹出list选中弹窗 + ```typescript import { XDialogController } from '../dialog/XDialogController'; import { XDialogList } from '../dialog/XDialogList'; -@Component -struct MyView { +@ +Component +struct +MyView +{ // 控制器,控制开关 dialogController: XDialogController = {} as XDialogController - build() { - Column() { - Button({ buttonStyle: ButtonStyleMode.TEXTUAL }) { + build() + { + Column() + { + Button + ({ buttonStyle: ButtonStyleMode.TEXTUAL }) + { Image($r('sys.media.ohos_ic_public_more')) .width(26).height(26) - }.width(65) - .onClick(() => { - if (this.dialogController != null) { - this.dialogController.open() - } - }) + } + . + width(65) + .onClick(() => { + if (this.dialogController != null) { + this.dialogController.open() + } + }) XDialogList({ // 控制器 @@ -127,14 +176,18 @@ struct MyView { autoCancel: true }) - }.width('100%').height('100%') + } + . + width('100%').height('100%') } } ``` -## 3.[网络请求](./src/main/ets/http/HttpHelper.ts) + +## 3.[网络请求](./src/main/ets/http/HttpHelper.ets) + > 使用时建议二次封装 -> +> > 参数定义 > ```typescript > /** @@ -148,34 +201,37 @@ struct MyView { > ``` ### 3.1.get请求 + ```typescript - HttpHelper.get() - .get>(url.url.startsWith('http') ? url.url : GlobalValue.getInstance().envUrl + url.url, - data ? JSON.stringify(data) : undefined, { - userId: GlobalValue.getInstance().userId, - clientId: GlobalValue.getInstance().getClientId(), - version: ConstantValue.VERSION, - deviceType: '01', - timeStamp: timeStamp + '', - sign: sign, - phoneModel: 'sign', - phoneVersion: 'sign', - phoneBrand: 'HarmonyOS' - }, url.apiNo) - .then((res: HttpResult) => { - if (res.status === '0') { - resolve(res.data as T) - } else { - reject(new Error(res.message)) - } +HttpHelper.get() + .get>(url.url.startsWith('http') ? url.url : GlobalValue.getInstance().envUrl + url.url, + data ? JSON.stringify(data) : undefined, { + userId: GlobalValue.getInstance().userId, + clientId: GlobalValue.getInstance().getClientId(), + version: ConstantValue.VERSION, + deviceType: '01', + timeStamp: timeStamp + '', + sign: sign, + phoneModel: 'sign', + phoneVersion: 'sign', + phoneBrand: 'HarmonyOS' + }, url.apiNo) + .then((res: HttpResult) => { + if (res.status === '0') { + resolve(res.data as T) + } else { + reject(new Error(res.message)) + } - }) - .catch((error: Error) => { - reject(error) - }) + }) + .catch((error: Error) => { + reject(error) + }) ``` + ### 3.2.postJson + ```typescript HttpHelper.get() @@ -203,7 +259,9 @@ HttpHelper.get() reject(error) }) ``` + ## 4.[自定义view](./src/main/ets/view) + ### 4.1.[LoadingView](./src/main/ets/view/LoadingView.ets) > 封装了loading的根布局 @@ -225,7 +283,9 @@ build() { } } ``` + ## 5.[Windows](./src/main/ets/utils/WindowHelper.ets) + ### 5.1.弹出自定义窗口 > 弹出自定义窗口 @@ -279,9 +339,6 @@ WindowHelper.topRectHeight() WindowHelper.bottomRectHeight() ``` - - - # **** 常见问题 diff --git a/src/main/ets/utils/ToolsHelper.ets b/src/main/ets/utils/ToolsHelper.ets index 80d371b..330f521 100644 --- a/src/main/ets/utils/ToolsHelper.ets +++ b/src/main/ets/utils/ToolsHelper.ets @@ -1,6 +1,6 @@ import promptAction from '@ohos.promptAction'; import { BusinessError, deviceInfo } from '@kit.BasicServicesKit'; -import { HashMap, util } from '@kit.ArkTS'; +import { HashMap } from '@kit.ArkTS'; import { DeviceInfo } from '../bean/DeviceInfo'; export interface Btn { @@ -91,6 +91,15 @@ function customDialogBuilder(option: ListOptions, dialogId: number) { * 常用方法 */ export class ToolsHelper { + /** + * 弹出Toast + * @param msg + */ + static log(...args: ESObject[]) { + const k = ToolsHelper.getStackKey()?.split('/') + console.log(`========>${k?k[k.length-1].split('.')[0]:''}::`, args) + } + /** * 弹出Toast * @param msg @@ -224,6 +233,10 @@ export class ToolsHelper { private static deviceInfo: DeviceInfo + /** + * 获取设备信息 + * @returns {@link DeviceInfo} + */ static getDeviceInfo() { if (!ToolsHelper.deviceInfo) { ToolsHelper.deviceInfo = new DeviceInfo() diff --git a/src/main/ets/utils/WindowHelper.ets b/src/main/ets/utils/WindowHelper.ets index 63118fe..02fedfa 100644 --- a/src/main/ets/utils/WindowHelper.ets +++ b/src/main/ets/utils/WindowHelper.ets @@ -81,7 +81,7 @@ export class WindowHelper { WindowHelper._bottomRectHeight = avoidArea.bottomRect.height let avoidArea2 = WindowHelper._windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT); WindowHelper._topRectHeight = avoidArea2.topRect.height - console.log('=====>', WindowHelper._topRectHeight) + // console.log('=====>', WindowHelper._topRectHeight) } }