zhanghua
2025-06-13 21c9c72db79cd1cc450d7dfb668cbab43befa991
src/utils/graphql.js
@@ -3,33 +3,56 @@
// const apolloClient = new ApolloClient({
//   uri: 'http://127.0.0.1:7001/graphql'
// })
import {
  MessageBox
} from 'element-ui'
// export default apolloClient;
// 定义不同请求地址
const EFORMURI = "http://192.168.3.88:18080/dream_test/graphql";
const IOTURI = "https://www.9village.cn/dream_test" + "/graphql";
const EFORMURI = "/dream/graphql";
const IOTURI = "/dream/graphql";
// import ApolloClient from 'apollo-boost' //引入apollo-boost插件
import { ApolloClient } from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloLink } from "apollo-link";
import {
  ApolloClient
} from "apollo-client";
import {
  createHttpLink
} from "apollo-link-http";
import {
  InMemoryCache
} from "apollo-cache-inmemory";
import {
  ApolloLink
} from "apollo-link";
const httpLink = createHttpLink({
  uri: EFORMURI, //配置api调用连接
});
const middlewareLink = new ApolloLink((operation, forward) => {
  operation.setContext({
    headers: {
      Authorization: 'eyJhbGciOiJIUzI1NiJ9.eyJleHBUaW1lIjoiMjAyNC8wNy8yNSAxMTozOSIsInVzZXJJZCI6MSwianRpIjoiZGVkZWRlMWQtYTU4MC00NDkxLWI0YzAtMjA5ODRjYTk3NmE3IiwiaWF0IjoxNzIxODcxNTYyLCJzdWIiOiJ5Y2wiLCJleHAiOjE3MjE4Nzg3NjJ9.gvEk8RHvJD7QQjr83XVwPe9msqEeVITXg3hIJRaNubI',
      staffId: "1680",
      playerId: '3350',
    },
  }); //request拦截器
  const user = JSON.parse(localStorage.getItem("user"))
  if (user) {
    operation.setContext({
      headers: {
        Authorization: user.jwtToken,
        // Authorization:'eyJhbGciOiJIUzI1NiJ9.eyJleHBUaW1lIjoiMjAyNC8wNy8zMSAxODoxMSIsInVzZXJJZCI6NDQsImp0aSI6ImVlMDM3NmFjLWQ5OTAtNDRkZS05YTVmLTUxYjhjMjc0YWNiOSIsImlhdCI6MTcyMjQxMzUwMSwic3ViIjoieWNsIiwiZXhwIjoxNzIyNDIwNzAxfQ.bSd0qag44dwXeJQpvryYJXEtrQ03-8MmHQWhgILGXO4',
        staffId: JSON.parse(localStorage.getItem("selectStaff")).id,
        playerId: user.players.length > 0 ?
          user.players[0].id : 0,
      },
    }); //request拦截器
  return forward(operation).map((response) => {
    return response;
  }); //response拦截器,但是此处并不能对错误响应进行拦截
    return forward(operation).map((response) => {
      return response;
    }); //response拦截器,但是此处并不能对错误响应进行拦截
  } else {
    MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
      confirmButtonText: '重新登录',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      location.href = '/#/login';
    })
  }
});
const authLink = middlewareLink.concat(httpLink);
@@ -49,7 +72,7 @@
  link: authLink,
  cache: new InMemoryCache(),
  connectToDevTools: true,
  defaultOptions: defaultOptions,
  // defaultOptions: defaultOptions,
});
// const apolloClient = new ApolloClient({
//   uri: 'https://countries.trevorblades.com/',