提交 bf60f79515525052afd2124b464463db5e61ba5d

作者 juvenile
1 个父辈 6435bbe1

代码提交

... ... @@ -7,12 +7,12 @@ const path = require("path");
7 7 const Controller = require("egg").Controller;
8 8
9 9 class AuthController extends Controller {
10   - index(req, res, next) {
  10 + index(req) {
11 11 logger.info(req);
12 12 // res.redirect("https://www.baidu.com");
13 13 }
14 14
15   - getImg(req, res, next) {
  15 + getImg(req, res) {
16 16 logger.info(req.url, "1231231231");
17 17 const parentdir = path.resolve(__dirname, "..");
18 18 const paths =
... ... @@ -41,7 +41,7 @@ class AuthController extends Controller {
41 41 // } else res.render("index", { csrfToken: "" });
42 42 }
43 43
44   - productDes(req, res, next) {
  44 + productDes(req, res) {
45 45 if (req.csrfToken)
46 46 res.render("chat/new-chat", { csrfToken: req.csrfToken() });
47 47 else res.render("chat/new-chat", { csrfToken: "" });
... ... @@ -55,7 +55,7 @@ class AuthController extends Controller {
55 55 else res.render("recharge", { recharge: params, csrfToken: "" });
56 56 }
57 57
58   - register(req, res, next) {
  58 + register(req, res) {
59 59 if (req.csrfToken)
60 60 res.render("newRegister", { csrfToken: req.csrfToken() });
61 61 else res.render("newRegister", { csrfToken: "" });
... ... @@ -73,7 +73,7 @@ class AuthController extends Controller {
73 73 else res.render("registerMobileSuccess", { csrfToken: "" });
74 74 }
75 75
76   - signIn(req, res, next) {
  76 + signIn(req, res) {
77 77 // console.log(req);
78 78 if (req.csrfToken) res.render("newLogin", { csrfToken: req.csrfToken() });
79 79 else res.render("newLogin", { csrfToken: "" });
... ... @@ -130,7 +130,7 @@ class AuthController extends Controller {
130 130
131 131 signOut() {
132 132 const { ctx, logger } = this;
133   - const { req, res, session } = ctx;
  133 + const { req, session } = ctx;
134 134 if (
135 135 session.passport &&
136 136 session.passport.user &&
... ... @@ -148,7 +148,7 @@ class AuthController extends Controller {
148 148 ctx.redirect("/");
149 149 }
150 150
151   - smsCodes(req, res, next) {
  151 + smsCodes(req, res) {
152 152 rest.postJson({
153 153 baseUrl: "url",
154 154 url: "auth/smscodes",
... ... @@ -169,7 +169,7 @@ class AuthController extends Controller {
169 169 }
170 170 }
171 171
172   - tenants(req, res, next) {
  172 + tenants(req, res) {
173 173 rest.register({
174 174 baseUrl: "url",
175 175 url: "tenants",
... ... @@ -512,101 +512,78 @@ class AuthController extends Controller {
512 512 }
513 513 }
514 514
515   - changeTenant(req, res) {
  515 + async changeTenant() {
  516 + const {ctx ,logger,config} =this
  517 + const {req, session,service} = ctx
  518 + const { restful = {} } = config;
  519 + const { tokenUrl, host,port } = restful;
  520 + const { access_token } = session.user_info;
516 521 var params = {
517 522 grant_type: "refresh_token",
518 523 scope: "global_access:tenant_admin,tenant:" + req.body.id,
519   - refresh_token: req.session.passport.user.refreshToken,
  524 + refresh_token: req.session.user_info.refresh_token,
520 525 };
521   -
522   - req.session.passport.user.token = "";
523   -
524   - rest.postJson({
525   - baseUrl: "url",
526   - url:
527   - settings.restful.url +
528   - "uaa" +
529   - settings.restful.version +
530   - "/auth/tokens",
531   - useUrl: true,
532   - params: params,
533   - callback: _cb,
534   - req: req,
535   - res: res,
536   - options: {},
537   - excludeToken: true,
538   - });
539   -
540   - function _cb(data, response) {
541   - if (response.statusCode < 300) {
542   - req.session.passport.user.token = data.access_token;
543   - req.session.passport.user.refreshToken = data.refresh_token;
544   - req.session.passport.user.user_info = {
545   - user: data.user,
546   - perms: [],
547   - tenant: data.tenant,
548   - scope: data.scope,
549   - oss: req.session.passport.user.user_info.oss,
550   - };
551   - req.session.save(function () {
552   - rest.get({
553   - baseUrl: "url",
554   - url:
555   - settings.restful.url +
556   - "uaa" +
557   - settings.restful.version +
558   - "/perms/detail",
559   - useUrl: true,
560   - params: {},
561   - callback: _scb,
562   - req: req,
563   - res: res,
564   - options: {},
565   - });
566   - });
567   - } else {
568   - res.send({
569   - code: response.statusCode,
570   - message: data.message,
571   - initialize_done: "n",
572   - });
573   - }
574   - }
575   -
576   - function _scb(data1, response) {
577   - if (response.statusCode < 300 && data1.items) {
578   - req.session.passport.user.user_info.perms = data1.items;
579   - req.session.save(function () {
580   - rest.get({
581   - baseUrl: "url",
582   - url:
583   - settings.restful.url +
584   - "filemeta" +
585   - settings.restful.version +
586   - "/config",
587   - useUrl: true,
588   - params: {},
589   - callback: _scb1,
590   - req: req,
591   - res: res,
592   - options: {},
593   - });
594   - });
595   - } else {
596   - res.send({ code: response.statusCode, message: data1.message });
597   - }
598   - }
599   -
600   - function _scb1(data2, response) {
601   - if (response.statusCode < 300 && data2 && data2.bucket) {
602   - req.session.passport.user.user_info.oss = data2;
603   - req.session.save(function () {
604   - res.send({ code: 200, message: "切换租户成功" });
605   - });
606   - } else {
607   - res.send({ code: 200, message: "切换租户失败" });
  526 + let result = await service.login.changeTant( params);
  527 + if (result && result.code >= 300) {
  528 + ctx.status = result.status;
  529 + ctx.body = result.data;
  530 + }else {
  531 + ctx.status = result.status
  532 + ctx.body = {
  533 + code:200,
  534 + message:'切换租户成功'
608 535 }
609 536 }
  537 + return ctx.body
  538 + // req.session.passport.user.token = "";
  539 + // const result = await curl(`${host}/${port}${tokenUrl}`, {
  540 + // method: "POST",
  541 + // dataType: "json",
  542 + // data: JSON.stringify(params),
  543 + // headers: {
  544 + // 'authorization': `Bearer ${access_token}`,
  545 + // accept: "application/json",
  546 + // "content-type": "application/json",
  547 + // },
  548 + // });
  549 + // logger.info(result,'resulpt')
  550 +
  551 +
  552 +
  553 + // function _scb(data1, response) {
  554 + // if (response.statusCode < 300 && data1.items) {
  555 + // req.session.passport.user.user_info.perms = data1.items;
  556 + // req.session.save(function () {
  557 + // rest.get({
  558 + // baseUrl: "url",
  559 + // url:
  560 + // settings.restful.url +
  561 + // "filemeta" +
  562 + // settings.restful.version +
  563 + // "/config",
  564 + // useUrl: true,
  565 + // params: {},
  566 + // callback: _scb1,
  567 + // req: req,
  568 + // res: res,
  569 + // options: {},
  570 + // });
  571 + // });
  572 + // } else {
  573 + // res.send({ code: response.statusCode, message: data1.message });
  574 + // }
  575 + // }
  576 +
  577 + // function _scb1(data2, response) {
  578 + // if (response.statusCode < 300 && data2 && data2.bucket) {
  579 + // req.session.passport.user.user_info.oss = data2;
  580 + // req.session.save(function () {
  581 + // res.send({ code: 200, message: "切换租户成功" });
  582 + // });
  583 + // } else {
  584 + // res.send({ code: 200, message: "切换租户失败" });
  585 + // }
  586 + // }
610 587 }
611 588
612 589 sendRejectOffer(req, res) {
... ...
... ... @@ -94,16 +94,10 @@ class ProxyController extends Controller {
94 94 const url = this.splitUrl(originalUrl);
95 95 const options = this.getOptions(url, 'GET');
96 96 const {host ,port,path,...rest} = options
97   - logger.info(`${host}:${port}${path}`, {
98   - ...rest,
99   - timeout: [5000, 60000],
100   - },12312321)
101 97 const result = await ctx.curl(`${host}:${port}${path}`, {
102 98 ...rest,
103 99 timeout: [5000, 60000],
104 100 });
105   - logger.info("result:", result);
106   -
107 101 ctx.body = result.data|| {};
108 102
109 103 return ctx.body;
... ...
... ... @@ -35,12 +35,10 @@ module.exports = (app) => {
35 35
36 36 // 序列化与反序列化,序列化存储到session中只保存用户id
37 37 app.passport.serializeUser(async (ctx, user) => {
38   - console.log(user, "user");
39 38 return user;
40 39 });
41 40
42 41 app.passport.deserializeUser(async (ctx, user) => {
43   - console.log(user,'kncvkxznkcxlklasdjfajdfldkasjflkasdnvkasdnmklasndvldnsa');
44 42 return user;
45 43 });
46 44 };
... ...
... ... @@ -6,6 +6,7 @@ module.exports = app => {
6 6
7 7 // 给proxy设置token
8 8 router.get(/^\/api\/[\w/]+/, 'proxy.get');
  9 + router.get(/^\/auth_api\/[\w/]+/, 'proxy.get');
9 10 router.post(/^\/api\/[\w/]+/, 'proxy.post');
10 11 router.put(/^\/api\/[\w/]+/, 'proxy.put');
11 12 router.head(/^\/api\/[\w/]+/, 'proxy.head');
... ...
... ... @@ -19,6 +19,27 @@ class LoginService extends Service {
19 19 ctx.session.user_info = result.data;
20 20 return result;
21 21 }
  22 +
  23 + async changeTant(params) {
  24 + const { ctx, config={}, logger } = this;
  25 + const {session} = ctx
  26 + const { restful={} } = config;
  27 + const { access_token } = session.user_info;
  28 + const { host, port, tokenUrl,tokenMethod} = restful;
  29 + const result = await ctx.curl(`${host}:${port}${tokenUrl}`, {
  30 + method: `${tokenMethod}`,
  31 + dataType: 'json',
  32 + data: JSON.stringify(params),
  33 + headers: {
  34 + 'authorization': `Bearer ${access_token}`,
  35 + 'accept': 'application/json',
  36 + 'content-type': 'application/json'
  37 + },
  38 + timeout: [5000, 60000]
  39 + });
  40 + ctx.session.user_info = result.data;
  41 + return result;
  42 + }
22 43
23 44 }
24 45
... ...
注册登录 后发表评论