码迷,mamicode.com
首页 > 微信 > 详细

微信小程序class封装http

时间:2019-08-24 09:54:43      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:his   index   版权   com   jpg   api   mode   with   一个   

config.js

var config = {
    base_api_url:"https://douban.uieee.com/v2/"
}
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 {
    getTop250(callback) {
        this.request({
            url: movie + "top250",
            success: res => {
                callback(res);
            }
        })
    }
    getComingsoon(callback) {
        this.request({
            url: movie + "coming_soon",
            success: res => {
                callback(res)
            }
        })
    }
}
export {
    MovieModel
}

index.js 引用

import {MovieModel} from "../../models/movie"
var movie = new MovieModel();

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。


请点赞!因为你们的赞同/鼓励是我写作的最大动力!

欢迎关注达叔小生的简书!

这是一个有质量,有态度的博客

技术图片

微信小程序class封装http

标签:his   index   版权   com   jpg   api   mode   with   一个   

原文地址:https://www.cnblogs.com/dashucoding/p/11403509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!