shihuojian commited on
Commit
f079248
·
1 Parent(s): 7948322

修改注入请求判断

Browse files
ndcc/server/lib/basic.js CHANGED
@@ -5,12 +5,13 @@ const Cookies = require('cookies');
5
 
6
  module.exports = class{
7
 
8
- constructor(request,server,inject) {
9
- this.inject = inject;
10
  this.server = server;
11
  this.request = request;
12
  this.plugins = server.settings.app.plugins;
13
- let {cache,info,headers,url,mongo,userType,auth,route,query,payload,params,method,socket} = inject ? inject.request : request;
 
14
  const {referrer,host,hostname,received,id} = info;
15
  const {req,res} = request.raw;
16
  const xFF = headers['x-forwarded-for'];
@@ -22,7 +23,7 @@ module.exports = class{
22
 
23
  this.ctx = {
24
  plugins:route.settings.plugins || {}, //替换为注入插件
25
- db:(collection,options={})=>mongodb(inject ? inject.request : request,collection,options),
26
  user:!_.isEmpty(user) ? {_id:user._id,name:user.username,source:user.source} : user,
27
  cache,
28
  id:ToBase64(id),
@@ -134,7 +135,7 @@ module.exports = class{
134
  payload:this.getPayload()
135
  }
136
  };
137
- return this.inject ? this.raw : await this.loadPlugins(this.raw);
138
  }catch (e){
139
  Error(e,'init');
140
  }
 
5
 
6
  module.exports = class{
7
 
8
+ constructor(request,server) {
9
+ this.isInjected = request.isInjected; // 是否是server.inject创建的
10
  this.server = server;
11
  this.request = request;
12
  this.plugins = server.settings.app.plugins;
13
+ const getRequest = this.isInjected ? request.plugins.request : request; //获取注入请求
14
+ let {cache,info,headers,url,mongo,userType,auth,route,query,payload,params,method,socket} = getRequest;
15
  const {referrer,host,hostname,received,id} = info;
16
  const {req,res} = request.raw;
17
  const xFF = headers['x-forwarded-for'];
 
23
 
24
  this.ctx = {
25
  plugins:route.settings.plugins || {}, //替换为注入插件
26
+ db:(collection,options={})=>mongodb(getRequest,collection,options),
27
  user:!_.isEmpty(user) ? {_id:user._id,name:user.username,source:user.source} : user,
28
  cache,
29
  id:ToBase64(id),
 
135
  payload:this.getPayload()
136
  }
137
  };
138
+ return this.isInjected ? this.raw : await this.loadPlugins(this.raw);
139
  }catch (e){
140
  Error(e,'init');
141
  }
ndcc/server/plugins/route.js CHANGED
@@ -34,7 +34,7 @@ exports.plugin = {
34
  return async (request,h)=>{
35
  try {
36
  request.raw.req.url = `/api/plugins/${_id}`;
37
- const {req,res} = await new Basic(request,server,request.plugins).init();
38
  await handler(req,res);
39
  return h.close;
40
  } catch (e) {
 
34
  return async (request,h)=>{
35
  try {
36
  request.raw.req.url = `/api/plugins/${_id}`;
37
+ const {req,res} = await new Basic(request,server).init();
38
  await handler(req,res);
39
  return h.close;
40
  } catch (e) {
ndcc/server/plugins/socket.js CHANGED
@@ -94,15 +94,21 @@ exports.plugin = {
94
  socket.emit(callback_id,output)
95
  }
96
  });
97
- // socket.on('post',async (payload)=>{
98
- // const result = await server.inject({url:"/socket/auth",method:"POST",
99
- // payload,
100
- // plugins:{
101
- // request
102
- // }
103
- // });
104
- // // console.log(result,'====')
105
- // })
 
 
 
 
 
 
106
  }
107
  });
108
  item.settings.plugins['socket'].io = io
 
94
  socket.emit(callback_id,output)
95
  }
96
  });
97
+ socket.on('post',async (options)=>{
98
+ const {data,url,callback_id} = options;
99
+
100
+ //需要处理判断注入请求不需要走权限~~,然后request获取注入请求的request,接娃回来写
101
+ const res = await server.inject({url,method:"POST",
102
+ payload:data,
103
+ headers:socket.request.headers,
104
+ plugins:{request}
105
+ });
106
+ if(res.statusMessage !== "OK"){
107
+ socket.emit(callback_id,res.result)
108
+ }else{
109
+ console.log(res,'======')
110
+ }
111
+ })
112
  }
113
  });
114
  item.settings.plugins['socket'].io = io
src/pages/api/plugins/Zi34RUP5T5YuGLrJ.js CHANGED
@@ -3,7 +3,6 @@
3
  export default async (req)=>{
4
  const {send,reply,error,isGet,db,cookie,event,socket} = await req.ctx();
5
  try{
6
- error('fsdf')
7
  send(await db('test').findOne())
8
  }catch (e){
9
  error(e)
 
3
  export default async (req)=>{
4
  const {send,reply,error,isGet,db,cookie,event,socket} = await req.ctx();
5
  try{
 
6
  send(await db('test').findOne())
7
  }catch (e){
8
  error(e)
src/pages/api/routes/ZizW9ckSRyqCIpyG.js CHANGED
@@ -1,8 +1,8 @@
1
 
2
  export default async (req)=>{
3
- const {send,error} = await req.ctx();
4
  try{
5
- send('Hello World!dsdwsdfsddfff11111');
6
  }catch (e){
7
  error(e)
8
  }
 
1
 
2
  export default async (req)=>{
3
+ const {send,error,db} = await req.ctx();
4
  try{
5
+ send(await db('test').findOne());
6
  }catch (e){
7
  error(e)
8
  }
src/pages/web/ZhkJRJZe81m1AB-0/index.js CHANGED
@@ -6,14 +6,19 @@ export default function App(props) {
6
  const start = async () => {
7
 
8
  const socket = io.connect('http://localhost:3000', {
9
- path:"/api/ZhIWjVHdVLru7eFn",
10
  transports: ['websocket']
11
  });
12
- socket.on("testSocket", (value) => {
13
  console.log(value)
14
  });
15
-
16
- // socket.emit('post',{name:'testPost'})
 
 
 
 
 
17
 
18
 
19
 
 
6
  const start = async () => {
7
 
8
  const socket = io.connect('http://localhost:3000', {
9
+ path:"/ZhkJRJZe81m1AB-0",
10
  transports: ['websocket']
11
  });
12
+ socket.on("testPost", (value) => {
13
  console.log(value)
14
  });
15
+ socket.emit("post",{
16
+ url:"/api/ZizW9ckSRyqCIpyG",
17
+ callback_id:'testPost',
18
+ data:{
19
+ name:'testPost'
20
+ }
21
+ });
22
 
23
 
24