在小程序開發管理界面配置域名
點擊詳情查看域名信息
在utils下創建http.js
function request(options) { // 請求攔截器 // ... // 1. 加一些統一的參數,或者配置 if (!options.url.startsWith("https://") && !options.url.startsWith("http://")) { options.url = "https://showme2.myhope365.com" + options.url } // 默認的請求頭 let header = { "content-type": "application/x-www-form-urlencoded", // 加上統一的cookie "cookie": wx.getStorageSync("cookie") || "" }; if (options.header) { header = { ...header, ...options.header } } return new Promise((reslove, reject) => { // 調用接口 wx.request({ // 默認的配置 // 加載傳入的配置 ...options, header, success(res) { // 響應攔截器,所有接口獲取數據之前,都會先執行這里 // 1. 統一的錯誤處理 if (res.statusCode != 200) { wx.showToast({ title: '服務器異常,請聯系管理員', }) } reslove(res) }, fail(err) { reject(err) } }) }) } export function get(url, options = {}) { return request({ url, ...options }) } export function post(url, data, options = {}) { return request({ url, data, method: "POST", ...options }) }
使用post
引入
import { post } from "../../utils/http";
post('https://showme2.myhope365.com/api/login', { username:this.data.userName, password:this.data.passWord, rememberMe:true } ).then(res => { console.log(res); })
get
引入
import {
get
} from "../../utils/http";
get(`https://showme2.myhope365.com/api/cms/article/open/detail/${options.id}`).then(res => { console.log(res.data.data); this.setData({ content: res.data.data }) })