This commit is contained in:
doudou201703
2026-08-21 23:48:29 +08:00
parent 0e2fe60253
commit dd4383130d
20 changed files with 179 additions and 241 deletions
@@ -26,9 +26,9 @@ def sync_lock_线程锁(lock):
def wrapper(*args, **kwargs):
with lock:
return fn(*args, **kwargs)
return wrapper
return decorator
@@ -39,10 +39,9 @@ class AllowMyAppRedirect(HttpResponseRedirect):
class 接收微信的code回调ViewSets(ViewSet):
lock = threading.Lock()
private_key_pem_私钥 = Path(settings.BASE_DIR).joinpath("django_jwt_token/private.pem").read_bytes()
def jwt_生成token(self, openid, uniond):
# 读取私钥的 bytes字符串
# 创建一个 JWT对象
jwt对象实例 = JWT() # 创建对象
# 创建 payload
@@ -57,7 +56,7 @@ class 接收微信的code回调ViewSets(ViewSet):
signing_key = jwk_from_pem(pem_content=私钥对象, )
token = jwt对象实例.encode(payload=message, key=signing_key, alg="RS256")
return token
def get网站授权access_token(self, code, state):
uri = "https://api.weixin.qq.com/sns/oauth2/access_token"
postData = requests.post(url=uri, params={
@@ -72,8 +71,9 @@ class 接收微信的code回调ViewSets(ViewSet):
openid = pydash.get(obj=postData, path="openid")
scope = pydash.get(obj=postData, path="scope")
unionid = pydash.get(obj=postData, path="unionid")
print(postData, "00000000000000000000")
return postData
def get获取用户个人信息(self, code, state):
uri = f"https://api.weixin.qq.com/sns/userinfo"
token, is创建 = weitoken.objects.update_or_create(token_name="web_token")
@@ -110,7 +110,7 @@ class 接收微信的code回调ViewSets(ViewSet):
getData = requests.get(url=uri, params={"access_token": access_token, "openid": openid})
res = orjson.loads(getData.content.decode(encoding="utf-8"))
return res
@action(detail=False, methods=["get", ], url_path="get_code", )
# @sync_lock_线程锁( lock=lock )
def get_code(self, request: Request):
@@ -126,7 +126,7 @@ class 接收微信的code回调ViewSets(ViewSet):
openid = pydash.get(obj=user_info_用户信息, path="openid")
unionid = pydash.get(obj=user_info_用户信息, path="unionid")
print("为什么没有值了呢", openid, unionid)
token = self.jwt_生成token(openid=openid, uniond=unionid)
token = self.jwt_生成token(openid=openid, uniond=unionid) # 前端带着code进来,
setData = pydash.set_(obj=user_info_用户信息, path="token", value=token)
query_string = urlencode(setData, )
uri = f"myapp://open/home?{query_string}"