• 小程序 之同步请求


    一、代码

    //request通用
    const request = (url, param, method = 'get', dataType = 'json', responseType = 'text') => {
      var method = method.toLowerCase()
      return new Promise((resolve, reject) => {
        if (method == 'get') {
          var header = {
            'content-type': 'application/json'
          }
        } else if (method == 'post') {
          var header = {
            'content-type': 'application/x-www-form-urlencoded'
          }
        }else{
          reject('header is error')
        }
        wx.request({
          url: url,
          data: param,
          header: header,
          method: method,
          dataType: dataType,
          responseType: responseType,
          success(res) {
            console.log(res)
            resolve(res.data)
          },
          fail(err) {
            console.log(err)
            reject(err)
          }
        })
      })
    }
    
    // request get 请求
    const getData = (url, param) => {
      return new Promise((resolve, reject) => {
        wx.request({
          url: url,
          method: 'GET',
          data: param,
          success(res) {
            console.log(res)
            resolve(res.data)
          },
          fail(err) {
            console.log(err)
            reject(err)
          }
        })
      })
    }
    
    // request post 请求
    const postData = (url, param) => {
      return new Promise((resolve, reject) => {
        wx.request({
          url: url,
          method: 'POST',
          data: param,
          success(res) {
            console.log(res)
            resolve(res.data)
          },
          fail(err) {
            console.log(err)
            reject(err)
          }
        })
      })
    }
    
    // loading加载提示
    const showLoading = () => {
      return new Promise((resolve, reject) => {
        wx.showLoading({
          title: '加载中...',
          mask: true,
          success(res) {
            console.log('显示loading')
            resolve(res)
          },
          fail(err) {
            reject(err)
          }
        })
      })
    }
    
    // 关闭loading
    const hideLoading = () => {
      return new Promise((resolve) => {
        wx.hideLoading()
        console.log('隐藏loading')
        resolve()
      })
    }
    
    module.exports = {
      request,
      getData,
      postData,
      showLoading,
      hideLoading
    }
  • 相关阅读:
    ios 开发日记 15
    ios开发日记
    ios 开发日记 13-剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
    ios开发日记-12 sdwebImage内存泄露
    ios 开发日记 9
    ios开发日记11 对tableView三种计算动态行高方法的分析
    ios开发日记 10- CGPathAddArc和CGPathAddArcToPoint函数
    ios开发日记10
    关于appStore不显示构建版本的问题
    UITextField/UITextView限制字数
  • 原文地址:https://www.cnblogs.com/yang-2018/p/13776653.html
Copyright © 2020-2023  润新知