评论

微信小程序class封装http

微信小程序class封装http

  • config.js
var config = {
    base_api_url:"https://*****/"
}
export {config}

  • utils/http.js
import {config} from "../config";
class HTTP{
  request(params) {
    if (!params.method) {
      params.method = "GET"
    }
    wx.request({
      url: config.base_api_url + params.url,
      data: params.data,
      method:params.method,
      header: {
        'Content-Type': 'json'
      },
      success: function (res) {
        let statusCode = res.statusCode.toString();
        if(statusCode.startsWith("2")){
          params.success(res.data);
        }else{
          wx.showToast({
            title:"网络错误",
            icon:"none"
          })
        }
      },
      fail: function() {
        wx.showToast({
          title:"错误",
          icon:"none"
        })
      }
    })
  }
}
export{
  HTTP
}
  • models/movie.js
import { HTTP } from "../utils/http";
const movie = "movie/";
class MovieModel extends HTTP {
    getData(callback) {
        this.request({
            url: movie + "getData",
            success: res => {
                callback(res);
            }
        })
    }
}
export {
    MovieModel
}
  • index.js 引用
import {MovieModel} from "../../models/movie"
var movie = new MovieModel();
最后一次编辑于  2020-03-13  
点赞 2
收藏
评论

1 个评论

  • 小肥羊🍊
    小肥羊🍊
    2020-03-13

    收藏、点赞,学习

    2020-03-13
    赞同
    回复
登录 后发表内容