• 小程序_请求封装network


    在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     })
  • 相关阅读:
    hdu 4183(网络流)
    hdu 1565&hdu 1569(网络流--最小点权值覆盖)
    hdu 1532(最大流)
    HDU 2141 Can you find it?
    HDU 1096 A+B for Input-Output Practice (VIII)
    HDU 1095 A+B for Input-Output Practice (VII)
    HDU 1094 A+B for Input-Output Practice (VI)
    HDU 1093 A+B for Input-Output Practice (V)
    HDU 1092 A+B for Input-Output Practice (IV)
    HDU 1091 A+B for Input-Output Practice (III)
  • 原文地址:https://www.cnblogs.com/wush-1215/p/9829443.html
Copyright © 2020-2023  润新知