Merge branch 'tamron' of http://git.xfoto.com.cn/Kodo/kodo into tamron

FFIB 2 年之前
父节点
当前提交
dc3d5dffc6
共有 4 个文件被更改,包括 52 次插入13 次删除
  1. 29 13
      api/wxa_views.py
  2. 1 0
      kodo/settings.py
  3. 3 0
      utils/redis/rkeys.py
  4. 19 0
      utils/redis/rwxacode.py

+ 29 - 13
api/wxa_views.py

@@ -8,36 +8,45 @@ from pywe_wxa_qrcode import get_wxa_code_unlimit
8 8
 from miniapp.models import SceneInfo
9 9
 from utils.qiniucdn import qiniu_file_url, upload
10 10
 from utils.redis.connect import r
11
+from utils.redis.rwxacode import get_wxa_code_qiniu_url, get_wxa_code_scene_, set_wxa_code_info
11 12
 
12 13
 
13 14
 WECHAT = settings.WECHAT
14 15
 
15 16
 
17
+# Support API Cache
16 18
 def get_wxa_code(request):
17 19
     scene = request.POST.get('scene', '')
18 20
     page = request.POST.get('page', '')
19 21
 
20
-    si, created = SceneInfo.objects.get_or_create(scene=scene, page=page, status=True)
22
+    qiniu_url = get_wxa_code_qiniu_url(scene, page)
21 23
 
22
-    if si.qiniu_url:
24
+    if qiniu_url:
23 25
         return response(data={
24
-            'qiniu_url': si.qiniu_url,
26
+            'qiniu_url': qiniu_url,
25 27
         })
26 28
 
27
-    wxcfg = WECHAT.get('MINIAPP', {})
29
+    si, created = SceneInfo.objects.get_or_create(scene=scene, page=page, status=True)
30
+
31
+    qiniu_url = si.qiniu_url
32
+
33
+    if not qiniu_url:
34
+        wxcfg = WECHAT.get('MINIAPP', {})
35
+
36
+        appid = wxcfg.get('appID')
37
+        secret = wxcfg.get('appsecret')
28 38
 
29
-    appid = wxcfg.get('appID')
30
-    secret = wxcfg.get('appsecret')
39
+        res = get_wxa_code_unlimit(si.sid, page, res_to_base64=False, appid=appid, secret=secret, storage=RedisStorage(r))
31 40
 
32
-    res = get_wxa_code_unlimit(si.sid, page, res_to_base64=False, appid=appid, secret=secret, storage=RedisStorage(r))
41
+        if res.headers and res.headers.get('Content-disposition'):
42
+            qiniu_url = qiniu_file_url(upload(res.content))
43
+        else:
44
+            qiniu_url = ''
33 45
 
34
-    if res.headers and res.headers.get('Content-disposition'):
35
-        qiniu_url = qiniu_file_url(upload(res.content))
36
-    else:
37
-        qiniu_url = ''
46
+        si.qiniu_url = qiniu_url
47
+        si.save()
38 48
 
39
-    si.qiniu_url = qiniu_url
40
-    si.save()
49
+    set_wxa_code_info(scene, page, qiniu_url, si.sid)
41 50
 
42 51
     return response(data={
43 52
         'qiniu_url': qiniu_url,
@@ -47,6 +56,13 @@ def get_wxa_code(request):
47 56
 def get_wxa_code_scene(request):
48 57
     sid = request.POST.get('sid', '')
49 58
 
59
+    scene = get_wxa_code_scene_(sid)
60
+
61
+    if scene:
62
+        return response(data={
63
+            'scene': scene,
64
+        })
65
+
50 66
     try:
51 67
         scene = SceneInfo.objects.get(sid=sid, status=True)
52 68
     except SceneInfo.DoesNotExist:

+ 1 - 0
kodo/settings.py

@@ -205,6 +205,7 @@ REDIS = {
205 205
         'USER': '',
206 206
         'PASSWORD': '',
207 207
         'db': 0,
208
+        'decode_responses': True,
208 209
     }
209 210
 }
210 211
 

+ 3 - 0
utils/redis/rkeys.py

@@ -22,5 +22,8 @@ MEMBER_SEND_COUPON_LIST = 'kodo:member:send:coupon:list'
22 22
 MEMBER_SEND_COUPON_LIST2 = 'kodo:member:send:coupon:list2'
23 23
 MEMBER_UPGRADE_INFO = 'kodo:member:upgrade:info:%s:%s'  # brand_id, user_id
24 24
 
25
+WXA_CODE_SCENE_PAGE_QINIUURL_MAPPING = 'kodo:wxa:code:scene:page:mapping'  # HASH, {scene+page: qiniu_url}
26
+WXA_CODE_SID_SCENE_MAPPING = 'kodo:wxa:code:scene:mapping'  # HASH, {sid: scene}
27
+
25 28
 # 七牛
26 29
 QINIU_UPLOAD_LIST = 'kodo:qiniu:upload:list'

+ 19 - 0
utils/redis/rwxacode.py

@@ -0,0 +1,19 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+from utils.redis.connect import r
4
+from utils.redis.rkeys import WXA_CODE_SCENE_PAGE_QINIUURL_MAPPING, WXA_CODE_SID_SCENE_MAPPING
5
+
6
+
7
+def set_wxa_code_info(scene, page, qiniu_url, sid):
8
+    p = r.pipeline()
9
+    p.hset(WXA_CODE_SCENE_PAGE_QINIUURL_MAPPING, '{0}:{1}'.format(scene, page), qiniu_url)
10
+    p.hset(WXA_CODE_SID_SCENE_MAPPING, sid, scene)
11
+    p.execute()
12
+
13
+
14
+def get_wxa_code_qiniu_url(scene, page):
15
+    return r.hget(WXA_CODE_SCENE_PAGE_QINIUURL_MAPPING, '{0}:{1}'.format(scene, page))
16
+
17
+
18
+def get_wxa_code_scene_(sid):
19
+    return r.hget(WXA_CODE_SID_SCENE_MAPPING, sid)