posts - 501,comments - 0,views - 23802

简化代码的一些措施

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
        });
    }
}




posted on   垂序葎草  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示