【愚公系列】2022年02月 微信小程序-Request网络请求的封装

举报
愚公搬代码 发表于 2022/02/19 17:42:19 2022/02/19
【摘要】 前言Request网络请求在任何应用中都是必不可少的,但微信小程序的wx.request()太过单一没法满足复杂的请求,所以就有本片文章讲解如何封装小程序的请求 一、微信小程序首先我们来看一下官方文档中介绍的wx.request()默认使用方式wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: {}, header: { ...

前言

Request网络请求在任何应用中都是必不可少的,但微信小程序的wx.request()太过单一没法满足复杂的请求,所以就有本片文章讲解如何封装小程序的请求

一、微信小程序

首先我们来看一下官方文档中介绍的wx.request()
在这里插入图片描述
默认使用方式

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {},
  header: {
      'content-type': 'application/json' // 默认值
  },
  success: function(res) {
    console.log(res.data)
  }
})

二、Request.js封装

Request.js是基于WX API的封装只有一个文件

const URL = 'http://love520.com'
 
module.exports = {
   //封装request方法,第一个参数请求地址,第二个参数传递参数,第三个参数请求方式
    request:function(url,data={},method){
       //返回promise对象  resolve 成功的回调方法 reject失败的回调方法 一旦发生就不会改变
        return new Promise((resolve, reject)=>{
            wx.request({
                url: URL + url,
                data,
                method,
                header:{
                    'token':wx.getStorageSync('token')
                },
                success:(res)=>{
                    if(res.statusCode === 200 && res.data.code === 200){
                        resolve(res.data)
                        wx.hideLoading()
                    } else {
                        wx.showToast({
                            icon:'error',
                            title: res.Msg,
                        })
                        reject(res.Msg)
                    }
                },
                fail:(err)=>{
                    wx.showToast({
                        icon:'error',
                        title: '接口无响应',
                    })
                    reject('接口无响应')
                }
            })
        })
    }
}

三、Route.js封装

Route.js主要是区分业务的怎删改查根据领域模型划分多个业务体系

const { request } = require('./request.js')
//restful类型接口
module.exports = {
  GetUsers:(data) => request('/api/identity/users/{id}',{},'GET'),
  PostUsers:(data) => request('/api/identity/users/{id}',data,'POST'),
  PutUsers:(data) => request('/api/identity/users/{id}',data,'PUT'),
  DeleteUsers:(data) => request('/api/identity/users/{id}',{},'DELETE'),
}

在这里插入图片描述

四、使用

import { GetUsers,PostUsers,PutUsers,DeleteUsers} from "../../utils/route"
GetUsers(id,{}).then((res)=>{
    this.setData({
        list: res.data
    })
})
PostUsers(id,data).then((res)=>{
    wx.showToast({
        icon:'none',
        title: res.Msg,
    })
})
PutUsers(id,data).then((res)=>{
    wx.showToast({
        icon:'none',
        title: res.Msg,
    })
})
DeleteUsers(id,{}).then((res)=>{
    wx.showToast({
        icon:'none',
        title: res.Msg,
    })
})
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。