企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
可修改式基础地址 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); }) } ~~~