🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
可修改式基础地址 config.js ~~~ var config={ base_api_url:"http://douban.uieee.com/v2/" } export { config } ~~~ HTTP封装 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, method: params.method, data: params.data, header: { "Content-Type": "json" }, success: function (res) { var statusCode = res.statusCode.toString(); if (statusCode.startsWith("2")) { params.success(res.data); } else { wx.showToast({ title: "网络错误", icon: "none" }); } } }) } } export { HTTP } ~~~ 继承类传递url继续封装 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 } ~~~ 页面调用封装的HTTP和MovieModel(两者二选一) ~~~ import {HTTP} from "../../utils/http"; const httpTop250 = new HTTP(); import {MovieModel} from "../../models/movie"; const movie = new MovieModel(); onLoad: function (options) { // httpTop250.request({ // url: "top250", // success: res=>{ // console.log(res); // } // }) movie.getTop250(res=>{ console.log(res); }) } ~~~