Skip to content

Commit a34ef2c

Browse files
committed
完善uview的api请求封装和公共js
1 parent a2b31eb commit a34ef2c

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

uniapp_uview/common/config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const mode = 'devOnline'; //devLocal:本地测试、devOnline:线上测试、production:生产环境
2+
let ROOTPATH = ''; //域名
3+
switch (mode) {
4+
case 'devLocal':
5+
ROOTPATH = "http://192.168.2.5:61111"
6+
break;
7+
case 'devOnline':
8+
ROOTPATH = "https://xxxxx.cn"
9+
break;
10+
case 'production':
11+
ROOTPATH = "https://xxxxx.cn"
12+
break;
13+
default:
14+
throw new Error('未配置环境');
15+
console.log(`未配置环境`);
16+
}
17+
export { ROOTPATH }

uniapp_uview/common/const.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
* 公用变量
3+
*
4+
*/

uniapp_uview/common/http.api.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//api集合
2+
let apiApp = {
3+
login: '/api/login',
4+
getInfo: '/777/777/index',
5+
}
6+
let apiHome = {
7+
hotSearchUrl: '/777/77/hot_search'
8+
}
9+
10+
// 此处第二个参数vm,就是我们在页面使用的this,你可以通过vm获取vuex等操作
11+
const install = (Vue, vm) => {
12+
13+
// 此处没有使用传入的params参数
14+
let getSearch = (params = {}) => vm.$u.get(apiHome.hotSearchUrl, params);
15+
// 此处使用了传入的params参数,一切自定义即可
16+
17+
let getInfo = (params = {}) => vm.$u.post(apiApp.getInfo, params);
18+
19+
// 将各个定义的接口名称,统一放进对象挂载到vm.$u.api(因为vm就是this,也即this.$u.api)下
20+
vm.$u.api = {getSearch, getInfo};
21+
}
22+
23+
export default {
24+
install
25+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { ROOTPATH as baseUrl } from "@/common/config.js"
2+
3+
// 这里的Vue为Vue对象(非创建出来的实例),vm为main.js中“Vue.use(httpInterceptor, app)”这一句的第二个参数,
4+
// 为一个Vue的实例,也即每个页面的"this"
5+
// 如果需要了解这个install方法是什么,请移步:https://uviewui.com/components/vueUse.html
6+
const install = (Vue, vm) => {
7+
// 此为自定义配置参数,具体参数见上方说明
8+
Vue.prototype.$u.http.setConfig({
9+
baseUrl,
10+
showLoading: true, // 是否显示请求中的loading
11+
loadingMask: true, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
12+
loadingText: '加载中', // 请求loading中的文字提示
13+
loadingTime: 800,
14+
originalData: false, // 是否在拦截器中返回服务端的原始数据
15+
// 设置自定义头部content-type
16+
header: {
17+
'content-type': 'application/json;charset=UTF-8'
18+
}
19+
});
20+
21+
// 请求拦截部分,如配置,每次请求前都会执行
22+
Vue.prototype.$u.http.interceptor.request = (config) => {
23+
console.log('config', config)
24+
// 引用token
25+
// 方式一,存放在vuex的token,假设使用了uView封装的vuex方式
26+
// 见:https://uviewui.com/components/globalVariable.html
27+
// config.header.token = vm.token;
28+
29+
// 方式二,如果没有使用uView封装的vuex方法,那么需要使用$store.state获取
30+
// config.header.token = vm.$store.state.token;
31+
32+
// 方式三,如果token放在了globalData,通过getApp().globalData获取
33+
// config.header.token = getApp().globalData.username;
34+
35+
// 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的
36+
// 所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值
37+
// const token = uni.getStorageSync('token');
38+
// config.header.token = token;
39+
config.header.Token = 'xxxxxx';
40+
41+
// 可以对某个url进行特别处理,此url参数为this.$u.get(url)中的url值
42+
if (config.url == '/user/login') config.header.noToken = true;
43+
// 最后需要将config进行return
44+
return config;
45+
// 如果return一个false值,则会取消本次请求
46+
// if(config.url == '/user/rest') return false; // 取消某次请求
47+
}
48+
49+
// 响应拦截,如配置,每次请求结束都会执行本方法
50+
Vue.prototype.$u.http.interceptor.response = (res) => {
51+
if (res.code == 200) {
52+
// res为服务端返回值,可能有code,result等字段
53+
// 这里对res.result进行返回,将会在this.$u.post(url).then(res => {})的then回调中的res的到
54+
// 如果配置了originalData为true,请留意这里的返回值
55+
return res.result;
56+
} else if (res.code == 201) {
57+
// 假设201为token失效,这里跳转登录
58+
vm.$u.toast('验证失败,请重新登录');
59+
setTimeout(() => {
60+
// 此为uView的方法,详见路由相关文档
61+
vm.$u.route('/pages/user/login')
62+
}, 1500)
63+
return false;
64+
} else {
65+
// 如果返回false,则会调用Promise的reject回调,
66+
// 并将进入this.$u.post(url).then().catch(res=>{})的catch回调中,res为服务端的返回值
67+
return false;
68+
}
69+
}
70+
}
71+
72+
export default {
73+
install
74+
}

uniapp_uview/common/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
* 公共方法
3+
*
4+
*/
5+

uniapp_uview/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ const app = new Vue({
1515
store,
1616
...App
1717
})
18+
19+
// http拦截器,此为需要加入的内容
20+
import httpInterceptor from '@/common/http.interceptor.js'
21+
// 这里需要写在最后,是为了等Vue创建对象完成,引入"app"对象(也即页面的"this"实例)
22+
Vue.use(httpInterceptor, app)
23+
24+
// http接口API集中管理引入部分
25+
import httpApi from '@/common/http.api.js'
26+
Vue.use(httpApi, app)
27+
1828
app.$mount()

uniapp_uview/pages/index/index.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
onLoad() {
2222
this.$u.vuex('vuex_user.name', '诗圣');
2323
this.$u.toast('Hello uView!');
24+
// 调用getSearch接口
25+
this.$u.api.getSearch({
26+
id: 777
27+
}).then(res => {
28+
console.log(res);
29+
})
2430
},
2531
methods: {
2632

0 commit comments

Comments
 (0)