request请求
This commit is contained in:
parent
68c6c82588
commit
1f5f9cd6bc
69
src/utils/Request.ts
Normal file
69
src/utils/Request.ts
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
import store from "@/views/home/store/index";
|
||||||
|
import router from '@/views/home/router'
|
||||||
|
|
||||||
|
// 创建axios实例,设置配置得默认值
|
||||||
|
// const instance = axios.create({
|
||||||
|
// baseUrl:'https://market.cebg.games', // 这里写接口的http地址,
|
||||||
|
// timeout: 5000, // 设置请求超时的默认值
|
||||||
|
// })
|
||||||
|
let apiHost = process.env.NODE_ENV !== 'production' ? process.env.VUE_APP_API_HOST : document.location.host;
|
||||||
|
let scheme = document.location.protocol;
|
||||||
|
const instance = axios.create({
|
||||||
|
baseURL: scheme + "//" + apiHost + '/api/',
|
||||||
|
timeout: 5000
|
||||||
|
})
|
||||||
|
|
||||||
|
// 设置请求拦截器
|
||||||
|
instance.interceptors.request.use(
|
||||||
|
config => {
|
||||||
|
// 判断当前是否有token,有则在请求头上加上token
|
||||||
|
// console.log(store)
|
||||||
|
if (store.state.token) {
|
||||||
|
config.headers.Authorization = "Bearer " + store.state.token
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
// 请求错误进行拦截并返回错误信息
|
||||||
|
// console.log(error)
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
// 设置响应拦截
|
||||||
|
instance.interceptors.response.use(
|
||||||
|
response => {
|
||||||
|
const res = response.data
|
||||||
|
return res
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
let response = error.response;
|
||||||
|
if (response?.status === 401) {
|
||||||
|
store.commit('del_token')
|
||||||
|
router.replace('login')
|
||||||
|
} else {
|
||||||
|
console.error('response error', error.response);
|
||||||
|
}
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
export default instance
|
||||||
|
export const Request = (method,Url:string,params? : object | any) =>
|
||||||
|
instance({
|
||||||
|
url: Url,
|
||||||
|
method: method,
|
||||||
|
params: params
|
||||||
|
})
|
||||||
|
// export const Http_getNonce = (params) =>
|
||||||
|
// instance({
|
||||||
|
// url: 'http://127.0.0.1:8000/api/get-nonce',
|
||||||
|
// method: 'get',
|
||||||
|
// params: params
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// export const Http_login = (data) =>
|
||||||
|
// instance({
|
||||||
|
// url: 'http://127.0.0.1:8000/api/metamask-login',
|
||||||
|
// method: 'post',
|
||||||
|
// params: data
|
||||||
|
// })
|
@ -2,9 +2,11 @@
|
|||||||
* 前台地址
|
* 前台地址
|
||||||
*/
|
*/
|
||||||
export default class Url {
|
export default class Url {
|
||||||
|
//TODO 钱包登录
|
||||||
static readonly GET_NONCE : string = 'get-nonce'
|
static readonly GET_NONCE : string = 'get-nonce'
|
||||||
static readonly METAMASK_LOGIN : string = 'metamask-login'
|
static readonly METAMASK_LOGIN : string = 'metamask-login'
|
||||||
static readonly LOGOUT : string = "logout";
|
static readonly LOGOUT : string = "logout";
|
||||||
|
static readonly HOME : string = "home";
|
||||||
// TODO 菜单
|
// TODO 菜单
|
||||||
static readonly MENUS : string = "menu";
|
static readonly MENUS : string = "menu";
|
||||||
static readonly DELETE_MENU : string = "menu/%d";
|
static readonly DELETE_MENU : string = "menu/%d";
|
||||||
@ -13,10 +15,3 @@ export default class Url {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 后台地址
|
|
||||||
*/
|
|
||||||
export class AdminUrl {
|
|
||||||
static readonly LOGIN : string = "login"; // 登陆
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
import axios from 'axios';
|
|
||||||
import store from "@/views/home/store/index";
|
|
||||||
|
|
||||||
// 创建axios实例,设置配置得默认值
|
|
||||||
// const instance = axios.create({
|
|
||||||
// baseUrl:'https://market.cebg.games', // 这里写接口的http地址,
|
|
||||||
// timeout: 5000, // 设置请求超时的默认值
|
|
||||||
// })
|
|
||||||
const instance = axios.create({
|
|
||||||
baseURL: 'https://market.cebg.games',
|
|
||||||
timeout: 5000
|
|
||||||
})
|
|
||||||
|
|
||||||
// 设置请求拦截器
|
|
||||||
instance.interceptors.request.use(
|
|
||||||
config => {
|
|
||||||
// 判断当前是否有token,有则在请求头上加上token
|
|
||||||
if (store.getters.token) {
|
|
||||||
config.headers.Authorization = "Bearer " + store.getters.token
|
|
||||||
}
|
|
||||||
// console.log(config)
|
|
||||||
return config
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
// 请求错误进行拦截并返回错误信息
|
|
||||||
// console.log(error)
|
|
||||||
return Promise.reject(error)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
// 设置响应拦截
|
|
||||||
instance.interceptors.response.use(
|
|
||||||
response => {
|
|
||||||
const res = response.data
|
|
||||||
return res
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
return Promise.reject(error)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
export const Http_getNonce = (params) =>
|
|
||||||
instance({
|
|
||||||
url: 'http://127.0.0.1:8000/api/get-nonce',
|
|
||||||
method: 'get',
|
|
||||||
params: params
|
|
||||||
})
|
|
||||||
|
|
||||||
export const Http_login = (data) =>
|
|
||||||
instance({
|
|
||||||
url: 'http://127.0.0.1:8000/api/metamask-login',
|
|
||||||
method: 'post',
|
|
||||||
params: data
|
|
||||||
})
|
|
@ -1,5 +1,4 @@
|
|||||||
import { Component, Vue } from 'vue-property-decorator'
|
import { Component, Vue } from 'vue-property-decorator'
|
||||||
import Url, {AdminUrl} from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Component, Vue } from 'vue-property-decorator'
|
import { Component, Vue } from 'vue-property-decorator'
|
||||||
import Url from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import {ActionContext} from "vuex";
|
import {ActionContext} from "vuex";
|
||||||
import Http, { ResponseErrorInterface, ResponseInterface } from "@/utils/Http";
|
import Http, { ResponseErrorInterface, ResponseInterface } from "@/utils/Http";
|
||||||
import {AdminUrl} from "@/utils/Url";
|
|
||||||
import Types from "@/views/admin/store/types";
|
import Types from "@/views/admin/store/types";
|
||||||
import { User } from "@/interface/User";
|
import { User } from "@/interface/User";
|
||||||
|
|
||||||
@ -19,16 +18,16 @@ export default {
|
|||||||
token : undefined
|
token : undefined
|
||||||
},
|
},
|
||||||
actions : {
|
actions : {
|
||||||
Login(context : ActionContext<UserState, any>, form : any) {
|
// Login(context : ActionContext<UserState, any>, form : any) {
|
||||||
return Http.post(AdminUrl.LOGIN, form).then((res : ResponseInterface) => {
|
// return Http.post(AdminUrl.LOGIN, form).then((res : ResponseInterface) => {
|
||||||
console.log(res);
|
// console.log(res);
|
||||||
context.commit(Types.SET_USER_INFO, res.data);
|
// context.commit(Types.SET_USER_INFO, res.data);
|
||||||
context.commit(Types.SET_USER_TOKEN, res.data.access_token);
|
// context.commit(Types.SET_USER_TOKEN, res.data.access_token);
|
||||||
return Promise.resolve(res.data);
|
// return Promise.resolve(res.data);
|
||||||
}).catch((err : ResponseErrorInterface) => {
|
// }).catch((err : ResponseErrorInterface) => {
|
||||||
return Promise.reject(err);
|
// return Promise.reject(err);
|
||||||
})
|
// })
|
||||||
},
|
// },
|
||||||
},
|
},
|
||||||
mutations : {
|
mutations : {
|
||||||
[Types.SET_USER_INFO](state : UserState, user : User) {
|
[Types.SET_USER_INFO](state : UserState, user : User) {
|
||||||
|
@ -22,7 +22,3 @@ export interface ElTreeDataInterface {
|
|||||||
children : ElTreeDataInterface[]
|
children : ElTreeDataInterface[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum EVENT_TYPE {
|
|
||||||
UPDATE_BREAD_ITEM = "UPDATE_BREAD_ITEM",
|
|
||||||
UPDATE_MY_PROJECT = "UPDATE_MY_PROJECT"
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
||||||
import Url, {AdminUrl} from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Component, Mixins, Vue, Watch } from 'vue-property-decorator'
|
import { Component, Mixins, Vue, Watch } from 'vue-property-decorator'
|
||||||
import Url from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import { Component, Mixins, Vue, Watch } from 'vue-property-decorator'
|
import { Component, Mixins, Vue, Watch } from 'vue-property-decorator'
|
||||||
import Url, { AdminUrl } from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
@ -7,11 +6,14 @@ import {
|
|||||||
Mutation,
|
Mutation,
|
||||||
} from "vuex-class";
|
} from "vuex-class";
|
||||||
import Types from "@/views/admin/store/types";
|
import Types from "@/views/admin/store/types";
|
||||||
import HomeParent, { BreadItem } from "@/views/home/Common/HomeParent";
|
|
||||||
import { EVENT_TYPE, MenuInterface, ProjectInterface } from "@/views/home/Common/Types";
|
|
||||||
import { Route, RouteRecord } from "vue-router";
|
import { Route, RouteRecord } from "vue-router";
|
||||||
import { Menu } from "view-design";
|
import { Menu } from "view-design";
|
||||||
|
import HomeParent, { BreadItem } from "@/views/home/Common/HomeParent";
|
||||||
|
import { MenuInterface, } from "@/views/home/Common/Types";
|
||||||
import store from "@/views/home/store";
|
import store from "@/views/home/store";
|
||||||
|
import { Request } from "@/utils/Request";
|
||||||
|
import Url from "@/utils/Url";
|
||||||
|
import {Message} from "element-ui";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -23,26 +25,23 @@ export default class Layout extends HomeParent {
|
|||||||
public visible: boolean = false;
|
public visible: boolean = false;
|
||||||
public myMenus: MenuInterface[] = [];
|
public myMenus: MenuInterface[] = [];
|
||||||
public routes: BreadItem[] = [];
|
public routes: BreadItem[] = [];
|
||||||
public myProject: ProjectInterface[] = [];
|
|
||||||
|
|
||||||
|
|
||||||
public created() {
|
public created() {
|
||||||
this._getMyMenu()
|
this._getMyMenu()
|
||||||
// this._getMyProject()
|
|
||||||
// this.$bus.$on(EVENT_TYPE.UPDATE_BREAD_ITEM, this._updateRoutes.bind(this));
|
|
||||||
// this.$bus.$on(EVENT_TYPE.UPDATE_MY_PROJECT, this._getMyProject.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Watch("$route", {immediate: true, deep: true})
|
// @Watch("$route", {immediate: true, deep: true})
|
||||||
private _watchRoute(route: Route): void {
|
// private _watchRoute(route: Route): void {
|
||||||
this.routes.length = 0;
|
// this.routes.length = 0;
|
||||||
route.matched.forEach((item: RouteRecord) => {
|
// route.matched.forEach((item: RouteRecord) => {
|
||||||
let ret: BreadItem = this.$router.resolve({name: item.name, params: route.params});
|
// let ret: BreadItem = this.$router.resolve({name: item.name, params: route.params});
|
||||||
ret.name = item.meta.title;
|
// ret.name = item.meta.title;
|
||||||
this.routes.push(ret);
|
// this.routes.push(ret);
|
||||||
// console.log(ret);
|
// // console.log(ret);
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
public get isLogin() {
|
public get isLogin() {
|
||||||
@ -52,13 +51,26 @@ export default class Layout extends HomeParent {
|
|||||||
public onDropdownEvent(name: string): void {
|
public onDropdownEvent(name: string): void {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "logout" : this.logout();break;
|
case "logout" : this.logout();break;
|
||||||
// case "updateName" : this.updateName();break;
|
case "updateName" : this.updateName();break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public logout() {
|
public async logout() {
|
||||||
store.commit('del_token')
|
const res:any = await Request('get',Url.LOGOUT)
|
||||||
this.goPage("login");
|
if (res.code == 200){
|
||||||
|
store.commit('del_token')
|
||||||
|
this.goPage("login");
|
||||||
|
}else {
|
||||||
|
Message({
|
||||||
|
message: res.message,
|
||||||
|
type: 'error',
|
||||||
|
duration: 2 * 1000
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async updateName(){
|
||||||
|
const res:any = await Request('get','home')
|
||||||
|
// console.log(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,8 +4,9 @@ import HomeParent from "@/views/home/Common/HomeParent";
|
|||||||
import SvgIcon from '@/components/SvgIcon.vue'
|
import SvgIcon from '@/components/SvgIcon.vue'
|
||||||
import { Message } from 'element-ui';
|
import { Message } from 'element-ui';
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import {Http_getNonce, Http_login} from "@/utils/login-request";
|
import { Request } from "@/utils/Request";
|
||||||
import store from "@/views/home/store";
|
import store from "@/views/home/store";
|
||||||
|
import Url from "@/utils/Url";
|
||||||
|
|
||||||
|
|
||||||
interface Window {
|
interface Window {
|
||||||
@ -97,11 +98,10 @@ export default class Login extends HomeParent {
|
|||||||
tips,
|
tips,
|
||||||
net_id: chainId
|
net_id: chainId
|
||||||
}
|
}
|
||||||
const res:any = await Http_login(authData)
|
const res:any = await Request('post',Url.METAMASK_LOGIN,authData)
|
||||||
|
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
store.commit('set_token',res.data.token)
|
store.commit('set_token',res.data.token)
|
||||||
|
|
||||||
this.goPage("home");
|
this.goPage("home");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ export default class Login extends HomeParent {
|
|||||||
let nonce = store.getters.nonce
|
let nonce = store.getters.nonce
|
||||||
if (!nonce) {
|
if (!nonce) {
|
||||||
let params = {account:this.account, net_id:this.chainId}
|
let params = {account:this.account, net_id:this.chainId}
|
||||||
const res:any = await Http_getNonce(params);
|
const res:any = await Request('get',Url.GET_NONCE,params)
|
||||||
store.commit('set_nonce',res.data)
|
store.commit('set_nonce',res.data)
|
||||||
this.nonce = res.data
|
this.nonce = res.data
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="login">
|
<div class="login">
|
||||||
<div class="w-full h-3/5 flex items-center justify-center align-middle">系统</div>
|
<div class="w-full h-2/5 flex items-center justify-center align-middle">系统</div>
|
||||||
<div class="w-full flex justify-center align-middle">
|
<div class="w-full flex justify-center align-middle">
|
||||||
<div class=" rounded-xl" @click="doLogin">
|
<div class=" rounded-xl" @click="doLogin">
|
||||||
<svg-icon icon-class="login" style="width: 100px;height: 100px;cursor:pointer"/>
|
<svg-icon icon-class="login" style="width: 100px;height: 100px;cursor:pointer"/>
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import {ActionContext} from "vuex";
|
import {ActionContext} from "vuex";
|
||||||
import Http, { ResponseErrorInterface, ResponseInterface } from "@/utils/Http";
|
import Http, { ResponseErrorInterface, ResponseInterface } from "@/utils/Http";
|
||||||
import Url from "@/utils/Url";
|
|
||||||
import Types from "@/views/home/store/types";
|
import Types from "@/views/home/store/types";
|
||||||
import { UserInterface } from "@/views/home/Common/Types";
|
import { UserInterface } from "@/views/home/Common/Types";
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
||||||
import Url, {AdminUrl} from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
||||||
import Url, {AdminUrl} from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
import {Component, Mixins, Vue, Watch} from 'vue-property-decorator'
|
||||||
import Url, {AdminUrl} from "@/utils/Url";
|
|
||||||
import {
|
import {
|
||||||
State,
|
State,
|
||||||
Getter,
|
Getter,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user