小程序_請求封裝network
阿新 • • 發佈:2018-10-22
showmodal ext code ade js文件 title json req mob
在utils目錄下創建network.js文件封裝請求
封裝的network.js:
1 //模塊一,全局變量 2 let urlList = { 3 host: ‘http://47.106.25.53/‘, 4 loginHost: "http://47.106.25.53/", 5 webModelUrl: ‘ipp-web/‘, //登陸模塊 6 mobileModelUrl: ‘ipp-mobile/‘, //業務模塊 7 authModelUrl: ‘ipp-auth-web/‘, //上傳圖片 8 }, 9 requestHandler = { 10 title: ‘‘,11 url: ‘‘, 12 success: function (res) { 13 14 }, 15 fail: function () { 16 17 } 18 }, 19 domain = urlList.host + urlList.mobileModelUrl; 20 //--------end------------ 21 22 //模塊二,處理請求 23 function request(method, requestHandler) { 24 const token = wx.getStorageSync(‘token‘);25 if (!requestHandler.title) requestHandler.title = "加載中"; 26 wx.showLoading({ 27 title: requestHandler.title, 28 }) 29 setTimeout(function () { 30 wx.hideLoading() 31 }, 10000); 32 wx.request({ 33 url: domain + requestHandler.url, 34 data: requestHandler.data, 35 method: method,36 header: { 37 "X-Requested-With": "XMLHttpRequest", 38 ‘Content-Type‘: ‘application/json‘, 39 ‘token‘: token 40 }, 41 dataType: ‘json‘, 42 success: function (res) { 43 if (res.data.msgCode == ‘F0006‘ || res.data.msgCode == ‘F0007‘) { 44 /*wx.showModal({ 45 title: ‘溫馨提示‘, 46 content: ‘登錄已失效,請重新登錄!‘, 47 cancelText: ‘取消‘, 48 confirmText: ‘確定‘, 49 success: function (res) { 50 wx.reLaunch({ 51 url: ‘../logs/logs‘, 52 }) 53 } 54 })*/ 55 } 56 wx.hideLoading() 57 requestHandler.success(res) 58 }, 59 fail: function () { 60 wx.hideLoading() 61 requestHandler.fail() 62 } 63 }) 64 } 65 //---------end----------- 66 67 68 //模塊三,請求方法 69 //GET請求 70 function GET(requestHandler) { 71 request(‘GET‘, requestHandler) 72 }; 73 //POST請求 74 function POST(requestHandler) { 75 request(‘POST‘, requestHandler) 76 }; 77 //--------end----------- 78 79 80 81 //模塊四,輸出方法 82 module.exports = { 83 GET: GET, 84 POST: POST, 85 urlList: urlList 86 }; 87 //-----end----------
在頁面上使用require引入輸出的封裝對象:
1 const network = require(‘../../utils/network.js‘);
使用方法:
1 network.POST({ 2 url: ‘asnorder/getPackingUnitInfo.shtml‘, 3 data: {}, 4 success: function (res) { 5 6 }, 7 })
小程序_請求封裝network