大家好,本人是oh三方库eftool的作者,应大量开发者的需求在eftool的1.2.1-rc.2版本中已经新增efRcp请求模块,欢迎大家使用,现介绍一下已有功能和使用方式
本示例使用eftool版本为API12的1.2.1-rc.2版本
首选需要引入相关设置如
import {
efRcpClientApi,
efRcpParams
} from '@yunkss/eftool'
使用
首选按需进行初始化参数配置
> 请求相关的参数配置建议在Ability的onWindowStageCreate方法中配置
> 可使用BuildProfile获取到当前运行的环境如debug/qa/test/release等 然后设置不同的请求信息
if(运行环境===debug){
//关于rcp的配置
efRcpParams.baseURL = "http://192.168.0.101:18088";
efRcpParams.loadingTxt = '小的快马加鞭...';
efRcpParams.isLogger = true;
efRcpParams.isLoading = false;
}
if(运行环境===qa){
//关于rcp的配置
efRcpParams.baseURL = "http://192.168.0.222:18088";
efRcpParams.loadingTxt = '正在努力加载...';
//设置证书内容
efRcpSecurityParam.certContent = '111111111';
efRcpParams.isLogger = false;
efRcpParams.isLoading = true;
}
登录示例
async testLogin() {
let dto = await efRcpClientApi.post <outdto<userdto>>('/api/eftool/login', {
'account': 'efadmin',
'pwd': '123456'
}, {}, {}, '更改了请求loading内容...');
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
//请求成功后将token存储在efRcpParams.tokenValue
efRcpParams.tokenValue = dto.getDataRow().token;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
post示例 json格式
async testPostJSON() {
let dto = await efRcpClientApi.post <outdto<userdto>>('/api/eftool/post', {
"nickName": "旺旺崔冰冰",
"account": 'yunkss@163.com',
"age": 12,
"hobby": ["吃", "喝", "敲代码"],
"sex": true
});
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
post示例 form格式
async testPostForm() {
let dto = await efRcpClientApi.postForm<outdto<string>>('/api/eftool/postFormData', {
"nickName": "旺旺崔冰冰",
"account": 'yunkss@163.com',
"age": 12,
"hobby": ["吃", "喝", "敲代码"],
"sex": true
});
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
get示例
async testGet() {
let dto = await efRcpClientApi.get<outdto<string>>('/api/eftool/get/122341', {
"testHeader": "111"
}, {
"testCookie": "csx"
});
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
put示例
async testPUT() {
let dto = await efRcpClientApi.put<outdto<string>>('/api/eftool/put', {
"nickName": "旺旺崔冰冰",
"account": 'yunkss@163.com',
"age": 12,
"hobby": ["吃", "喝", "敲代码"],
"sex": true
});
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
delete示例
async testDelete() {
let dto = await efRcpClientApi.delete<outdto<string>>('/api/eftool/delete/5345345');
if (dto.getSuccess()) {
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
} else {
ToastUtil.showToast(JSONObject.toJSONString(dto));
}
}
cancel示例
async testCancel() {
let dto = await efRcpClientApi.post <outdto<userdto>>('/api/eftool/post', {
"nickName": "旺旺崔冰冰",
"account": 'yunkss@163.com',
"age": 12,
"hobby": ["吃", "喝", "敲代码"],
"sex": true
});
await efRcpClientApi.cancel('/api/eftool/post');
}
上传示例
async testUploadFile() {
//拉起选中照片
let result = await PickerUtil.selectPhotoVideo();
//判断是否成功
if (result.getSuccess()) {
let list = result.getDataTable();
//获取选中照片uri
let item = list[0];
//创建文件信息
let fileUriObject = new fileUri.FileUri(item);
//获取文件名
let name = fileUriObject.name;
//获取流
let bufferImg = FileUtil.getBufferByURI(item);
this.btnCtx = "正在上传中...";
this.showDownBtn = Visibility.Visible;
//上传
let dto =
await efRcpClientApi.uploadFile<outdto<string>>('/api/eftool/upload', {
"file": {
contentType: 'multipart/form-data',
remoteFileName: name,
contentOrPath: {
content: bufferImg.getDataRow()
}
}
}, (progress) => {
if (progress >= 100) {
this.showDownBtn = Visibility.None;
this.process = 0;
} else {
this.process = progress;
}
});
let res = JSONObject.toJSONString(dto);
ToastUtil.showToast(res);
this.message = res;
}
}
下载示例
async testDownloadStream() {
this.btnCtx = "正在下载中...";
this.showDownBtn = Visibility.Visible;
let dto =
await efRcpClientApi.downloadStream<outdto<string>>('/api/eftool/download/a89d3c16-1dba-4a41-909f-1214f52d3466-IMG_20240214_212821.jpg',
'abc.jpg', (progress) => {
if (progress >= 100) {
this.showDownBtn = Visibility.None;
this.process = 0;
} else {
this.process = progress;
}
});
//获取文件路径
let url = FileUtil.getFilesDirPath('', 'abc.jpg');
//转换成uri
let uri = fileUri.getUriFromPath(url);
//预览
await PreviewUtil.previewImage(uri);
this.message = JSONObject.toJSONString(dto);
}
async testDownloadFile() {
this.btnCtx = "正在下载中...";
this.showDownBtn = Visibility.Visible;
let dto =
await efRcpClientApi.downloadFile<outdto<string>>('/api/eftool/download/7626f4b1-9835-4eb4-8660-ae299ba4733a-IMG_20240214_212933.jpg',
'csx.jpg', (progress) => {
if (progress >= 100) {
this.showDownBtn = Visibility.None;
this.process = 0;
} else {
this.process = progress;
}
})
//获取文件路径
let url = FileUtil.getFilesDirPath('', 'csx.jpg');
//转换成uri
let uri = fileUri.getUriFromPath(url);
//预览
await PreviewUtil.previewImage(uri);
this.message = JSONObject.toJSONString(dto);
}