微信小程式——get,post請求封裝
阿新 • • 發佈:2021-01-17
專案目錄:
util.js:
var header //請求頭 // 封裝post請求 const post = (url, data, isUrl) => { header = { 'content-type': isUrl ? 'application/x-www-form-urlencoded' : 'application/json ', 'Cookie': wx.getStorageSync("cookieKey") //讀取cookie 'toekn':'123456' }; var promise = new Promise((resolve, reject) => { //網路請求 wx.request({ url: req + url, data: data, method: 'POST', header: header, success: function (res) { //伺服器返回資料 if (res.statusCode == 200) { if (res.data.code == '301') { //身份驗證過期 wx.hideLoading() wx.showModal({ showCancel: false, content: res.data.msg, success(res) { if (res.confirm) { console.log('使用者點選確定') wx.clearStorage() wx.navigateBack() } else if (res.cancel) { console.log('使用者點選取消') } } }) } else { resolve(res); } } else { //返回錯誤提示資訊 reject(res.data); } }, error: function (e) { reject('網路出錯'); } }) }); return promise; } // 封裝get請求 const get = (url, data, isUrl) => { header = { 'content-type': !isUrl ? 'application/x-www-form-urlencoded' : 'application/json ', 'Cookie': wx.getStorageSync("cookieKey") //讀取cookie 'toekn':'123456' } var promise = new Promise((resolve, reject) => { //網路請求 wx.request({ url: req + url, data: data, header: header, success: function (res) { //伺服器返回資料 if (res.statusCode == 200) { console.log(); if (res.data.code == '301') { //身份驗證過期 wx.hideLoading() wx.showModal({ showCancel: false, content: res.data.msg, success(res) { if (res.confirm) { console.log('使用者點選確定') wx.clearStorage() wx.navigateBack() } else if (res.cancel) { console.log('使用者點選取消') } } }) } else { resolve(res); } } else { //返回錯誤提示資訊 reject(res.data); } }, error: function (e) { reject('網路出錯'); } }) }); return promise; } function json2Form(json) { var str = []; for (var p in json) { str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p])); } return str.join("&"); } //圖片地址轉換 function addX(b) { var d var c = [] b.map(item => { c.push('\\"' + item + '\\"'); }) if (c[2]) { d = "[" + c[0] + ',' + c[1] + ',' + c[2] + "]" } else if (c[1]) { d = "[" + c[0] + ',' + c[1] + "]" } else if (c[0]) { d = "[" + c[0] + "]" } return d } module.exports = { post, get, reqImg, json2Form, addX //圖片地址轉換 }
使用:
//獲取應用例項 const app = getApp() var req = require('../../utils/util') Page({ data: { isLogin: false, //是否登入 openid: wx.getStorageSync('openid'),//獲取本地openid shop: '', capy: false, mony: 0, isModelOne: false, isModel: false, isUse: false, isUsek: false }, clearAll() { wx.clearStorage() //清除本地快取 }, onShow() { //get請求 if (!wx.getStorageSync('tokenQi')) req.get('/qiniu/token/get', { type: 1, version: '1.0' }, 1) .then(res => { var tokenQi = res.data.msg wx.setStorageSync('tokenQi', tokenQi) }) }, onLoad() { }, //post請求 getPhoneNumber: function (e) { var that = this let data = e.detail let iv = data.iv let encryptedData = data.encryptedData req.post('/merchant/phoneNumber', { encryptedData: encryptedData, iv: iv, openId: that.data.openid }) .then(res => { if (res.data.code == 0) { if (res.data.data == null) { wx.showToast({ title: '沒有獲取到手機號', icon: "none" }) } else { that.setData({ phoneNumber: res.data.data.phoneNumber }) let phoneNumber = res.data.data.phoneNumber that.loginM(phoneNumber) } } else { wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) } }) } })