Browse Source

http修改

tongshangming 2 years ago
parent
commit
4d55ee16e5
1 changed files with 5 additions and 7 deletions
  1. 5 7
      utils/http.js

+ 5 - 7
utils/http.js

@@ -2,9 +2,8 @@ import { useUserStore } from '@/stores/user'
 import config from './config'
 import utils from './utils'
 
+const userStore = useUserStore()
 const request = (method, url, data, opt) => {
-	const user = useUserStore()
-
 	return new Promise((resolve, reject) => {
 		opt.showLoading && uni.showLoading({
 			title: opt.loadingTitle,
@@ -15,7 +14,7 @@ const request = (method, url, data, opt) => {
 			method,
 			url: utils.isHttp(url) ? url : config.apiBaseUrl + url,
 			header: {
-				Authorization: user.token,
+				Authorization: userStore.token,
 				...opt.header
 			},
 			data
@@ -24,8 +23,8 @@ const request = (method, url, data, opt) => {
 
 			const data = res.data
 			if (data.code === 201) {
-				user.setUserInfo({})
-				user.setToken('')
+				userStore.setUserInfo({})
+				userStore.setToken('')
 
 				// opt.isAuth && uni.navigateTo({
 				// 	url: '/pages/login/login'
@@ -62,8 +61,7 @@ methods.forEach(method => {
 		)
 
 		if (opt.isAuth) {
-			const user = useUserStore()
-			if (!user.token) {
+			if (!userStore.token) {
 				// uni.navigateTo({
 				// 	url: '/pages/login/login'
 				// })