微信小程序--wx.request封装和使用

栏目: IOS · Android · 发布时间: 6年前

内容简介:5、尾言
  • 微信小程序基础
  • 熟悉 web 开发的朋友可能对 axios 情有独钟。
  • wx.request 的二次封装
  • api 的集中管理和使用

2、配置 baseUrl

  1. 一般情况下,项目中的 baseUrl 域名前缀、登录的 code用户信息 等都是配置在 app.js
//app.js
App({
  onLaunch: function () {
    wx.login({
      success: res => {
        if (res.code) {
          this.globalData.loginCode = res.code   // 获取的code码,以进一步换取用户信息
          // res: {
          //   code: "061Zltjh1sXj6s0z96hh1Z1njh1Zltj5"
          //   errMsg: "login:ok"
          // }
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    loginCode: null,
    baseUrl: 'https://...'
  }
})
复制代码

3、封装 wx.request

  1. 在小程序目录下建立 utils 文件夹,并在文件夹下创建 request.js 文件
// request.js
const request = (options) => {
  return new Promise((resolve, reject) => {
    const { data, method } = options
    if(data && method !== 'get') {
      options.data = JSON.stringify(data)
    }
    wx.request({
      header: { 'Content-Type': 'application/json' },
      ...options,
      success: function(res) {
        if(res.data.code === 2000) {
          resolve(res.data)
        } else {
          reject(res.data)
        }
      },
      fail: function(res) {
        reject(res.data)
      }
    })
  })
}
export default request
复制代码

4、 api 的集中管理和使用

  1. 在小程序目录下建立 api 文件夹,并在文件夹下创建 user.js 文件(文件名推荐按模块命名)
  2. 用过 axios 的朋友接下来应该会感觉很熟悉
// user.js
import request from '../utils/request.js'
// baseUrl也可拼接在request.js中,当有多个鉴权模块,放在这里更灵活
const baseUrl = getApp().globalData.baseUrl

export function apiLogin(data) {
  return request({
    url: `${baseUrl}/user/login`,
    method: 'post',
    data
  })
}

export function apiGetUserInfo() {
  return request({
    url: `${baseUrl}/user/userInfo`,
    method: 'get'
  })
}

export function apiModifyUserPassword(data) {
  return request({
    url: `${baseUrl}/user/modifyPassword`,
    method: 'put',
    data
  })
}

export function apiLogout() {
  return request({
    url: `${baseUrl}/user/logout`,
    method: 'delete'
  })
}
复制代码
  1. 在页面中使用
// pages/login/login.js
import { apiLogin } from '../../api/user.js'
Page({
  onLoad: function (options) {
    this.login()
  },
  login() {
    apiLogin({
      // api params
    }).then(res => {
      // handle success
    }).catch(error => {
      // handle error
    })
  }
})
复制代码

5、尾言

  1. 由于涉及到 ES6 语法,需要在本地设置中开启 ES6ES5
  2. 小程序开发过程中应注意 this 指向性问题。
  3. 感谢浏览,若有不足之处请指正,欢迎留言探讨。

以上所述就是小编给大家介绍的《微信小程序--wx.request封装和使用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Approximation Algorithms

Approximation Algorithms

Vijay V. Vazirani / Springer / 2001-07-02 / USD 54.95

'This book covers the dominant theoretical approaches to the approximate solution of hard combinatorial optimization and enumeration problems. It contains elegant combinatorial theory, useful and inte......一起来看看 《Approximation Algorithms》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具