支付宝开发工具开发钉钉小程序--封装axios

文章介绍了使用Node.js实现的异步HTTP请求函数,包括get和post方法,以及如何处理Token认证。重点展示了如何在axios封装中结合Promise和内容类型管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


let tokenAbnormal = false;
const http = {
  get: function (url, data) {
    return new Promise((success, error) => {
      dd.httpRequest({
        url: baseURL + url,
        headers: {
          "Content-Type": "application/json",
          "Authorization":"Bearer "+ dd.getStorageSync({ key: 'token' }).data
        },
        method: 'GET',
        data: data,
        dataType: 'json',
        success: function (res) {
          if (res.data.code === 200 || res.data.code === 0) {
            success(res.data)
          } else {
            my.showToast({
              type: 'fail',
              content: res.data.message,
              duration: 3000
            })
            error()
          }
        },
        fail: function (res) {
          error(res)
        },
        complete: function (res) {

        }
      });
    })
  },
  post: function (url, data) {
    if(url == "/dingLogin"){
      var Authorization =''
    }else{
      var Authorization ="Bearer "+dd.getStorageSync({ key: 'token' }).data
    }
    return new Promise((success, error) => {
      dd.httpRequest({
        url: baseURL + url,
        headers: {
          "Content-Type": "application/json",
          "Authorization":Authorization
        },
        method: 'POST',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function (res) {
          if (res.data.code === 200 || res.data.code === 0 || res.data.code === 300) {
            success(res.data)
          } else {
            my.showToast({
              type: 'fail',
              content: res.data.message,
              duration: 3000
            })
             error()
          }
        },
        fail: function (res) {
          error(res)
        },
        complete: function (res) {

        }
      });
    })
  },
  axios: function (url, data) {
    return new Promise((success, error) => {
      dd.httpRequest({
        url: baseURL + url,
        headers: {
          "Content-Type": "application/json",
          "Authorization": "Bearer "+dd.getStorageSync({ key: 'token' }).data
        },
        method: 'GET',
        data: data,
        dataType: 'json',
        success: function (res) {
          success(res.data)
        },
        fail: function (res) {
          error(res)
        },
        complete: function (res) {

        }
      });
    })
  },
}
export default http;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值