Browse Source

长文业务: 随机返回3个小程序
投流业务:返回一个小程序
企微业务: 返回一个小程序

新增接口: 增加mini_program信息

罗俊辉 8 months ago
parent
commit
9e5a3cca56
4 changed files with 184 additions and 37 deletions
  1. 2 1
      applications/deal/__init__.py
  2. 98 0
      applications/deal/minigram.py
  3. 72 35
      applications/deal/response.py
  4. 12 1
      applications/routes.py

+ 2 - 1
applications/deal/__init__.py

@@ -3,4 +3,5 @@
 接口信息
 """
 from .record import Record
-from .response import Response
+from .response import Response
+from .minigram import Minigram

+ 98 - 0
applications/deal/minigram.py

@@ -0,0 +1,98 @@
+"""
+@author: luojunhui
+"""
+minigram_map = {
+    1: {
+        25: {
+            "avatar": "https://rescdn.yishihui.com/0temp/ttmhzfsh.png",
+            "id": "wx0b7d95eb293b783b",
+            "name": "天天美好祝福生活",
+            "index": 25
+        },
+        29: {
+            "avatar": "https://rescdn.yishihui.com/0temp/cyfyld.png",
+            "id": "wx65c76bb4c67934db",
+            "name": "财运福运来到",
+            "index": 29
+        },
+        31: {
+            "avatar": "https://rescdn.yishihui.com/0temp/mhzfshxf2.png",
+            "id": "wx2e4478b1641b3b15",
+            "name": "美好祝福生活幸福",
+            "index": 31
+        }
+    },
+    2: {
+        36: {
+            "avatar": "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg",
+            "id": "wxcddf231abd0dabdc",
+            "name": "祝福有福有财",
+            "index": 36
+        }
+    },
+    3: {
+        27: {
+            "avatar": "https://rescdn.yishihui.com/0temp/xymhfqdd.png",
+            "id": "wx7187c217efef24a7",
+            "name": "幸运美好福气多多",
+            "index": 27
+        }
+    }
+}
+
+
+class Minigram(object):
+    """
+    小程序卡片
+    """
+
+    def __init__(self, params):
+        self.params = params
+        self.business_type = None
+        self.mini_code = None
+        self.trace_id = None
+
+    def check_params(self):
+        """
+        校验参数
+        :return:
+        """
+        try:
+            self.business_type = self.params['businessType']
+            self.mini_code = self.params['miniCode']
+            self.trace_id = self.params['traceId']
+            return None
+        except Exception as e:
+            response = {
+                "status": "fail",
+                "code": 1,
+                "message": str(e),
+                "info": "params check error"
+            }
+            return response
+
+    def choose_minigram(self):
+        """
+        分配小程序卡片
+        :return:
+        """
+        try:
+            minigram = minigram_map.get(self.business_type).get(self.mini_code)
+            response = {
+                "programAvatar": minigram['avatar'],
+                "programId": minigram['id'],
+                "programName": minigram['name'],
+                "trace_id": self.trace_id
+            }
+        except Exception as e:
+            response = {
+                "error": "invalid params",
+                "msg": str(e)
+            }
+        return response
+
+    async def deal(self):
+        """
+        :return:
+        """
+        return self.check_params() if self.check_params() else self.choose_minigram()

+ 72 - 35
applications/deal/response.py

@@ -22,6 +22,38 @@ class Response(object):
         self.trace_id = trace_id
         self.mysql_client = mysql_client
         self.mini_program_type = mini_program_type
+        self.mini_map = {
+            25: {
+                "avatar": "https://rescdn.yishihui.com/0temp/ttmhzfsh.png",
+                "id": "wx0b7d95eb293b783b",
+                "name": "天天美好祝福生活",
+                "index": 25
+            },
+            29: {
+                "avatar": "https://rescdn.yishihui.com/0temp/cyfyld.png",
+                "id": "wx65c76bb4c67934db",
+                "name": "财运福运来到",
+                "index": 29
+            },
+            31: {
+                "avatar": "https://rescdn.yishihui.com/0temp/mhzfshxf2.png",
+                "id": "wx2e4478b1641b3b15",
+                "name": "美好祝福生活幸福",
+                "index": 31
+            },
+            36: {
+                "avatar": "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg",
+                "id": "wxcddf231abd0dabdc",
+                "name": "祝福有福有财",
+                "index": 36
+            },
+            27: {
+                "avatar": "https://rescdn.yishihui.com/0temp/xymhfqdd.png",
+                "id": "wx7187c217efef24a7",
+                "name": "幸运美好福气多多",
+                "index": 27
+            }
+        }
 
     async def get_result(self):
         """
@@ -76,36 +108,15 @@ class Response(object):
         # auto_white(root_share_id)
         return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
 
-    def choose_mini_program(self):
+    def choose_mini_program(self, mini_id):
         """
         获取小程序分享卡片
         :return:
         """
-        if self.mini_program_type == 1:
-            # 正常长文业务
-            # programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
-            # programId = "wx59d9e2c05f00f880"
-            # programName = "刷刷有趣生活"
-            programAvatar = "https://rescdn.yishihui.com/0temp/ttmhzfsh.png"
-            programId = "wx0b7d95eb293b783b"
-            programName = "天天美好祝福生活"
-        elif self.mini_program_type == 2:
-            # 投流业务
-            programAvatar = "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg"
-            programId = "wxcddf231abd0dabdc"
-            programName = "祝福有福有财"
-        elif self.mini_program_type == 3:
-            # 企业微信
-            programAvatar = "https://rescdn.yishihui.com/0temp/xymhfqdd.png"
-            programId = "wx7187c217efef24a7"
-            programName = "幸运美好福气多多"
-        else:
-            programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
-            programId = "wx59d9e2c05f00f880"
-            programName = "刷刷有趣生活"
-        return programAvatar, programId, programName
+        mini_info = self.mini_map[mini_id]
+        return mini_info['avatar'], mini_info['id'], mini_info['name'], mini_info['index']
 
-    async def generate_card(self, video_id, kimi_title, index, gh_id):
+    async def generate_card(self, video_id, kimi_title, index, gh_id, mini_id):
         """
         生成分享卡片
         :return:
@@ -115,7 +126,7 @@ class Response(object):
         productionName = kimi_title
         videoUrl = response['data'][0]['videoPath']
         user_id = response['data'][0]['user']['uid']
-        programAvatar, programId, programName = self.choose_mini_program()
+        programAvatar, programId, programName, pqMiniId = self.choose_mini_program(mini_id)
         root_share_id, source_id, productionPath = self.create_gzh_path(video_id, user_id, gh_id)
         source = "Web"
         logging(
@@ -134,6 +145,7 @@ class Response(object):
             "rootShareId": root_share_id,
             "productionPath": productionPath,
             "videoUrl": videoUrl,
+            "mini_id": mini_id,
             "paragraphPosition": index * 0.25
         }
         update_result_sql = f"""
@@ -161,6 +173,36 @@ class Response(object):
         )
         return result
 
+    async def generate_cards(self, video_list, kimi_title, gh_id):
+        """
+        生成一组卡片
+        :param video_list:
+        :param kimi_title:
+        :param gh_id:
+        :return:
+        """
+        L = []
+        if self.mini_program_type == 1:
+            mini_id_list = [25, 29, 31]
+            video_count = len(video_list)
+            mini_choice_index = random.sample(range(3), video_count)
+            mini_choice_item = [mini_id_list[i] for i in mini_choice_index]
+            for index, video_id in enumerate(video_list, 1):
+                result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
+                                                  mini_id=mini_choice_item[index - 1])
+                L.append(result)
+        elif self.mini_program_type == 2:
+            for index, video_id in enumerate(video_list, 1):
+                result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
+                                                  mini_id=36)
+                L.append(result)
+        elif self.mini_program_type == 3:
+            for index, video_id in enumerate(video_list, 1):
+                result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
+                                                  mini_id=27)
+                L.append(result)
+        return L
+
     async def deal(self):
         """
         Recall Deal
@@ -170,7 +212,7 @@ class Response(object):
         status_code = response.get("content_status")
         process_times = response.get("process_times")
         if status_code == 0:
-            if process_times > 5:
+            if process_times > 3:
                 result = {
                     "traceId": self.trace_id,
                     "code": 0,
@@ -189,15 +231,12 @@ class Response(object):
                 "Message": "该请求正在处理中"
             }
         elif status_code == 2:
-            L = []
             unEmptyList = [i for i in response['vid_list'] if i]
-            for index, best_video_id in enumerate(unEmptyList, 1):
-                card = await self.generate_card(best_video_id, response.get("kimi_title"), index, response['gh_id'])
-                L.append(card)
-            if L:
+            cards = await self.generate_cards(unEmptyList, response.get("kimi_title"), response['gh_id'])
+            if cards:
                 result = {
                     "traceId": self.trace_id,
-                    "miniprogramList": L
+                    "miniprogramList": cards
                 }
             else:
                 result = {
@@ -224,5 +263,3 @@ class Response(object):
             trace_id=self.trace_id
         )
         return result
-
-

+ 12 - 1
applications/routes.py

@@ -3,7 +3,7 @@
 """
 from quart import Blueprint, jsonify, request
 
-from applications.deal import Response, Record
+from applications.deal import Response, Record, Minigram
 
 my_blueprint = Blueprint('LongArticles', __name__)
 
@@ -45,4 +45,15 @@ def Routes(mysql_client):
         response = await RD.deal()
         return jsonify(response)
 
+    @my_blueprint.route("/choose_minigram", methods=['POST'])
+    async def match_minigram():
+        """
+        获取小程序信息
+        :return:
+        """
+        data = await request.get_json()
+        M = Minigram(params=data)
+        response = await M.deal()
+        return jsonify(response)
+
     return my_blueprint