微信小程序----通过promise封装网络请求

import loading from ‘./loading’
import message from ‘./message’
import api from ‘…/config/api’
export function request(url: string, data = {}, method?: method,showLoding:boolean=true):any{
if(showLoding){
loading.show()
}
return new Promise((resolve, reject) => {
data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
console.log(url=====>${url}, data=====>, data)
wx.request({
url,
data,
method: method === ‘GET’ ? ‘GET’ : ‘POST’,
success: (res: any) => {
// console.log(success response=====>${JSON.stringify(res.data)})
if (res.statusCode == 200) {
// 请求成功
const response = res.data
switch (parseInt(response.code)) {
case 1000:
resolve(response.result)
break;
case 7003:
wx.navigateTo({
url:’/pages/register/detail/detail’
})
break;
default:
message.error(response.result)
reject(response)
}
} else {
message.error(res.data.result)
}
},
fail(e: any) {
message.error(‘网络异常’)
console.log(fail response=====>${JSON.stringify(e)})
},
complete() {
if(showLoding)
loading.hide()
}
})
})
}

// showmodal报错后点击确认回退两个步长
export const erroBackTwoRequest = function (url: string, data = {}, method?: method) {
return new Promise((resolve, reject) => {
data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
console.log(url=====>${url}, data=====>, data)
wx.request({
url,
data,
method: method === ‘GET’ ? ‘GET’ : ‘POST’,
success: (res: any) => {
// console.log(success response=====>${JSON.stringify(res.data)})
if (res.statusCode == 200) {
// 请求成功
const response = res.data
switch (parseInt(response.code)) {
case 1000:
resolve(response.result)
break;
case 7003:
wx.navigateTo({
url:’/pages/register/detail/detail’
})
break;
default:
console.log(res.data);
console.log(res.data.result);

          wx.showModal({
            content:''+res.data.result,
            confirmText:"确认",
            showCancel:false,
            success(a){
              if(a.confirm){
                wx.navigateBack(2)
              }
            }
          })
          reject(response)
      }
    } else {
      message.error(res.data.result)
    }
  },
  fail(e: any) {
    message.error('网络异常')
    console.log(`fail response=====>${JSON.stringify(e)}`)
  },
  complete() {
    // loading.hide()
  }
})

})
}
export const sliceRequest = function (url: string, data = {}, method?: method) {
return new Promise((resolve, reject) => {
data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
console.log(url=====>${url}, data=====>, data)
wx.request({
url,
data,
method: method === ‘GET’ ? ‘GET’ : ‘POST’,
success: (res: any) => {
// console.log(success response=====>${JSON.stringify(res.data)})
if (res.statusCode == 200) {
// 请求成功
const response = res.data
switch (parseInt(response.code)) {
case 1000:
resolve(response.result)
break;
case 7003:
wx.navigateTo({
url:’/pages/register/detail/detail’
})
break;
default:
message.error(response.result)
reject(response)
}
} else {
message.error(res.data.result)
}
},
fail(e: any) {
message.error(‘网络异常’)
console.log(fail response=====>${JSON.stringify(e)})
},
complete() {
// loading.hide()
}
})
})
}
export const getNavigationHeight = () => {
const { model, windowHeight, windowWidth, statusBarHeight } = wx.getSystemInfoSync()
let titleBarHeight = 0;
if (model.indexOf(‘iPhone’) !== -1) {
titleBarHeight = 44;
} else {
titleBarHeight = 48;
}
let rpxStatus = Math.round((statusBarHeight * 750 / windowWidth))
let rpxTitle = Math.round((titleBarHeight * 750 / windowWidth))
return {
windowHeight: windowHeight,
windowWidth: windowWidth,
rpxStatus,
rpxTitle,
navigationHeight: rpxStatus + rpxTitle
}
// return wx.getMenuButtonBoundingClientRect()
}
function requestSlice(url: string, data = {}):any{
data = Object.assign( data, { sessionId: getApp().globalData.sessionId })
wx.request({
url,
data,
method: ‘POST’,
fail(e: any) {
message.error(‘网络异常’)
console.log(fail response=====>${JSON.stringify(e)})
}
})

}
wx.enterIn= function (){
//@ts-ignore
requestSlice(api.addMiniprogramData,{page:this.route,type:‘1’})
}
wx.getOut=function (){
//@ts-ignore
requestSlice(api.addMiniprogramData,{page:this.route,type:‘2’})
}

import loading from './loading'
import message from './message'
import api from '../config/api'
export function request(url: string, data = {}, method?: method,showLoding:boolean=true):any{
  if(showLoding){
    loading.show()
  }
  return new Promise((resolve, reject) => {
    data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
    console.log(`url=====>${url}`, `data=====>`, data)
    wx.request({
      url,
      data,
      method: method === 'GET' ? 'GET' : 'POST',
      success: (res: any) => {
        // console.log(`success response=====>${JSON.stringify(res.data)}`)
        if (res.statusCode == 200) {
          // 请求成功
          const response = res.data
          switch (parseInt(response.code)) {
            case 1000:
              resolve(response.result)
              break;
              case 7003:
                wx.navigateTo({
                  url:'/pages/register/detail/detail'
                })
                break;
            default:
              message.error(response.result)
              reject(response)
          }
        } else {
          message.error(res.data.result)
        }
      },
      fail(e: any) {
        message.error('网络异常')
        console.log(`fail response=====>${JSON.stringify(e)}`)
      },
      complete() {
        if(showLoding)
        loading.hide()
      }
    })
  })
}

// showmodal报错后点击确认回退两个步长
export const erroBackTwoRequest = function (url: string, data = {}, method?: method) {
  return new Promise((resolve, reject) => {
    data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
    console.log(`url=====>${url}`, `data=====>`, data)
    wx.request({
      url,
      data,
      method: method === 'GET' ? 'GET' : 'POST',
      success: (res: any) => {
        // console.log(`success response=====>${JSON.stringify(res.data)}`)
        if (res.statusCode == 200) {
          // 请求成功
          const response = res.data
          switch (parseInt(response.code)) {
            case 1000:
              resolve(response.result)
              break;
              case 7003:
                wx.navigateTo({
                  url:'/pages/register/detail/detail'
                })
                break;
            default:
              console.log(res.data);
              console.log(res.data.result);
              
              wx.showModal({
                content:''+res.data.result,
                confirmText:"确认",
                showCancel:false,
                success(a){
                  if(a.confirm){
                    wx.navigateBack(2)
                  }
                }
              })
              reject(response)
          }
        } else {
          message.error(res.data.result)
        }
      },
      fail(e: any) {
        message.error('网络异常')
        console.log(`fail response=====>${JSON.stringify(e)}`)
      },
      complete() {
        // loading.hide()
      }
    })
  })
}
export const sliceRequest = function (url: string, data = {}, method?: method) {
  return new Promise((resolve, reject) => {
    data = Object.assign({}, data, { sessionId: getApp().globalData.sessionId })
    console.log(`url=====>${url}`, `data=====>`, data)
    wx.request({
      url,
      data,
      method: method === 'GET' ? 'GET' : 'POST',
      success: (res: any) => {
        // console.log(`success response=====>${JSON.stringify(res.data)}`)
        if (res.statusCode == 200) {
          // 请求成功
          const response = res.data
          switch (parseInt(response.code)) {
            case 1000:
              resolve(response.result)
              break;
              case 7003:
                wx.navigateTo({
                  url:'/pages/register/detail/detail'
                })
                break;
            default:
              message.error(response.result)
              reject(response)
          }
        } else {
          message.error(res.data.result)
        }
      },
      fail(e: any) {
        message.error('网络异常')
        console.log(`fail response=====>${JSON.stringify(e)}`)
      },
      complete() {
        // loading.hide()
      }
    })
  })
}
export const getNavigationHeight = () => {
  const { model, windowHeight, windowWidth, statusBarHeight } = wx.getSystemInfoSync()
  let titleBarHeight = 0;
  if (model.indexOf('iPhone') !== -1) {
    titleBarHeight = 44;
  } else {
    titleBarHeight = 48;
  }
  let rpxStatus = Math.round((statusBarHeight * 750 / windowWidth))
  let rpxTitle = Math.round((titleBarHeight * 750 / windowWidth))
  return {
    windowHeight: windowHeight,
    windowWidth: windowWidth,
    rpxStatus,
    rpxTitle,
    navigationHeight: rpxStatus + rpxTitle
  }
  // return wx.getMenuButtonBoundingClientRect()
}
function requestSlice(url: string, data = {}):any{
    data = Object.assign( data, { sessionId: getApp().globalData.sessionId })
    wx.request({
      url,
      data,
      method: 'POST',
      fail(e: any) {
        message.error('网络异常')
        console.log(`fail response=====>${JSON.stringify(e)}`)
      }
    })

}
wx.enterIn= function (){
  //@ts-ignore
  requestSlice(api.addMiniprogramData,{page:this.route,type:'1'})
}
wx.getOut=function (){
  //@ts-ignore
  requestSlice(api.addMiniprogramData,{page:this.route,type:'2'})
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值