日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信小程序服务器开小差了,微信小程序wx.request请求封装

發(fā)布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信小程序服务器开小差了,微信小程序wx.request请求封装 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

微信小程序 wx.request

RequestTask wx.request(Object object)發(fā)起 HTTPS 網(wǎng)絡(luò)請求。

示例代碼

wx.request({

url: 'test.php', //僅為示例,并非真實的接口地址

data: {

x: '',

y: ''

},

header: {

'content-type': 'application/json' // 默認(rèn)值

},

success (res) {

console.log(res.data)

}

})

wx.request 不進(jìn)行二次封裝確實不太好用 分享下我這邊wx.request的封裝

api.js

const app = getApp()

const request = (url, options) => {

return new Promise((resolve, reject) => {

wx.request({

url: `${app.globalData.host}${url}`,

method: options.method,

data: options.data,

header: {

'Content-Type': 'application/json; charset=UTF-8',

'Authorization': app.globalData.accessToken

},

timeout: 10000,

success(request) {

if (request.statusCode === 200) {

resolve(request.data)

} else if (request.statusCode === 401) {

wx.navigateTo({

url: '/pages/login/login'

})

reject(request.data)

} else if (request.statusCode === 500) {

wx.showModal({

title: '系統(tǒng)錯誤',

content: request.data.errmsg,

})

reject(request.data)

} else {

reject(request.data)

}

},

fail(error) {

wx.showModal({

title: '系統(tǒng)錯誤',

content: '服務(wù)器正在開小差',

})

reject(error.data)

},

complete: function (res) {}

})

})

}

// 把需要Loading的方法進(jìn)行封裝,減少不必要代碼

const requestWithLoading = (url, options) => {

return new Promise((resolve, reject) => {

wx.showLoading({

title: '加載中',

})

wx.request({

url: `${app.globalData.host}${url}`,

method: options.method,

data: options.data,

header: {

'Content-Type': 'application/json; charset=UTF-8',

'Authorization': app.globalData.accessToken

},

timeout: 10000,

success(request) {

if (request.statusCode === 200) {

resolve(request.data)

} else if (request.statusCode === 401) {

wx.navigateTo({

url: '/pages/login/login'

})

reject(request.data)

} else if (request.statusCode === 500) {

wx.showModal({

title: '系統(tǒng)錯誤',

content: request.data.errmsg,

})

reject(request.data)

} else {

reject(request.data)

}

},

fail(error) {

wx.showModal({

title: '系統(tǒng)錯誤',

content: '服務(wù)器正在開小差',

})

reject(error.data)

},

complete: function (res) {

wx.hideLoading()

}

})

})

}

const get = (url, options = {}) => {

return request(url, {

method: 'GET',

data: options

})

}

const getWithLoading = (url, options = {}) => {

return requestWithLoading(url, {

method: 'GET',

data: options

})

}

const post = (url, options) => {

return request(url, {

method: 'POST',

data: options

})

}

const postWithLoading = (url, options) => {

return requestWithLoading(url, {

method: 'POST',

data: options

})

}

const put = (url, options) => {

return request(url, {

method: 'PUT',

data: options

})

}

const putWithLoading = (url, options) => {

return requestWithLoading(url, {

method: 'PUT',

data: options

})

}

// 不能聲明DELETE(關(guān)鍵字)

const remove = (url, options) => {

return request(url, {

method: 'DELETE',

data: options

})

}

const removeWithLoading = (url, options) => {

return requestWithLoading(url, {

method: 'DELETE',

data: options

})

}

module.exports = {

get,

getWithLoading,

post,

postWithLoading,

put,

putWithLoading,

remove,

removeWithLoading

}

使用方式

const api = require('../../api/api')

Page()前引入

api.post(login, {

data: ''

}).then(res => {

if(){}

}).catch(err => {

wx.showToast({

title: err.message,

icon: 'none'

})

})

data 參數(shù)說明

最終發(fā)送給服務(wù)器的數(shù)據(jù)是 String 類型,如果傳入的 data 不是 String 類型,會被轉(zhuǎn)換成 String 。轉(zhuǎn)換規(guī)則如下:

對于 GET 方法的數(shù)據(jù),會將數(shù)據(jù)轉(zhuǎn)換成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)

對于 POST 方法且 header[‘content-type’] 為 application/json 的數(shù)據(jù),會對數(shù)據(jù)進(jìn)行 JSON 序列化

對于 POST 方法且 header[‘content-type’] 為 application/x-www-form-urlencoded 的數(shù)據(jù),會將數(shù)據(jù)轉(zhuǎn)換成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)…)

總結(jié)

以上是生活随笔為你收集整理的微信小程序服务器开小差了,微信小程序wx.request请求封装的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。