feat: 抽奖活动公开数据序列化

- mask_name:公开页面的姓名脱敏(王诗云 → 王*云)
- LotteryCampaignInfo.public_status / overview_data:供小程序抽奖专区展示
  活动信息、参与人数、中奖名单(不含手机号)

FFIB 5 dias atrás
pai
commit
7ed53bef49
1 arquivos alterados com 53 adições e 0 exclusões
  1. 53 0
      sales/models.py

+ 53 - 0
sales/models.py

@@ -192,6 +192,22 @@ class LotteryCampaignQuerySet(models.QuerySet):
192 192
         return self.filter(status=True)
193 193
 
194 194
 
195
+def mask_name(name):
196
+    """公开页面用的姓名脱敏:王诗云 → 王*云,两字 → 王*"""
197
+    text = (name or u'').strip()
198
+
199
+    if not text:
200
+        return u'匿名用户'
201
+
202
+    if len(text) == 1:
203
+        return text + u'*'
204
+
205
+    if len(text) == 2:
206
+        return text[0] + u'*'
207
+
208
+    return text[0] + u'*' * (len(text) - 2) + text[-1]
209
+
210
+
195 211
 class LotteryCampaignInfo(BaseModelMixin):
196 212
     """抽奖活动"""
197 213
 
@@ -229,6 +245,43 @@ class LotteryCampaignInfo(BaseModelMixin):
229 245
         """已到开奖时间:报名信息锁定,只能改收货信息"""
230 246
         return bool(self.draw_at and timezone.now() >= self.draw_at)
231 247
 
248
+    def public_status(self):
249
+        """公开页面的阶段:upcoming / ongoing / pending / drawn / closed"""
250
+        now = timezone.now()
251
+
252
+        if not self.status:
253
+            return 'closed'
254
+        if self.draw_at and now >= self.draw_at:
255
+            return 'drawn'
256
+        if self.start_at and now < self.start_at:
257
+            return 'upcoming'
258
+        if self.end_at and now > self.end_at:
259
+            return 'pending'
260
+
261
+        return 'ongoing'
262
+
263
+    def overview_data(self, entries):
264
+        """公开页面:活动信息 + 参与人数 + 中奖名单(姓名脱敏,不含手机号)"""
265
+        winners = [entry for entry in entries if entry.result == LotteryEntryInfo.WON]
266
+
267
+        return {
268
+            'campaign_id': self.campaign_id,
269
+            'title': self.title,
270
+            'prize_text': self.prize_text or '',
271
+            'start_at': to_local_iso(self.start_at),
272
+            'end_at': to_local_iso(self.end_at),
273
+            'draw_at': to_local_iso(self.draw_at),
274
+            'status': self.public_status(),
275
+            'entry_count': len(entries),
276
+            'winner_count': self.winner_count,
277
+            'claim_days': self.claim_days,
278
+            'winners': [
279
+                {'name': mask_name(entry.name), 'model_name': entry.model_name or ''}
280
+                for entry in winners
281
+            ],
282
+            'rule_sections': self.rule_sections or [],
283
+        }
284
+
232 285
     @property
233 286
     def data(self):
234 287
         return {