2025-03-18 08:46:50 +08:00

21 lines
891 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from apps.oauth.views import get_wechat_xcx_access_token_url
from django.core.management.base import BaseCommand
import json
from django.core.cache import cache
"""
7200秒 2小时时间可以设置小于7200秒
定时获取微信小程序的access_token放到缓存redis默认access_token过期时间2小时
"""
class Command(BaseCommand):
def handle(self, *args, **options):
restoken = get_wechat_xcx_access_token_url()
if restoken.status_code == 200:
json_data = json.loads(restoken.content)
if 'errcode' not in json_data: # 如果获取失败返回失败信息
access_token = json_data['access_token']
expires_in = json_data['expires_in']
cache.set('xcx_access_token', access_token,7000)
# cache.expire('xcx_access_token', expires_in)