taro vue 请求封装

import Taro from '@tarojs/taro'
import store from '../store/index'
// 1.错误提示
// 2.接口请求的加载中
// 3.数据请求成功 success===0000
// 4.错误信息的解析  状态码

export async function request (params) {
  const {url,data,method,header,isLoading=true,isShowError=true,timeout=60000,...otherData} = params
  if(isLoading){
    Taro.showLoading({
      title: ''
    })  
  }
  return await new Promise((resolve)=>{
    return Taro.request({
      data:data,
      url:url,
      method:method,
      timeout:timeout,
      header: {
        'content-type': 'application/json;charset=UTF-8,text/plain,*/*',
        'deerma_token':store.state.token || undefined,
        ...header
      },
      ...otherData,
      success: function (res) {
        if(isLoading){
          Taro.hideLoading();
        }
        if (res.data.code==='0000') {
            resolve({success:true,data:res.data.data}); 
        } else {
            if(isShowError){
              showErrorToast(res.data.msg)
            }
            resolve({data:res,success:false})
        }
      },
      error(e) {
        if(isLoading){
          Taro.hideLoading();
        }
        if(isShowError){
          showErrorToast(e)
          }
        // eslint-disable-next-line no-undef
        resolve({data:e,success:false})
      }
    })
  })
}


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

 

async toLogin(){
      if(this.clickIt) return
      const {success,data}=await loginIdm({loginAccount: this.accountVal ,loginPass: this.passworldVal})
      if(success){
        // 获取用户信息
        this.$store.dispatch('getCurrentUser',data.token)
        router.navigateTo({url:'/pages/index/index'})
      }
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值