zoukankan      html  css  js  c++  java
  • angular4.0微信oAuth第三方认证的正确方式

    当我们的项目运行在微信端时,用到oAuth第三方认证。问题来了,在ng4中微信认证应该放在哪里呢?

    开始项目的时候,我将oAuth认证放在了每个页面模版中,发现返回历史页的时候,需要返回两次。

    这个问题应该是认证的时候跳转页面导致的,所以,我们要考虑将oAuth放到合适的位置去。

    下面具体的来说一说oAuth在ng4的步骤。

    一、引入oauth.js文件

    将oauth.js文件放在“assets”文件夹下,然后在index.html里面引入

    <script src="assets/js/oauth.js"></script>

    二、声明变量oAuth

    这一步很关键,因为引入的js文件中的对象oAuth,并不能被ng识别,所以我们需要先声明oAuth。

    在typings.d.ts中全局申明oAuth变量

    declare var oAuth: any;

    三、在main.ts实现oAuth微信认证

    为什么在main.ts中实现认证?main.ts负责引导整个angular应用的起点。具体请移步我写的《angular4.0项目main.ts详解》中去学习吧。

    实现微信认证请看下面的代码,核心是:在微信认证完成后的回调中,执行 platformBrowserDynamic().bootstrapModule(AppModule);

    import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    
    if (environment.production) {
      enableProdMode();
    }
    
    //微信oauth认证
    oAuth.cfg(Config.uid, Config.isDebug, Config.scope);
    oAuth.checkToken((apiopenid, apitoken) => {
        Config.apiopenid = apiopenid;
        Config.apitoken = apitoken;
        //认证完成后,调用bootstrapModule方法来传入AppModule作为启动模块来启动应用。
        platformBrowserDynamic().bootstrapModule(AppModule);
    });

    ok,就这样完成了,谢谢大婶指点。

  • 相关阅读:
    HDU 5883 欧拉回路
    HDU 5889 Barricade (Dijkstra+Dinic)
    网络流Dinic算法模板 POJ1273
    216. Combination Sum III
    211. Add and Search Word
    973. K Closest Points to Origin
    932. Beautiful Array
    903. Valid Permutations for DI Sequence
    514. Freedom Trail
    312. Burst Balloons
  • 原文地址:https://www.cnblogs.com/minigrasshopper/p/7727974.html
Copyright © 2011-2022 走看看