callback.component.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { Inject, Optional, Component, OnInit, ViewContainerRef } from '@angular/core';
  17. import { ActivatedRoute, Router } from '@angular/router';
  18. import { ReuseTabService } from '@delon/abc/reuse-tab';
  19. import { SettingsService } from '@delon/theme';
  20. import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
  21. import { AuthnService } from '../../service/authn.service';
  22. import { SocialsProviderService } from '../../service/socials-provider.service';
  23. import { SocialsProviderBindUserComponent } from './socials-provider-bind-user/socials-provider-bind-user.component';
  24. @Component({
  25. selector: 'app-callback',
  26. template: ``
  27. })
  28. export class CallbackComponent implements OnInit {
  29. provider = '';
  30. constructor(
  31. private viewContainerRef: ViewContainerRef,
  32. private modalService: NzModalService,
  33. private router: Router,
  34. private socialsProviderService: SocialsProviderService,
  35. private settingsService: SettingsService,
  36. private authnService: AuthnService,
  37. @Optional()
  38. @Inject(ReuseTabService)
  39. private reuseTabService: ReuseTabService,
  40. private route: ActivatedRoute
  41. ) {}
  42. ngOnInit(): void {
  43. this.provider = this.route.snapshot.params['provider'];
  44. if (!this.settingsService.user.name) {
  45. this.socialsProviderService.callback(this.provider, this.route.snapshot.queryParams).subscribe(res => {
  46. if (res.code === 0) {
  47. // 清空路由复用信息
  48. this.reuseTabService.clear();
  49. // 设置用户Token信息
  50. this.authnService.auth(res.data);
  51. } else if (res.code === 102) {
  52. //绑定用户
  53. this.openBindUser(res.message);
  54. return;
  55. }
  56. this.authnService.navigate({});
  57. });
  58. } else {
  59. this.socialsProviderService.bind(this.provider, this.route.snapshot.queryParams).subscribe(res => {
  60. if (res.code === 0) {
  61. }
  62. this.router.navigateByUrl('/config/socialsassociate');
  63. });
  64. }
  65. }
  66. openBindUser(socialUserId: String) {
  67. console.log('bind user : ', this.provider, socialUserId);
  68. const modal = this.modalService.create({
  69. nzContent: SocialsProviderBindUserComponent,
  70. nzViewContainerRef: this.viewContainerRef,
  71. nzComponentParams: {
  72. socialUserId: socialUserId,
  73. provider: this.provider
  74. },
  75. nzOnOk: () => new Promise(resolve => setTimeout(resolve, 1000))
  76. });
  77. // Return a result when closed
  78. modal.afterClose.subscribe(result => {
  79. if (result.refresh) {
  80. }
  81. });
  82. }
  83. }