Command subscribe

Kimi.Huang 5 lat temu
rodzic
commit
099d167822

+ 1 - 0
commands/management/commands/membercard.py

@@ -34,6 +34,7 @@ class Command(CompatibilityBaseCommand):
34 34
 
35 35
         while True:
36 36
             # r.rpushjson('MEMBERCARD_USERINFO_LIST', {
37
+            #    'brand_id': 'brand_id',
37 38
             #    'card_id': 'card_id',
38 39
             #    'code': 'code',
39 40
             # })

+ 72 - 0
commands/management/commands/subscribe.py

@@ -0,0 +1,72 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import logging
4
+
5
+from django.conf import settings
6
+from django.db import transaction
7
+from django_six import CompatibilityBaseCommand, close_old_connections
8
+from django_we.models import SubscribeUserInfo
9
+from pywe_storage import RedisStorage
10
+from pywe_user import get_user_info
11
+
12
+from account.models import UserInfo
13
+from utils.redis.connect import r
14
+from utils.redis.rkeys import SUBSCRIBE_USERINFO_LIST
15
+
16
+
17
+WECHAT = settings.WECHAT
18
+
19
+
20
+logger = logging.getLogger('console')
21
+
22
+
23
+class Command(CompatibilityBaseCommand):
24
+    def handle(self, *args, **options):
25
+
26
+        logger.info('SubscribeUserInfo is dealing')
27
+
28
+        while True:
29
+            # r.rpushjson('SUBSCRIBE_USERINFO_LIST', {
30
+            #    'brand_id': 'brand_id',
31
+            #    'openid': 'openid',
32
+            # })
33
+            k, v = r.blpopjson(SUBSCRIBE_USERINFO_LIST, 60)
34
+            if not v:
35
+                continue
36
+
37
+            logger.info(v)
38
+
39
+            brand_id, openid = v.get('brand_id', ''), v.get('openid', '')
40
+
41
+            wxcfg = WECHAT.get('{}:JSAPI'.format(brand_id), {})
42
+
43
+            appid = wxcfg.get('appID')
44
+            secret = wxcfg.get('appsecret')
45
+
46
+            userinfo = get_user_info(openid, appid=appid, secret=secret, storage=RedisStorage(r))
47
+
48
+            close_old_connections()
49
+
50
+            with transaction.atomic():
51
+                unionid, openid = userinfo.get('unionid', ''), userinfo.get('openid', '')
52
+                SubscribeUserInfo.objects.update_or_create(openid=openid, defaults={
53
+                    'unionid': unionid,
54
+                    'nickname': userinfo.get('nickname', ''),
55
+                    'sex': userinfo.get('sex', ''),
56
+                    'headimgurl': userinfo.get('headimgurl', ''),
57
+                    'country': userinfo.get('country', ''),
58
+                    'province': userinfo.get('province', ''),
59
+                    'city': userinfo.get('city', ''),
60
+                    'subscribe': userinfo.get('subscribe', ''),
61
+                    'subscribe_time': userinfo.get('subscribe_time', ''),
62
+                    'subscribe_scene': userinfo.get('subscribe_scene', ''),
63
+                    'groupid': userinfo.get('groupid', ''),
64
+                    'tagid_list': userinfo.get('tagid_list', ''),
65
+                    'qr_scene': userinfo.get('qr_scene', ''),
66
+                    'qr_scene_str': userinfo.get('qr_scene_str', ''),
67
+                    'language': userinfo.get('language', ''),
68
+                    'remark': userinfo.get('remark', ''),
69
+                })
70
+                UserInfo.objects.filter(unionid=unionid).update(openid=openid)
71
+
72
+            close_old_connections()

+ 1 - 1
requirements_dj.txt

@@ -20,5 +20,5 @@ django-rlog==1.0.7
20 20
 django-shortuuidfield==0.1.3
21 21
 django-six==1.0.4
22 22
 django-uniapi==1.0.5
23
-django-we==1.4.2
23
+django-we==1.5.0
24 24
 djangorestframework==3.9.4

+ 1 - 1
requirements_pywe.txt

@@ -9,5 +9,5 @@ pywe-pay==1.0.13
9 9
 pywe-pay-notify==1.0.4
10 10
 pywe-response==1.0.1
11 11
 pywe-sign==1.0.8
12
-pywe-user==1.0.2
12
+pywe-user==1.0.4
13 13
 pywe-xml==1.0.6

+ 11 - 4
shells/views.py

@@ -5,19 +5,26 @@ from pywe_storage import RedisStorage
5 5
 from pywe_user import get_all_users
6 6
 
7 7
 from utils.redis.connect import r
8
+from utils.redis.rkeys import SUBSCRIBE_USERINFO_LIST
8 9
 
9 10
 
10 11
 WECHAT = settings.WECHAT
11 12
 
12 13
 
13
-def fetch_users_func(authorizer_appid, get_user_infos):
14
-    print authorizer_appid, get_user_infos
14
+def fetch_users_func(authorizer_appid, infos):
15
+    print authorizer_appid, infos
16
+    openids = infos.get('data', {}).get('openid', [])
17
+    for openid in openids:
18
+        r.rpushjson(SUBSCRIBE_USERINFO_LIST, {
19
+            'brand_id': authorizer_appid,
20
+            'openid': openid
21
+        })
15 22
 
16 23
 
17
-def fetch_all_users(brand_id, ):
24
+def fetch_all_users(brand_id):
18 25
     wxcfg = WECHAT.get('{}:JSAPI'.format(brand_id), {})
19 26
 
20 27
     appid = wxcfg.get('appID')
21 28
     secret = wxcfg.get('appsecret')
22 29
 
23
-    get_all_users(appid=appid, secret=secret, storage=RedisStorage(r), users_func=fetch_users_func)
30
+    get_all_users(appid=appid, secret=secret, storage=RedisStorage(r), authorizer_appid=brand_id, users_func=fetch_users_func)

+ 2 - 0
utils/redis/rkeys.py

@@ -75,3 +75,5 @@ SCREEN_ADMIN_LOGIN = 'tamron:screen:admin:login:%s:%s'  # brand_id, token
75 75
 REDPACK_WAITING_SEND_LIST = 'tamron:redpack:waiting:send'  #
76 76
 
77 77
 MEMBERCARD_USERINFO_LIST = 'tamron:membercard:userinfo'  #
78
+
79
+SUBSCRIBE_USERINFO_LIST = 'subscribe:userinfo'