微信小程序请求文档配置及使用

配置域名配置config.js
// config 域名配置
const config = {
     host: 'https://test.17m.club/assets',
}
module.exports = config
请求拦截配置request.js
const config = require('../config/index.js')
import utils from '../utils/index.js'
const http = function(method, url, data, header = {}) {
  const token = utils.storage.getItem('token');
  if (token){
    header.Authorization = `bearer ${token}`;
  }
  if (!header['Content-Type']) {
    header['Content-Type'] = 'application/json';
  }
  return new Promise(function (resolve, reject) {
    wx.request({
      method: method,
      url: config.host + url,
      data: data,
      header: header,
      success: function (res) {
        if (res.statusCode === 200) {
          reslve(res.data);
        } else {
          reject(error)
          // utils.format.showErr(res.data.msg)
        }
      },

      error: function (error) {
        utils.format.showErr('网络请求错误')
        reject(error)
      }
    });
  });
}

export const post = (url, data) => {
  return http('post', url, data)
}

export const get = (url, data) => {
  return http('get', url, data)
}
请求index文档 index.js
import {post, get} from './request'

//登录
export const wxLogin = (params) =>  post('/api/oauth/wx', params)

最后我们可以在app.js引用,然后在APP中注册,单页面引用时需要调用getApp()方法即可

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容