软件开发过程中前后端联调相关问题

一、接口调用三步曲

1. uniapp接口调用

  1. data中定义项目1

  2. onload中调用

例如:this.getSwiperList()//调用获取轮播图数据的方法

  1. method中定义获取方法在这里插入图片描述

2. 微信小程序接口调用

  1. reques.js中接口封装

如:ScenicspotInfo (data)=> request(POST,‘getScenicspotInfo’,data),

在这里插入图片描述

  1. 需求页面.js(如detail.js)顶部引用接口封装文件

如: const $api = require(“…/…/utils/request”).Api;

在这里插入图片描述

  1. 定义调用页面数据接口的方法
TicketDetailApiWay(){
 $api.ScenicspotInfo({ 'id':1 }).then((res) => {
 this.setData({})
 }).catch(() => {console.log(error)})
}

在这里插入图片描述
需要传递参数(如id)测试的在这里插入图片描述

二、前端如何规范地模拟数据?

1. 接口文档规范(eg:首页楼层数据)

  • 1.请求路径:https://请求域名/api/public/v1/home/floordata
  • 2.请求方法:GET
  • 3.请求参数
参数名参数说明备注
  • 4.响应参数
参数名参数说明备注
floor_title楼层标题
product_list楼层内容列表
name名称
image_src图片路径
image_width商品图片宽度
open_type导航链接类型
navigator_url导航链接路径
  • 5.响应数据
{
    "message": [
        {
            "floor_title": {
                "name": "时尚女装",
                "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_title.png"
            },
            "product_list": [
                {
                    "name": "优质服饰",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_1@2x.png",
                    "image_width": "232",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=服饰"
                },
                {
                    "name": "春季热门",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_2@2x.png",
                    "image_width": "233",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=热"
                },
                {
                    "name": "爆款清仓",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_3@2x.png",
                    "image_width": "233",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=爆款"
                },
                {
                    "name": "倒春寒",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_4@2x.png",
                    "image_width": "233",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=春季"
                },
                {
                    "name": "怦然心动",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor01_5@2x.png",
                    "image_width": "233",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=心动"
                }
            ]
        },
        {
            "floor_title": {
                "name": "户外活动",
                "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_title.png"
            },
            "product_list": [
                {
                    "name": "勇往直前",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_1@2x.png",
                    "image_width": "232",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=户外"
                },
                {
                    "name": "户外登山包",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_2@2x.png",
                    "image_width": "273",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=登山包"
                },
                {
                    "name": "超强手套",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_3@2x.png",
                    "image_width": "193",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=手套"
                },
                {
                    "name": "户外运动鞋",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_4@2x.png",
                    "image_width": "193",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=运动鞋"
                },
                {
                    "name": "冲锋衣系列",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor02_5@2x.png",
                    "image_width": "273",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=冲锋衣"
                }
            ]
        },
        {
            "floor_title": {
                "name": "箱包配饰",
                "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_title.png"
            },
            "product_list": [
                {
                    "name": "清新气质",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_1@2x.png",
                    "image_width": "232",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=饰品"
                },
                {
                    "name": "复古胸针",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_2@2x.png",
                    "image_width": "263",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=胸针"
                },
                {
                    "name": "韩版手链",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_3@2x.png",
                    "image_width": "203",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=手链"
                },
                {
                    "name": "水晶项链",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_4@2x.png",
                    "image_width": "193",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=水晶项链"
                },
                {
                    "name": "情侣表",
                    "image_src": "https://www.zhengzhicheng.cn/pyg/pic_floor03_5@2x.png",
                    "image_width": "273",
                    "open_type": "navigate",
                    "navigator_url": "/pages/goods_list?query=情侣表"
                }
            ]
        }
    ],
    "meta": { "msg": "获取成功", "status": 200 }
}

2. 运用数据

2.1 左边单图片渲染时,利用{}动态设置图片的宽,下标为数组的索引,不用循环

:src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}"

2.2右边多图片渲染时,用循环,每循环一次可以得到一个item项,命名为item2和索引命名为i2,将索引当作key,

v-for="(item2,i2) in item.product_list"
//那么图片的地址为
<image src="item2.image_src" mode=""></image>
//完整渲染代码为
<view class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0">
<image :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode="widthFix"/>
</view>

2.3 点击图片如何跳转到对应的商品列表页?

在这里插入图片描述

  • 分析:给到的数据为"navigator_url": “/pages/goods_list?query=服饰”( ——问号前面是路径,问好后面是路径的参数),实际创建的商品列表页在分包目录下,如何跳转到各自对应的正确目录? 涉及到自定义url、拆分字符串、拼接字符串、为每个图片对象挂载一个自定义url属性(——即将要跳转到的地址),对原数据进行分割,先做字符串的split分割(——分割之后索引为1的项就是问号后面的值,即路径的参数,)再拿到索引为1的项,动态拼接过来。
// 通过双层 forEach 循环,处理 URL 地址
// prod是形参,用于遍历,参数从问号处做分割,split会把字符串以问号分割成数组,索引为1的是问号后面的值
res.message.forEach(floor => {
	floor.product_list.forEach(prod => {
	prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
})
})
  • 成功结果如下:在这里插入图片描述

3. 规范地模拟数据之多级菜单分类

//一级、二级、三级分类之间通过children属性嵌套,最外层[]里面存放所有一级分类,一级分类的children属性就是它的二级分类
{
    "message": [
        {
            "cat_id": 1,
            "cat_name": "大家电",
            "cat_pid": 0,
            "cat_level": 0,
            "cat_deleted": false,
            "cat_icon": "",
            "children": [
                {
                    "cat_id": 3,
                    "cat_name": "电视",
                    "cat_pid": 1,
                    "cat_level": 1,
                    "cat_deleted": false,
                    "cat_icon": "",
                    "children": [
                    {
                        "cat_id": 5,
                        "cat_name": "曲面电视",
                        "cat_pid": 3,
                        "cat_level": 2,
                        "cat_deleted": false,
                        "cat_icon": "full/2fb113b32f7a2b161f5ee4096c319afedc3fd5a1.jpg"
                    }]
                }
            ]
        }
    ],
    "meta": {
        "msg": "获取成功",
        "status": 200
    }
}
  

在这里插入图片描述

三、涉及项目逻辑问题的接口调用

示例:

在这里插入图片描述

1、上传图片接口请求

    submit() {
        console.log('点击完成')
        this.cropper.getImg((obj) => {
            //app.globalData.imgSrc = obj.url;
            // console.log( obj.url)
            // 上传方法 ,这个name属性是最重要的属性,他对应着后端传参数的值
            wx.uploadFile({
                url: 'http://192.168.2.50:8060/Api/UploadStarFilmImg',
                filePath: obj.url,
                name: 'file',
                header: {
                    'Content-Type': 'multipart/form-data' // POST默认值
                },
                method: 'post',
                formData: {
                    code: ''
                },
                success: (res) => {
                    //console.log('图片接口数据-----', res)
                    let result = JSON.parse(res.data)
                    console.log('图片接口数据result-----', result)
                    // 图片code存到本地
                    this.setData({
                        imgcode: res.code
                    })
                    app.globalData.imgcode = result.code
                    app.globalData.imgSrc = 'http://192.168.2.50:8012/SFImgPath/' + result.img
                    wx.navigateTo({
                        url: '/pages/index/addnext?src=' + config + result.img + "&imgCode=" + result.code,
                        //  裁剪完成点击跳转,携带的图片路径传入下一个页面,拼接字符串实现
                        // url: `/pages/index/addnext?imgSrc=${obj.url}`,
                    })
                },
                fail:(res)=>{
                    console.log(res)
                }
            });
        });
    },

2、上传录音和提交信息接口

上传录音接口请求 finish.js点击制作完成时
提交信息接口请求 addfinished.js、addfinishedtxt.js点击制作完成时

    Toendpage() {
        //------------------------------------------------------------------------------上传录音
        var voice = this.data.voice;
        wx.uploadFile({
            url: 'http://192.168.2.50:8060/Api/UploadStarFilmVoice',
            filePath: voice,
            name: 'file',
            header: {
                'Content-Type': 'multipart/form-data' // POST默认值
            },
            method: 'post',
            formData: {
                code: ''
            },
            success: res => {
                //console.log('录音接口数据-----', res)
                var resultV = JSON.parse(res.data);
                app.globalData.voice = resultV.voice
                console.log('录音接口数据resultV-----', resultV)
                // 将voice存到全局
                app.globalData.voice = 'http://192.168.2.50:8012/SFVoicePath/' + resultV.voice
                this.setData({
                    voice: resultV.voice
                })
                wx.navigateTo({
                    url: '/pages/addfinished/addfinished?voice=' + config + resultV.voice,
                })
            }
        })
        // 关闭loading提示
        wx.hideLoading()
        //---------------------------------------------------voice的提交信息接口
        wx.request({
            url: 'http://192.168.2.50:8060/Api/SubmatStarFilmInfo',
            data: {
                code: app.globalData.imgcode,
                sftype: 0,
                word: app.globalData.voice,
                status: 1,
                signature: this.data.bywho,
                towritten: this.data.towritten,
            },
            header: {
                'Content-Type': 'application/x-www-form-urlencoded' // POST默认值
            },
            method: 'post',
            success: res => {
                var resultS = res.data;
                console.log('提交信息接口数据resultS-----', resultS)
                wx.navigateTo({
                    url: '/pages/end/end?src=' + resultS.code + "&Voice=" + resultS.voice,
                })
            }
        })

    },

3、查询接口请求end.js和endtxt.js,oppositeget.js和oppvoice.js

       onLoad(options) {
        // 录音时间赋值
        let content = wx.getStorageSync('content')
        this.setData({
            speck_time: content.speck_time,
        })
        this.data.imgCode = app.globalData.imgcode
        console.log(this.data.imgCode)
        // 清除code
        app.globalData = {
            userInfo: null,
            imgSrc: '',
            imgcode: '',
            voice: '',
            CreateTimeStr: '',
        }
        var that = this;
        wx.request({
            url: 'http://192.168.2.50:8060/Api/SelStarFilmInfo',
            data: {
                code: this.data.imgCode
            },
            method: 'post',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                console.log('直接打印不用转化', (res.data))
                that.setData({

                    word: res.data.data.Word,
                    Signature: res.data.data.Signature,
                    ToWritten: res.data.data.ToWritten,
                    imgSrc: 'http://192.168.2.50:8012/SFImgPath/' + res.data.data.Img
                })
            }
        })
    },

4、全局变量设置

在这里插入图片描述

5、清除code

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前后是指前和后开发过程需要进行数据交互和接口试的过程。一般来说,前后可以分为以下几个步骤: 1. 确定接口文档:前后需要共同确定接口文档,包括接口的请求参数、响应参数、返回码等内容。 2. 前用接口:前根据接口文档编写相应的代码,用后提供的接口,并将请求参数发送给后。 3. 后处理请求:后接收到前发送的请求后,根据请求参数进行相应的处理,并返回响应数据。 4. 前处理响应数据:前接收到后返回的响应数据后,根据响应参数进行相应的处理,例如展示数据、更新页面等。 5. 试接口:在前后过程,需要不断地试接口,确保接口的正确性和稳定性。 6. 修复问题:如果在过程发现问题,需要及时修复并重新试接口。 除了以上步骤,前后还需要注意以下几点: 1. 接口命名规范:前后需要遵守一定的接口命名规范,以避免出现混淆和错误用的情况。 2. 参数校验:前后需要对接口的请求参数进行校验,以确保参数的正确性和安全性。 3. 接口安全:前后需要对接口进行安全性验证,避免恶意攻击和非法用。 总之,前后是一个需要前后密切配合、不断试的过程,通过良好的沟通和协作,可以让项目顺利进行并保证接口的正确性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值