wx.js 1.13 KB
import { MAP_OPTIONS } from '../helper/constants'
export default async (RequestMethods, method, params = {}) => {
  wx.showNavigationBarLoading()
  try {
    const res = await new Promise((resolve, reject) => {
      wx.request({
        url: `${MAP_OPTIONS.domain}${method}`,
        data:params,
        method: RequestMethods,
        header:
          RequestMethods === 'POST'
            ? {
                'content-type': 'application/json'
              }
            : {},
        dataType: 'json',
        responseType: 'text',
        success: resolve,
        fail: reject
      })
    })
    const { statusCode, data, errMsg } = res
    if (statusCode == '200') {
      if (data.status == '1') {
        return data
      } else {
        wx.showToast({
          title: data.info,
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: errMsg,
        icon: 'error'
      })
    }
  } catch (error) {
    wx.hideLoading()
    console.error(`接口异常:`, error)
    wx.showToast({
      title: '系统异常',
      icon: 'error'
    })
    throw error
  } finally {
    wx.hideNavigationBarLoading()
  }
}