欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

微信小程序-wx.request的封裝實現(xiàn) - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設/小程序開發(fā)/軟件開發(fā)

知識

不管是網(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 }) }); 復制代碼

相關案例查看更多