知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
微信小程序-wx.request的封裝實現(xiàn)
發(fā)表時間:2021-2-28
發(fā)布人:葵宇科技
瀏覽次數(shù):65
httpExt.js
const app = getApp()
const util = require('/util.js')
/**
* wx.request的封裝
* 使用方式: httpExt.get("admin/login", {}).then(res => {}).catch(err => {})
* @param {*} url 請求地址
* @param {*} method 請求類型
* @param {*} data 請求參數(shù)
* @param {*} options 接口配置
*/
const request = (url, method, data, options) => {
wx.showLoading({
title: '加載中',
mask: true
})
return new Promise((resolve, reject) => {
wx.request({
url: util.getUrl(url),
method: method || 'GET',
data: method === 'GET' ? options.data : JSON.stringify(options.data),
header: {
'Content-Type': 'application/json; charset=UTF-8',
// 'token': wx.getStorageSync("SSP_token")
},
success(res) {
if (res.data.success == true) {
resolve(res)
} else {
reject(res)
}
},
fail(error) {
reject(error)
},
complete: info => {
wx.hideLoading()
}
})
})
}
const get = (url, data = http://www.wxapp-union.com/{}, options = {}) => {
return request(url, 'GET', data, options)
}
const post = (url, data = http://www.wxapp-union.com/{}, options = {}) => {
return request(url, 'POST', data, options)
}
module.exports = {
get,
post
}
復制代碼
util.js
const hosts = require("/hosts.js")
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
const getUrl = (url) => {
if (url.indexOf('://') == -1) {
url = hosts.actualHost.mainHostName + url;
}
return url
}
module.exports = {
getUrl,
formatTime
}
復制代碼
hosts.js
// pro 生產(chǎn)環(huán)境 sit 測試環(huán)境 (上線前需要修改為'pro')
const hostType = 'sit'
const version = '1.0'
const host = {
/**
* 主服務域名
*/
mainHostName: new Map([
['pro', 'xxxxx'],
['sit', 'xxxxxx']
])
}
const actualHost = {
mainHostName: host.mainHostName.get(hostType)
}
export {
actualHost,
hostType,
version
}
復制代碼
使用:
const httpExt = require('../../utils/httpExt.js')
httpExt.get("xxxx", {}).then(res => {
// 接口成功操作
}).catch(err => {
console.log("error:", err);/* */
// 接口失敗操作
wx.showToast({
title: err.data.msg||"服務異常,請稍后重試!",
icon: 'error',
duration: 2000
})
});
復制代碼
相關案例查看更多
相關閱讀
- 云南網(wǎng)站建設公司排名
- 云南小程序開發(fā)課程
- 云南網(wǎng)站建設案例
- 霸屏推廣
- 開發(fā)制作小程序
- 網(wǎng)站建設特性
- 報廢車
- 云南網(wǎng)絡營銷
- 服務器
- 生成海報
- 小程序定制
- 北京小程序制作
- 麗江小程序開發(fā)
- 百度小程序開發(fā)
- 網(wǎng)絡營銷
- 買小程序被騙
- 微信小程序
- 小程序表單
- 汽車拆解管理系統(tǒng)
- 網(wǎng)站開發(fā)
- 百度推廣
- 網(wǎng)站排名優(yōu)化
- 網(wǎng)站優(yōu)化
- 云南建設廳網(wǎng)站
- web前端
- 網(wǎng)站開發(fā)公司哪家好
- 網(wǎng)站建設方案 doc
- 二叉樹
- 云南網(wǎng)站開發(fā)
- 百度小程序公司