徐勤民 ee16cac007 自动签相关 | há 2 meses atrás | |
---|---|---|
src | há 2 meses atrás | |
.gitignore | há 7 meses atrás | |
BuildProfile.ets | há 2 meses atrás | |
CHANGELOG.md | há 2 meses atrás | |
Index.ets | há 2 meses atrás | |
LICENSE | há 7 meses atrás | |
README.md | há 2 meses atrás | |
UTILS.md | há 7 meses atrás | |
build-profile.json5 | há 2 meses atrás | |
consumer-rules.txt | há 2 meses atrás | |
hvigorfile.ts | há 7 meses atrás | |
obfuscation-rules.txt | há 7 meses atrás | |
oh-package.json5 | há 2 meses atrás |
ohpm install @szyx/sdk_base
缓存工具类,运行时存储,应用停止运行后清空
import { AppStorageHelper } from '@szyx/sdk_base/Index'
// 存储string数据
AppStorageHelper.save(StorageKeys.CLIENT_ID, d)
// 获取存储的strign数据
let d = AppStorageHelper.get(StorageKeys.CLIENT_ID)
// 删除指定存储
AppStorageHelper.delete(StorageKeys.CLIENT_ID)
永久存储类,应用停止后也不会清空 需要验证,更新应用会不会被清理 可存储类型
number | string | boolean | Array<number> | Array<string> | Array<boolean> | Uint8Array
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('>>>>>')
})
常用方法工具栏 基础方法
import { ToolsHelper } from '@szyx/sdk_base';
ToolsHelper.showMessage('Hello Word!')
打印格式:
========>${顶层调用栈}::
import { ToolsHelper } from '@szyx/sdk_base';
ToolsHelper.log('Hello Word!')
import { ToolsHelper } from '@szyx/sdk_base';
ToolsHelper.getStackKey()
import { ToolsHelper } from '@szyx/sdk_base';
ToolsHelper.getDeviceInfo()
常用正则
import { ValidatorHelper } from '@szyx/sdk_base';
ValidatorHelper.isPhone('13800000000')
计算相关
import { AlgorithmHelper } from '@szyx/sdk_base';
AlgorithmHelper.calculateDistance(x1, y1, x2, y2)
打开webview页面
import { XWebHelper } from '@szyx/sdk_base';
// 必须先引入,否则无法跳转
const XWebview = import('../pages/XWebview');
XWebHelper.openWeb({
url: 'https://www.baidu.com',
title: '百度一下',
})
import { XDialogController } from '../dialog/XDialogController';
import { XDialogList } from '../dialog/XDialogList';
@
Component
struct
MyView
{
// 控制器,控制开关
dialogController: XDialogController = {} as XDialogController
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()
}
})
XDialogList({
// 控制器
controller: this.dialogController,
// 标题(可选)
title: '选择您的操作',
// 选择内容列表
values: ['刷新', '浏览器打开', '分享', '复制地址'],
// 用户选择事件
onSelected: (index: number, value: string) => {
ToolsHelper.showMessage(`用户选择了第${index}个,内容为:${value}`)
},
// 用户取消事件
onCancel: () => {
ToolsHelper.showMessage('用户取消操作')
},
// 是否可取消(点击空白处,或者物理返回键)
autoCancel: true
})
}
.
width('100%').height('100%')
}
}
使用时建议二次封装
参数定义
> /** > * > * @param url url地址 > * @param data 请求参数 > * @param headers 请求头 > * @param apiNo 请求标识,取消请求或者去重使用|考虑做自动重试使用 > * @returns > */ > ``` ### 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().userIds,
clientId: GlobalValue.getInstance().getClientIds(),
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)
})
### 3.2.postJson
typescript
HttpHelper.get() .post>(url.url.startsWith('http') ? url.url : GlobalValue.getInstance().envUrl + url.url,
data ? JSON.stringify(data) : undefined, {
userId: GlobalValue.getInstance().userIds,
clientId: GlobalValue.getInstance().getClientIds(),
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)
})
## 4.[自定义view](./src/main/ets/view)
### 4.1.[LoadingView](./src/main/ets/view/LoadingView.ets)
> 封装了loading的根布局
tsx import { LoadingView } from '@szyx/sdk_base'
@State isLoading: boolean = false
build() {
Stack() {
LoadingView({ isLoading: this.isLoading }) {
Column() {
Text('正常布局')
}
.width('100%')
.height('100%')
}
}
}
### 4.2.[SafeView](./src/main/ets/view/SafeView.ets)
> 基础布局
tsx import { SafeView } from '@szyx/sdk_base'
@State isLoading: boolean = false
build() {
SafeView({
isLoading: this.isLoading,
titleText: '注册',
onClickLeft:{img:$r("app.media.base_back"),
onClick:()=>{}
},
onClickRight:{
text:'添加',
color:'#ff1d9999',
onClick:()=>{}
}
}){
// 自己的布局
}
}
## 5.[Windows](./src/main/ets/utils/WindowHelper.ets)
### 5.1.弹出自定义窗口
> 弹出自定义窗口
tsx import { WindowHelper } from '@szyx/sdk_base'
WindowHelper.open({
name: '登录弹窗',
router:'login'
})
WindowHelper.close()
### 5.2.设置沉浸式相关
tsx // 初始化 import { WindowHelper } from '@szyx/sdk_base'; export default class AppAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
WindowHelper.windowClass = windowStage.getMainWindowSync()
});
GlobalContext.setContext(this.context)
}
}
tsx // 需要的界面 // 设置是否全屏 WindowHelper.setWindowLayoutFullScreen(true)
// 隐藏状态栏 WindowHelper.hideStatusBar()
// 显示状态栏 WindowHelper.showStatusBar()
// 顶部安全区高度 WindowHelper.topRectHeight()
// 底部安全区高度 WindowHelper.bottomRectHeight() ```