微信小程序 promise封装

promise 封装

var app = getApp()
import {
  ENV
} from '../env'    // 引入本地项目的代理地址  BaseUrl 或 host
/**
 * @description: 
 * @param {*} params:
 *                  url:'',
 *                  method:'GET',//非必须,默认GET
 *                  data:{},
 *                  responseType:'text', //非必须,默认为text
 *                  loading:'loading提示文字',  //非必须,默认为空字符串,表示不显示loading
 * @return {*}
 */
// let status = false
// 记录并发请求数量
let count = 0
function request(params) {
  // console.log(params,'params')
  // 每次请求开始加1
  count += 1
  return new Promise((resolve, rejects) => {
    // 确保本次登录的token是有效的
    app.loginMethod().then(() => {
      // 是否显示loading
      if (params.loading) {
        wx.showLoading({
          title: params.loading,
          mask:true
        })
      }
      wx.request({
        url: ENV.host + params.url,
        method: params.method || 'GET',
        data: params.data || {},
        responseType: params.responseType || 'text',
        header: {
          'content-type': params.contentType || 'application/json', // 默认值
          'miniprogram-satoken': wx.getStorageSync('token'),
          'api-version': 'v1.0'
        },
        success(res) {
          if (res.statusCode >= 200 && res.statusCode < 300 || res.statusCode === 304) {
            if (res.data.code === 0) {
              resolve(res)
            } else if (res.data.code === 10000) { // token失效
              // if (!status) {
                // status = true
                wx.clearStorage()
                app.fetchStatus = null
              // }
              let pages = getCurrentPages(); //获取加载的页面
              let currentPage = pages[pages.length - 1]; //获取当前页面的对象
              let url = '/' + currentPage.route; //当前页面ur
              wx.reLaunch({
                url
              })
            } else {
              rejects(res)
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false,
                confirmColor: '#DF222E'
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: '错误码:' + res.statusCode,
              showCancel: false,
              confirmColor: '#DF222E'
            })
          }
        },
        fail(err) {
          wx.showModal({
            title: '提示',
            content: '网络错误,请稍后重试!',
            showCancel: false,
            confirmColor: '#DF222E'
          })
          rejects(err)
        },
        complete(res) {
          count = count - 1
          if (params.loading && count === 0) {
            // setTimeout(() => {
              wx.hideLoading()
            // }, 500)
          }
        }
      })
    })
  })
}
export default request

引入配置的服务器代理地址

 */
const CURRENT_ENV = 'test' //当前环境   dev开发环境  test测试环境  prod生产环境

const ENV_LIST = {
  'dev':{
    host: 'http://192.168.14.126:11000/', //测试环境
    baseUrl:'http://192.168.14.126:11000/assistor', // 图片地址前缀
    //....
  },
  'test':{
    host: 'https://scrmtest.qiweioa.cn:48080/api/', //测试环境
    baseUrl: 'https://scrmtest.qiweioa.cn:48080/api/assistor', // 图片地址前缀
    // corpId:'wwba49910602626af3',
    // agentId: 1000005
    //....
    //....
  },
  'prod':{
    host: '', //测试环境
    baseUrl:'' // 图片地址前缀
    //....
    //....
  }
}

const ENV = ENV_LIST[CURRENT_ENV]
export {ENV , CURRENT_ENV}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值