• 微信小程序 封装请求api


    封装请求地址 https.js

     let baseUrl='https://XX.XXX.com/index.php/'; //自己得服务器地址
     export {
        baseUrl
      }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    结构目录

    结构目录

    封装 request.js

    import { baseUrl } from './https.js'
     
    module.exports = {
      request : function(url, methodType, data){
        let fullUrl = `${baseUrl}${url}`
        // let token = wx.getStorageSync('token') ? wx.getStorageSync('token')  : ''
        //(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
        wx.showLoading({ title: "数据请求中"  });
        return new Promise((resolve,reject)=>{
          wx.request({
            url: fullUrl,
            method:methodType,
            data:data,
            header: {
              'content-type': 'application/json', // 默认值
              // 'x-api-key': token,
            },
            success(res){
                resolve(res)
            //   if (res.data.status == 200) { //根据自己返回数据状态进行更改
            //     resolve(res.data)
            //     wx.hideLoading()
            //   }else{
              //手动关闭loading提示框
                wx.hideLoading()
            //     wx.showToast({
            //       title: res.data.msg,
            //       icon:'none'
            //     })
            //     reject(res.data.message)
            //   }
            },
            fail(){
              wx.showToast({
                title: '接口请求错误',
                icon:'none'
              })
              reject('接口请求错误')
            }
          })
        })
      },
      uploadFile : function(url, filePath, fname, formData){
        let fullUrl = `${baseUrl}${url}`
        // let token = wx.getStorageSync('token') ? wx.getStorageSync('token')  : ''
        //(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
        wx.showLoading({ title: "上传图片中"  });
        return new Promise((resolve,reject)=>{
            wx.uploadFile({
              filePath: filePath,
              name: fname,
              url: fullUrl,
              formData:formData,
              success(res){
                resolve(res)
            },
            fail(){
                wx.showToast({
                  title: '接口请求错误',
                  icon:'none'
                })
                reject('接口请求错误')
              }
            })
        })
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    自己的js文件构造

    import { request } from './request'
     
    module.exports = {
      // 获取采购list
      getPurchaseList: (data) => request('api/Api/list', 'POST', data),
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如何调用

    
    const api = require('../../../api/purchase'); // 头部引入
    
    
      getPurchaseList(){
        var that = this;
        let data ={
         "openId": that.data.userInfo.app_openid,
         "token": that.data.userInfo.login_token
        };
        api.getPurchaseList(data).then((res) => {
    			    if (res.data.status == 1) {
    			                    that.setData({
    			            goodsTypes: res.data
    			        })
                    } else {
                        app.myShowToast(res.data.msg);
                    }
           
        })
      },
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    _linux 进程间通信(匿名管道)
    决定迭代次数的两种效应
    前后端交互实例(javaweb05)
    python pdb调试
    uniapp插件开发
    节能与环保杂志节能与环保杂志社节能与环保编辑部2022年第6期目录
    【数据库系统概论】作业4 第四章 习题6|7 、第五章 习题6
    java计算机毕业设计人才库构建研究源码+系统+mysql数据库+lw文档
    遥居前列!华为云GaussDB再获行业权威验证
    面试问烂了的Java线程池执行流程,具体的执行细节你还会吗?
  • 原文地址:https://blog.csdn.net/Depressiom/article/details/132860692