刘翔宇-旅管家 3 years ago
parent cb53ee1e4a
commit b074e234b7

@ -1,70 +1,79 @@
import { import {
ROOTPATH as baseUrl ROOTPATH as baseUrl
} from "@/common/config.js" } from "@/common/config.js"
// 这里的Vue为Vue对象(非创建出来的实例)vm为main.js中“Vue.use(httpInterceptor, app)”这一句的第二个参数, // 这里的Vue为Vue对象(非创建出来的实例)vm为main.js中“Vue.use(httpInterceptor, app)”这一句的第二个参数,
// 为一个Vue的实例也即每个页面的"this" // 为一个Vue的实例也即每个页面的"this"
// 如果需要了解这个install方法是什么请移步https://uviewui.com/components/vueUse.html // 如果需要了解这个install方法是什么请移步https://uviewui.com/components/vueUse.html
const install = (Vue, vm) => { const install = (Vue, vm) => {
// 此为自定义配置参数,具体参数见上方说明 // 此为自定义配置参数,具体参数见上方说明
Vue.prototype.$u.http.setConfig({ Vue.prototype.$u.http.setConfig({
baseUrl, baseUrl,
showLoading: true, // 是否显示请求中的loading showLoading: true, // 是否显示请求中的loading
loadingMask: true, // 展示loading的时候是否给一个透明的蒙层防止触摸穿透 loadingMask: true, // 展示loading的时候是否给一个透明的蒙层防止触摸穿透
loadingText: '加载中', // 请求loading中的文字提示 loadingText: '加载中', // 请求loading中的文字提示
loadingTime: 800, loadingTime: 800,
originalData: true, // 是否在拦截器中返回服务端的原始数据 originalData: true, // 是否在拦截器中返回服务端的原始数据
// 设置自定义头部content-type // 设置自定义头部content-type
header: { header: {
'content-type': 'application/json;charset=UTF-8' 'content-type': 'application/json;charset=UTF-8'
} }
}); });
// 请求拦截部分,如配置,每次请求前都会执行 // 请求拦截部分,如配置,每次请求前都会执行
Vue.prototype.$u.http.interceptor.request = (config) => { Vue.prototype.$u.http.interceptor.request = (config) => {
console.log('config-http', config) console.log('config-http', config)
// 引用token // 引用token
// 方式一存放在vuex的token假设使用了uView封装的vuex方式 // 方式一存放在vuex的token假设使用了uView封装的vuex方式
// 见https://uviewui.com/components/globalVariable.html // 见https://uviewui.com/components/globalVariable.html
if(vm.vuex_token){ if (vm.vuex_token) {
config.header['Authorization'] = `Bearer ${vm.vuex_token}`; config.header['Authorization'] = `Bearer ${vm.vuex_token}`;
} }
// 最后需要将config进行return // 最后需要将config进行return
return config; return config;
// 如果return一个false值则会取消本次请求 // 如果return一个false值则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求 // if(config.url == '/user/rest') return false; // 取消某次请求
} }
// 响应拦截,如配置,每次请求结束都会执行本方法 // 响应拦截,如配置,每次请求结束都会执行本方法
Vue.prototype.$u.http.interceptor.response = (res) => { Vue.prototype.$u.http.interceptor.response = (res) => {
console.log('res-http', res) console.log('res-http', res)
if (res.statusCode === 200) {
if (res.data.errcode === 40001) {
uni.showToast({ if (res.statusCode === 200) {
icon: "none", if (res.data.hasOwnProperty("errcode")) {
title: "登陆过期,请重新登录" if (res.data.errcode === 40001) {
}) uni.showToast({
icon: "none",
setTimeout(() => { title: "登陆过期,请重新登录"
uni.redirectTo({ })
url: '/pages/login/login' setTimeout(() => {
}) uni.redirectTo({
}, 1500) url: '/pages/login/login'
})
} }, 1500)
return res.data;
} else { } else {
uni.showToast({ uni.showToast({
icon: "fail", icon: "none",
title: res.statusCode title: res.data.errmsg
}) })
}
return false; } else
} return res.data;
}
} } else {
uni.showToast({
export default { icon: "fail",
install title: res.statusCode
})
return false;
}
}
}
export default {
install
} }

@ -12,7 +12,7 @@
</view> </view>
<!-- 搜索 --> <!-- 搜索 -->
<view class="search"> <view class="search" v-if="false">
<view class="input-content"> <view class="input-content">
<u-input placeholder="请输入要搜索的内容" height="40" :custom-style="inputStyle"></u-input> <u-input placeholder="请输入要搜索的内容" height="40" :custom-style="inputStyle"></u-input>
</view> </view>

@ -75,6 +75,23 @@
}, },
methods: { methods: {
login() { login() {
if(this.form.username==""){
uni.showToast({
icon: "none",
title: `请正确填写登录名`,
});
return false
}
if(this.form.password==""){
uni.showToast({
icon: "none",
title: `请正确填写密码`,
});
return false
}
this.$u.api.login(this.form).then(res => { this.$u.api.login(this.form).then(res => {
this.$u.vuex("vuex_token", res.access_token); this.$u.vuex("vuex_token", res.access_token);

Loading…
Cancel
Save