zoukankan      html  css  js  c++  java
  • 小程序 HTTP请求封装

    • 定义http.js
    // http,promise封装
    import {
      config
    } from "../config"
    
    const tips = {
      1001: '参数错误',
      1002: 'json格式不正确',
      1003: '找不到资源'
    }
    
    class Http {
      constructor() {
        this.baseUrl = config.api_url
      }
      request({
        url,
        data = {},
        method = 'GET'
      }) {
        return new Promise((resolve, reject) => {
          wx.request({
            url: this.baseUrl + url,
            data,
            method,
            header: {
              'content-type': 'application/json',
              'appkey': config.appkey
            },
            success: res => {
              const code = res.statusCode.toString()
              if (code.startsWith('2')) {
                resolve(res.data);
              } else {
                reject()
                const error_code = res.data.error_code
                this._show_error(error_code)
              }
            },
            fail: err => {
              reject()
              this._show_error(1)
            }
          });
        })
      }
      _show_error(error_code) {
        if (!error_code) {
          error_code = 1
        }
        const tip = tips[error_code]
        wx.showToast({
          title: tip ? tip : tips[1],
          icon: 'none',
          duration: 2000
        })
      }
    
    }
    
    
    export {
      Http
    }
    
  • 相关阅读:
    最大流EK算法/DINIC算法学习
    hdu-3065-AC自动机
    51nod-1636-dp
    nyoj-1316-二分
    HDU-4510-日期
    HDU-2896-AC自动机
    51nod-1385-贪心-构造
    SpringMVC实现Restful风格的WebService
    SpringMVC使用中遇到的问题总结
    Boostrap(3)
  • 原文地址:https://www.cnblogs.com/KevinTseng/p/12858720.html
Copyright © 2011-2022 走看看