网页托管网站,wordpress的教程,自己做一个app,免费推广平台大全最近在学Uniapp#xff0c;到封装请求的时候本来还想用axios#xff0c;但是看到一些教学视频有更简单的方法#xff0c;
基于uni的拦截器和Promise封装的请求函数 但是他们是用TS写的#xff0c;还没学到TS#xff0c;我就把JS写了#xff0c;最终也是请求成功 // src/…最近在学Uniapp到封装请求的时候本来还想用axios但是看到一些教学视频有更简单的方法
基于uni的拦截器和Promise封装的请求函数 但是他们是用TS写的还没学到TS我就把JS写了最终也是请求成功 // src/utils/http.jsimport { useUserStore } from ../stores/modules/user// 请求基地址
const baseURL https://pcapi-xiaotuxian-front-devtest.itheima.net// 拦截器配置
const httpInterceptor {// 拦截前触发invoke(options) {// 1. 非 http 开头需拼接地址if (!options.url.startsWith(http)) {options.url baseURL options.url}// 2. 请求超时options.timeout 10000// 3. 添加小程序端请求头标识options.header {source-client: miniapp,...options.header,}// 4. 添加 token 请求头标识const userStore useUserStore()const token userStore.getToken() ? userStore.getToken() : if (token) {options.header.Authorization token}},
}// 拦截 request 请求
uni.addInterceptor(request, httpInterceptor)
// 拦截 uploadFile 文件上传
uni.addInterceptor(uploadFile, httpInterceptor)export const http (options) {return new Promise((resolve, reject) {uni.request({...options,success(res) {if (res.statusCode 200 res.statusCode 300){resolve(res.data)} else if ( res.statusCode 401){const userStore useUserStore()userStore.removeToken()uni.navigateTo({ url: /pages/login/login })reject(res)} else {uni.showToast({icon: none,title: (res.data).msg || 请求错误,})reject(res)}},fail(err) {uni.showToast({icon: none,title: 请求失败,})reject(err)}})})
}
组件引入http方法进行请求 最后附上TS的写法感觉其实差不多只是用TS语法进行了一下泛型的判断
// src/utils/http.ts// 请求基地址
const baseURL https://pcapi-xiaotuxian-front-devtest.itheima.net// 拦截器配置
const httpInterceptor {// 拦截前触发invoke(options: UniApp.RequestOptions) {// 1. 非 http 开头需拼接地址if (!options.url.startsWith(http)) {options.url baseURL options.url}// 2. 请求超时options.timeout 10000// 3. 添加小程序端请求头标识options.header {source-client: miniapp,...options.header,}// 4. 添加 token 请求头标识const memberStore useMemberStore()const token memberStore.profile?.tokenif (token) {options.header.Authorization token}},
}// 拦截 request 请求
uni.addInterceptor(request, httpInterceptor)
// 拦截 uploadFile 文件上传
uni.addInterceptor(uploadFile, httpInterceptor)type DataT {code: stringmsg: stringresult: T
}
// 2.2 添加类型支持泛型
export const http T(options: UniApp.RequestOptions) {// 1. 返回 Promise 对象return new PromiseDataT((resolve, reject) {uni.request({...options,// 响应成功success(res) {// 状态码 2xx参考 axios 的设计if (res.statusCode 200 res.statusCode 300) {// 2.1 提取核心数据 res.dataresolve(res.data as DataT)} else if (res.statusCode 401) {// 401错误 - 清理用户信息跳转到登录页const memberStore useMemberStore()memberStore.clearProfile()uni.navigateTo({ url: /pages/login/login })reject(res)} else {// 其他错误 - 根据后端错误信息轻提示uni.showToast({icon: none,title: (res.data as DataT).msg || 请求错误,})reject(res)}},// 响应失败fail(err) {uni.showToast({icon: none,title: 网络错误换个网络试试,})reject(err)},})})
}
文章转载自: http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.twpq.cn.gov.cn.twpq.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.pcgjj.cn.gov.cn.pcgjj.cn http://www.morning.txmkx.cn.gov.cn.txmkx.cn http://www.morning.nkqnn.cn.gov.cn.nkqnn.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.qwqzk.cn.gov.cn.qwqzk.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.frllr.cn.gov.cn.frllr.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.hbnwr.cn.gov.cn.hbnwr.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.yrdt.cn.gov.cn.yrdt.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.fylqz.cn.gov.cn.fylqz.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.txnqh.cn.gov.cn.txnqh.cn http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.psqs.cn.gov.cn.psqs.cn