ymao il y a 4 ans
Parent
commit
dff3799f68
5 fichiers modifiés avec 59 ajouts et 17 suppressions
  1. 29 0
      __init__.py
  2. 6 1
      authen/wxlogin/__init__.py
  3. 8 7
      authen/wxlogin/login.py
  4. 4 4
      authen/wxlogin/wraps.py
  5. 12 5
      authen/wxlogin/wx.py

+ 29 - 0
__init__.py

@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+import time
+import json
+
+from flask import Blueprint, url_for
+
+from .login import bp
+from .wx import WeixinLogin
+
+
+class WxLogin(WeixinLogin):
+
+    def __init__(self, app=None, config_prefix=""):
+        self.prefix = config_prefix
+        if app is not None:
+            self.init_app(app)
+
+    def init_app(self, app):
+        app.config.setdefault(self.prefix + "WXMP_APP_ID", None)
+        app.config.setdefault(self.prefix + "WXMP_APP_SECRET", None)
+        app.config.setdefault(self.prefix + "WXMP_USER_COOKIES_NAME", None)
+        app.config.setdefault(self.prefix + "WXMP_VERSION_CODE", "v1")
+        super(WxLogin, self).__init__(app, self.prefix)
+        # wx_login.init_app(app)
+
+        # 注册rbac视图函数
+        app.register_blueprint(
+            bp, url_prefix="/%s/wxlogin" % app.config["WXMP_VERSION_CODE"])

+ 6 - 1
authen/wxlogin/__init__.py

@@ -1,5 +1,10 @@
 # -*- coding: utf-8 -*-
 
+import time
+import json
+
+from flask import Blueprint, url_for
+
 from .login import bp
 from .wx import WeixinLogin
 
@@ -16,7 +21,7 @@ class WxLogin(WeixinLogin):
         app.config.setdefault(self.prefix + "WXMP_APP_SECRET", None)
         app.config.setdefault(self.prefix + "WXMP_USER_COOKIES_NAME", None)
         app.config.setdefault(self.prefix + "WXMP_VERSION_CODE", "v1")
-        super(WxLogin, self).init_app(app, self.prefix)
+        super(WxLogin, self).__init__(app, self.prefix)
         # wx_login.init_app(app)
 
         # 注册rbac视图函数

+ 8 - 7
authen/wxlogin/login.py

@@ -4,10 +4,10 @@ import json
 import urllib
 import datetime
 
-from flask import Blueprint, url_for, redirect, \
-    request, current_app
+from flask import Blueprint, url_for, flash, redirect, \
+    render_template, request, current_app
 
-from .wx import wxl
+from .wx import wx_login
 
 
 bp = Blueprint("wxmp", __name__, template_folder="templates")
@@ -15,25 +15,26 @@ bp = Blueprint("wxmp", __name__, template_folder="templates")
 
 @bp.route("/login")
 def wxmp_login():
+    wx_login.init_app(current_app)
     oauth_type = request.args.get("oauth_type", "snsapi_userinfo")
     if oauth_type not in ("snsapi_userinfo", "snsapi_base", "snsapi_login"):
         oauth_type = "snsapi_userinfo"
     next = request.args.get("next") or request.referrer or "/"
-    callback = url_for(
-        "wxmp.wxmp_authorized",
+    callback = url_for("wxmp.wxmp_authorized",
         oauth_type=oauth_type, next=next, _external=True)
-    url = wxl.authorize(urllib.parse.quote(callback), oauth_type)
+    url = wx_login.authorize(urllib.parse.quote(callback), oauth_type)
     return redirect(url)
 
 
 @bp.route("/authorized")
 def wxmp_authorized():
+    wx_login.init_app(current_app)
     code = request.args.get("code")
     if not code:
         return "ERR_INVALID_CODE", 400
     next = request.args.get("next", "/")
     oauth_type = request.args.get("oauth_type", "snsapi_userinfo")
-    data = wxl.access_token(code)
+    data = wx_login.access_token(code)
     # openid = data.openid
     # unionid = data.unionid
     resp = redirect(next)

+ 4 - 4
authen/wxlogin/wraps.py

@@ -1,10 +1,11 @@
+# -*- coding: utf-8 -*-
+
 import json
 import urllib
 
 from flask import redirect, url_for, request, current_app
 from functools import wraps
 
-
 def wxmp_login_required(func):
     @wraps(func)
     def decorated_view(*args, **kwargs):
@@ -16,7 +17,6 @@ def wxmp_login_required(func):
         return func(*args, **kwargs)
     return decorated_view
 
-
 def wxmp_base_login_required(func):
     @wraps(func)
     def decorated_view(*args, **kwargs):
@@ -25,9 +25,9 @@ def wxmp_base_login_required(func):
         if not token_data:
             return redirect(
                 url_for(
-                    "wxmp.wxmp_login",
+                    "wxmp.wxmp_login", 
                     next="?".join([
-                        request.path,
+                        request.path, 
                         urllib.parse.urlencode(request.args)
                     ]),
                     oauth_type="snsapi_base")

+ 12 - 5
authen/wxlogin/wx.py

@@ -65,7 +65,6 @@ class Map(dict):
         super(Map, self).__delitem__(key)
         del self.__dict__[key]
 
-
 class WeixinLoginError(WeixinError):
 
     def __init__(self, msg):
@@ -74,11 +73,18 @@ class WeixinLoginError(WeixinError):
 
 class WeixinLogin(object):
 
-    app_id = ""
-    app_secret = ""
+    def __init__(self, app=None, prefix=""):
+        if app:
+            self.app_id = app.config.get(prefix + "WXMP_APP_ID")
+            print(self.app_id, "fdsafdafdsafsa")
+            self.app_secret = app.config.get(prefix + "WXMP_APP_SECRET")
+        else:
+            self.app_id = ""
+            self.app_secret = ""
 
     def init_app(self, app, prefix=""):
         self.app_id = app.config.get(prefix + "WXMP_APP_ID")
+        print(self.app_id, "fdsafdafdsafsa")
         self.app_secret = app.config.get(prefix + "WXMP_APP_SECRET")
 
     def _get(self, url, params):
@@ -103,12 +109,14 @@ class WeixinLogin(object):
         else:
             url = "https://open.weixin.qq.com/connect/oauth2/authorize"
         data = dict()
+        print(self.app_id, "4444444")
         data.setdefault("appid", self.app_id)
         data.setdefault("redirect_uri", redirect_uri)
         data.setdefault("response_type", "code")
         data.setdefault("scope", scope)
         if state:
             data.setdefault("state", state)
+        print(data, "data")
         data = [(k, data[k]) for k in sorted(data.keys()) if data[k]]
         s = "&".join("=".join(kv) for kv in data if kv[1])
         print(s)
@@ -178,5 +186,4 @@ class WeixinLogin(object):
         """
         return self.userinfo(access_token, openid)
 
-
-wxl = WeixinLogin()
+wx_login = WeixinLogin()