简化代码的一些措施
http.js
/**
* send http request util file
*/
import http from '@ohos.net.http';
const Base_Url = 'http://nja5ih.natappfree.cc'
export default{
get(url, fn){
// 每一个httpRequest对应一个http请求任务,不可复用
let httpRequest = http.createHttp();
let options = {
method: http.RequestMethod.GET,
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
}
}
// http://cmn8ut.natappfree.cc/api/login
httpRequest.request(
// ***填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定***
Base_Url + url,
options,
(err, data) => {//err成功与否,data后台服务器返回的结果
if (!err) {//成功
//data.result >> 数据类型:String >> 为了后续的操作,需将data.result转成JSON对象
fn(JSON.parse(data.result));
} else {//失败
console.info('error:' + JSON.stringify(err));
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();//对象销毁
}
}
);
},
post(url, parmas, fn){
// 每一个httpRequest对应一个http请求任务,不可复用
let httpRequest = http.createHttp();
let options = {
method: http.RequestMethod.POST,
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
extraData: parmas
}
// http://cmn8ut.natappfree.cc/api/login
httpRequest.request(
// ***填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定***
Base_Url + url,
options,
(err, data) => {//err成功与否,data后台服务器返回的结果
if (!err) {//成功
fn(JSON.parse(data.result));
} else {//失败
console.info('error:' + JSON.stringify(err));
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();//对象销毁
}
}
);
}
}
classes.js
import router from '@ohos.router'
import HttpUtil from '../../http/http.js'
export default {
data: {
swiperImages:[],
quickStart: [],
starClasses: [],
hotCourses: [],
},
onInit() {//界面初始化时调用此方法
//发送http请求至后台服务器拿到首页轮播图的网络地址集合,实现轮播图加载
//swiperImages:['http://ip:port/images/swiper1.png','http://ip:port/images/swiper2.png']
//http.get() => result => this.swiperImages = result;
//
this.swiperImages = ['swiper1.jpg','swiper2.jpg','swiper3.jpg','swiper4.jpg','swiper5.jpg'];
//初始化快捷入口的数据
this.quickStart = [
{
name:'课程', image: 'course-icon.png', url: 'pages/course/course'
},
{
name:'班级', image: 'class-icon.png', url: 'pages/classes/classes'
},
{
name:'培训', image: 'train-icon.png', url: 'pages/train/train'
},
{
name:'测试', image: 'test-icon.png', url: 'pages/test/test'
},
{
name:'群组', image: 'group-icon.png', url: 'pages/group/group'
}
];
//发送http请求至后台服务,经过业务逻辑处理后,拿到排名前四的班级的信息
//http.get() => result => this.starClasses = result
//starClasses: [{name, master, stuNum, image},...]
HttpUtil.get('/api/getStarClasses', (result)=>{
this.starClasses = result.data;
})
this.starClasses = [
{
name : '软件20-1班', master : 'xxx', stuNum : 35, image: 'class1.jpg'
},
{
name : '软件20-3班', master : 'xxx', stuNum : 35, image: 'class2.jpg'
},
{
name : '软件20-5班', master : 'xxx', stuNum : 35, image: 'class3.jpg'
},
{
name : '软件20-7班', master : 'xxx', stuNum : 35, image: 'class4.jpg'
},
];
//发送http请求至后台服务,经过业务逻辑处理后,拿到排名前四的班级的信息
//http.get() => result => this.starClasses = result
//hotCourses: [{name, master, stuNum, image},...]
this.hotCourses = [
{
name: 'Java从入门到放弃', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng1.jpg'
},
{
name: 'Python从入门到放弃', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng2.jpg'
},
{
name: 'C++从入门到放弃', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng3.jpg'
},
{
name: 'C语言从入门到放弃', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng4.jpg'
},
{
name: 'HarmonyOS北向应用开发', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng5.jpg'
},
{
name: 'Spring微服务架构设计', teacher: '周卓', stuNum: 35, hour: 16, score: 3, image: 'kecheng6.jpg'
},
]
},
forward(url){
router.push({
url: url
});
}
}
标签:封装,name,如何,url,image,jpg,HarmonyOS,http,stuNum
From: https://www.cnblogs.com/chuixulvcao/p/16996971.html