全栈的自我修养 ———— uniapp中封装api请求

api

js 复制代码
import request from '@/tools/request'
export function login(loginMessage) {
    return request({
        url: '/login',
        data: loginMessage,
        loading: true
    })
}

request

js 复制代码
// request
const urlArr = {
    'DEV': 'http://localhost:6060', // 开发
    'PRO': 'https://域名:6060', // 生产
}

let BASEURL = urlArr['DEV'] // 调整当前环境

export default item => {
    if (item.loading) {
        uni.showLoading({
            title: "加载中",
            mask: true
        });
    }
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASEURL + item.url,
            method: item.method ? item.method : 'post',
            data: item.data,
            headers: item.headers ? item.headers : { "Content-Type": "application/json" },
            timeout: 20000,
            success(response) {
                resolve(response.data);
            },
            fail(err) {
                uni.showToast({
                    icon: "none",
                    title: '服务响应失败'
                });
                reject(err);
            },
            complete() {
                uni.hideLoading();
            }
        });
    })
};

使用

js 复制代码
import { login,sendCaptcha } from '@/api/login.js'
const res = await login(params)
相关推荐
脾气有点小暴2 分钟前
uniapp通用递进式步骤组件
前端·javascript·vue.js·uni-app·uniapp
问道飞鱼3 分钟前
【前端知识】从前端请求到后端返回:Gzip压缩全链路配置指南
前端·状态模式·gzip·请求头
小杨累了9 分钟前
CSS Keyframes 实现 Vue 无缝无限轮播
前端
小扎仙森10 分钟前
html引导页
前端·html
蜗牛攻城狮17 分钟前
JavaScript 尾递归(Tail Recursion)详解
开发语言·javascript·ecmascript
坐吃山猪27 分钟前
Electron04-系统通知小闹钟
开发语言·javascript·ecmascript
小飞侠在吗34 分钟前
vue toRefs 与 toRef
前端·javascript·vue.js
csuzhucong37 分钟前
斜转魔方、斜转扭曲魔方
前端·c++·算法
燃烧的土豆1 小时前
100¥ 实现的React项目 Keep-Alive 缓存控件
前端·react.js·ai编程
半生过往1 小时前
前端运行PHP 快速上手 使用 PHPStudy Pro 详细搭建与使用指南
开发语言·前端·php