封装接口请求(taro h5)

import Taro from '@tarojs/taro'

// 测试
let ApiUrl = 'http://192.168.1.8:8084/api/v2/'
let imgUrl = 'http://192.168.1.8:8084'
// 正式
// let ApiUrl = ''
// let imgUrl = ''

/**
 * 封装请求request
 */

function requestJson(url, data) {
  Taro.showLoading({
    title: '加载中',
    mask: true
  })
  return new Promise(function (resolve, reject) {
    Taro.request({
      url: url,
      method: "POST",
      data: JSON.stringify(data),
      header: {
        'Content-Type': 'application/json; charset=utf-8',
      },
      success: function (res) {
        Taro.hideLoading();
        // console.log('请求成功,url:', url);
        // console.log('请求参数,data:', data);
        if (res.statusCode === 200) {
          // 1090 未登录 1091未开通 1099其他
          if (res.data.code === 1090) {
            Taro.navigateTo({
              url: '../login/index'
            })
          } else {
            resolve(res.data);
          }
        } else {
          reject(res.errMsg);
        }
      },
      fail: function (err) {
        Taro.hideLoading();
        reject(err)
        showErrorToast(err)
        // console.log('请求失败,url', url);
        // console.log('请求参数,data:', data);
      }
    })
  });
}

function showErrorToast(msg) {
  Taro.showToast({
    title: msg,
    icon: 'none'
    // image: '/static/images/icon_error.png'
  })
}

const util = {
  requestJson,
  showErrorToast,
  ApiUrl,
  imgUrl
}

export default util

使用

import Taro from '@tarojs/taro'
import util from './util'

const Model = 'member/'
const TestApiUrl = util.ApiUrl
const apiMember = {
  // 会员登录
  Login: (param) => util.requestJson(TestApiUrl + Model + 'Login', param),
  // 获取用户信息
  Function2: (param) => util.requestJson(TestApiUrl + Model + 'Function2', param),
}
export default apiMember

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值