58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
![]() |
import time
|
|||
|
from django.http import JsonResponse
|
|||
|
from django.utils.timezone import now
|
|||
|
from django.db import connections
|
|||
|
from datetime import datetime
|
|||
|
|
|||
|
# 假设您有一个积分奖励的常量
|
|||
|
REWARD_POINTS = 100 # 例如100积分
|
|||
|
|
|||
|
def reward_points_based_on_membership(openid):
|
|||
|
"""
|
|||
|
根据openid查询用户,判断是否是会员,并检查会员是否过期。
|
|||
|
如果是会员且未过期,则奖励积分。
|
|||
|
:param openid: 用户的openid
|
|||
|
:return: JSON响应,包含奖励积分或未满足条件的信息
|
|||
|
"""
|
|||
|
# 假设数据库名为 'external_db',其中有 'User' 表
|
|||
|
with connections['external_db'].cursor() as cursor:
|
|||
|
# 查询用户是否是会员,以及会员到期时间
|
|||
|
query = 'SELECT is_member, member_end_time FROM user WHERE openid = %s'
|
|||
|
cursor.execute(query, [openid])
|
|||
|
result = cursor.fetchone()
|
|||
|
|
|||
|
if not result:
|
|||
|
return JsonResponse({'code': 404, 'message': '用户未找到', 'data': {}})
|
|||
|
|
|||
|
is_member, member_end_time = result
|
|||
|
|
|||
|
# 检查是否为会员且会员未过期
|
|||
|
if is_member == 1:
|
|||
|
# 将数据库中的时间戳转换为当前时间戳进行比较
|
|||
|
current_timestamp = int(time.time()) # 当前时间戳
|
|||
|
if member_end_time > current_timestamp:
|
|||
|
# 会员未过期,给予积分奖励
|
|||
|
# 假设奖励积分逻辑已经实现
|
|||
|
reward_user_points(openid, REWARD_POINTS)
|
|||
|
return JsonResponse({'code': 200, 'message': '奖励成功', 'data': {'reward_points': REWARD_POINTS}})
|
|||
|
else:
|
|||
|
return JsonResponse({'code': 400, 'message': '会员已过期', 'data': {}})
|
|||
|
else:
|
|||
|
return JsonResponse({'code': 400, 'message': '非会员,无积分奖励', 'data': {}})
|
|||
|
|
|||
|
def reward_user_points(openid, points):
|
|||
|
"""
|
|||
|
给用户奖励积分的逻辑,这里假设直接更新积分字段
|
|||
|
:param openid: 用户的openid
|
|||
|
:param points: 奖励的积分数量
|
|||
|
"""
|
|||
|
with connections['external_db'].cursor() as cursor:
|
|||
|
# 假设 User 表有一个 'points' 字段表示用户的积分
|
|||
|
update_query = """
|
|||
|
UPDATE user
|
|||
|
SET points = points + %s
|
|||
|
WHERE openid = %s
|
|||
|
"""
|
|||
|
cursor.execute(update_query, [points, openid])
|
|||
|
|