This commit is contained in:
ping 2025-08-22 11:27:17 +08:00
parent 2c18f68f2e
commit 31c8f26c44
7 changed files with 298 additions and 43 deletions

View File

@ -26,31 +26,39 @@ async def get_user_role(ns={}):
async def user_browse_history_add(ns={}):
# ns = {
# 'userid': '9KVhsVCJsW_29q3hRhMAr', # 用户ID
# 'product_id': 'p2s2YPPU7uquza3gGw9k2', # 产品ID
# 'productid': 'p2s2YPPU7uquza3gGw9k2', # 产品ID
# 'ip_address': '192.168.1.1', # IP地址
# 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4758.102' # 用户代理
# }
# 必要参数校验
if not ns.get('userid') or not ns.get('product_id'):
if not ns.get('userid') or not ns.get('productid'):
return {
'status': False,
'msg': 'userid and product_id are required'
'msg': 'userid and productid are required'
}
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 根据timestamp时间字段:browse_time去重 查找10个小时内是否存在
browse_time = datetime.datetime.now() - datetime.timedelta(hours=10)
# browse_time = datetime.datetime.now() - datetime.timedelta(hours=10)
check_sql = """
SELECT * FROM user_browse_history
WHERE userid = '%s' AND product_id = '%s' AND del_flg = '0' AND browse_time >= '%s';
""" % (ns.get('userid'), ns.get('product_id'), browse_time)
WHERE userid = '%s' AND productid = '%s' AND del_flg = '0';
""" % (ns.get('userid'), ns.get('productid'))
check_result = await sor.sqlExe(check_sql, {})
if check_result:
# 数据库更新browse_time字段
update_sql = """
UPDATE user_browse_history
SET browse_time = '%s'
WHERE id = '%s';
""" % (datetime.datetime.now(), check_result[0]['id'])
await sor.sqlExe(update_sql, {})
return {
'status': False,
'msg': 'The user has browsed this product within the last 10 hours'
'status': True,
'msg': 'Browse history recorded successfully',
'data': {'record_id': check_result[0]['id']}
}
# 生成记录ID
@ -58,10 +66,10 @@ async def user_browse_history_add(ns={}):
# 插入浏览记录
insert_sql = """
INSERT INTO user_browse_history (
id, userid, product_id, ip_address, user_agent, del_flg
) VALUES ('%s', '%s', '%s', '%s', '%s', '0');
""" % (record_id, ns.get('userid'), ns.get('product_id'),
ns.get('ip_address', ''), ns.get('user_agent', ''))
id, userid, productid, ip_address, user_agent, del_flg, tag
) VALUES ('%s', '%s', '%s', '%s', '%s', '0', '%s');
""" % (record_id, ns.get('userid'), ns.get('productid'),
ns.get('ip_address', ''), ns.get('user_agent', ''), ns.get('tag'))
await sor.sqlExe(insert_sql, {})
return {
'status': True,
@ -115,7 +123,7 @@ async def publish_product_search_detail(ns={}):
# 保存浏览记录
if userid:
await user_browse_history_add({'userid': userid, 'product_id': ns.get('id')})
await user_browse_history_add({'userid': userid, 'productid': ns.get('id'), 'tag': 'upp'})
return {
'status': True,

View File

@ -1,3 +1,43 @@
async def favorite_check(ns={}):
"""
检查用户是否已收藏某个商品/需求
:param ns: 包含userid, productid, favorite_type参数
:return: 检查结果
"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
if not ns.get('userid') or not ns.get('productid'):
return {
'status': False,
'msg': '缺少必要参数'
}
check_sql = """
SELECT id FROM user_favorite
WHERE userid = '%s' AND productid = '%s' AND del_flg = '0'
""" % (ns.get('userid'), ns.get('productid'))
check_result = await sor.sqlExe(check_sql, {})
if check_result:
return {
'status': True,
'msg': '查询成功',
'data': {
'is_favorite': True,
'id': check_result[0]['id']
}
}
else:
return {
'status': False
}
except Exception as e:
return {
'status': False,
'msg': '查询失败, %s' % str(e)
}
async def publish_product_search_first_page(ns={}):
"""
普通客户查看
@ -6,6 +46,12 @@ async def publish_product_search_first_page(ns={}):
:param ns:
:return:
"""
# 处理userid
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = await get_user()
publish_type = ns.get('publish_type')
page_size = int(ns['page_size']) if ns.get('page_size') else 8
current_page_param = int(ns['current_page']) if ns.get('current_page') else 1
@ -52,6 +98,13 @@ async def publish_product_search_first_page(ns={}):
res['img'] = 'https://' + domain_name + '/idfile?path=' + res['img']
else:
res['img'] = None
# 是否收藏关注
check_status_li = await favorite_check({'userid': ns.get('userid'), 'productid': res.get('id')})
if check_status_li['status']:
res['favorite'] = '1'
else:
res['favorite'] = '0'
# 电话和邮箱模糊化处理
if res.get('phone_number'):

55
b/user/favorite_add.dspy Normal file
View File

@ -0,0 +1,55 @@
async def favorite_add(ns={}):
"""
添加用户收藏
:param ns: 包含userid, productid, favorite_type等参数
:return: 操作结果
"""
ns_dic = {
'id': uuid(), # 生成32位ID
'userid': ns.get('userid'),
'productid': ns.get('productid'),
'favorite_type': ns.get('favorite_type', '1'), # 默认为商品收藏
'tag': ns.get('tag') # 标签
}
# 处理userid
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = None
if not userid:
server_error(401)
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 检查是否已收藏
check_sql = """
SELECT id FROM user_favorite
WHERE userid = '%s' AND productid = '%s' AND favorite_type = '%s' AND del_flg = '0' AND tag = '%s'
""" % (ns_dic['userid'], ns_dic['productid'], ns_dic['favorite_type'], ns_dic['tag'])
check_result = await sor.sqlExe(check_sql, {})
if check_result:
return {
'status': False,
'msg': '已收藏关注'
}
# 执行收藏操作
await sor.C('user_favorite', ns_dic)
return {
'status': True,
'msg': '关注收藏成功'
}
except Exception as e:
await sor.rollback()
return {
'status': False,
'msg': '关注收藏失败, %s' % str(e)
}
ret = await favorite_add(params_kw)
return ret

View File

@ -0,0 +1,43 @@
async def favorite_delete(ns={}):
"""
删除用户收藏(软删除)
:param ns: 包含id或userid+productid+favorite_type参数
:return: 操作结果
"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
if ns.get('id'):
# 根据收藏ID删除
update_dic = {
'id': ns.get('id'),
'del_flg': '1'
}
await sor.U('user_favorite', update_dic)
elif ns.get('userid') and ns.get('productid'):
# 根据用户ID和产品ID删除
update_sql = """
UPDATE user_favorite
SET del_flg = '1'
WHERE userid = '%s' AND productid = '%s' AND favorite_type = '%s' AND del_flg = '0'
""" % (ns.get('userid'), ns.get('productid'), ns.get('favorite_type', 'product'))
await sor.execute(update_sql)
else:
return {
'status': False,
'msg': '缺少必要参数'
}
return {
'status': True,
'msg': '取消关注收藏成功'
}
except Exception as e:
await sor.rollback()
return {
'status': False,
'msg': '取消关注收藏失败, %s' % str(e)
}
ret = await favorite_delete(params_kw)
return ret

View File

@ -0,0 +1,84 @@
async def favorite_search(ns={}):
"""
查询用户收藏列表
:param ns: 包含userid, favorite_type等查询参数
:return: 查询结果
"""
# 处理userid
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = None
if not userid:
server_error(401)
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 分页参数
current_page = int(ns.get('current_page', 1))
page_size = int(ns.get('page_size', 1000))
offset = (current_page - 1) * page_size
# 构建查询条件
where_conditions = ["f.del_flg = '0'"]
if ns.get('userid'):
where_conditions.append("f.userid = '%s'" % ns.get('userid'))
if ns.get('favorite_type'):
where_conditions.append("f.favorite_type = '%s'" % ns.get('favorite_type'))
where_clause = " AND ".join(where_conditions)
# 查询总数
count_sql = """
SELECT COUNT(*) AS total_count
FROM user_favorite f
WHERE %s
""" % where_clause
count_result = await sor.sqlExe(count_sql, {})
total_count = count_result[0]['total_count'] if count_result else 0
# 查询数据
query_sql = """
SELECT *
FROM user_favorite f
WHERE %s
ORDER BY f.create_at DESC
LIMIT %s OFFSET %s
""" % (where_clause, page_size, offset)
result = await sor.sqlExe(query_sql, {})
# 通过查询数据中的productid, 查询product表, 增加product_info
for product in result:
product_sql = f"""
SELECT * FROM user_publish_product
WHERE id = '{product['productid']}' AND del_flg = '0' AND listing_status = 'listing';
"""
product_info_li = await sor.sqlExe(product_sql, {})
if product_info_li:
product['product_info'] = product_info_li[0]
# 手机号加*
if product['product_info'].get('phone_number'):
product['product_info']['phone_number'] = '**************'
else:
product['product_info'] = None
return {
'status': True,
'msg': '查询成功',
'data': {
'total_count': total_count,
'current_page': current_page,
'page_size': page_size,
'favorites': result
}
}
except Exception as e:
return {
'status': False,
'msg': '查询失败, %s' % str(e)
}
ret = await favorite_search(params_kw)
return ret

View File

@ -1,42 +1,43 @@
async def user_browse_history_add(ns={}):
# ns = {
# 'userid': '9KVhsVCJsW_29q3hRhMAr', # 用户ID
# 'product_id': 'p2s2YPPU7uquza3gGw9k2', # 产品ID
# 'ip_address': '192.168.1.1', # IP地址
# 'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4758.102' # 用户代理
# }
# 必要参数校验
if not ns.get('userid') or not ns.get('product_id'):
if not ns.get('userid') or not ns.get('productid'):
return {
'status': False,
'msg': 'userid and product_id are required'
'msg': 'userid and productid are required'
}
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 根据timestamp时间字段:browse_time去重 查找10个小时内是否存在
browse_time = datetime.datetime.now() - datetime.timedelta(hours=10)
# browse_time = datetime.datetime.now() - datetime.timedelta(hours=10)
check_sql = """
SELECT * FROM user_browse_history
WHERE userid = '%s' AND product_id = '%s' AND del_flg = '0' AND browse_time >= '%s';
""" % (ns.get('userid'), ns.get('product_id'), browse_time)
WHERE userid = '%s' AND productid = '%s' AND del_flg = '0';
""" % (ns.get('userid'), ns.get('productid'))
check_result = await sor.sqlExe(check_sql, {})
if check_result:
# 数据库更新browse_time字段
update_sql = """
UPDATE user_browse_history
SET browse_time = '%s'
WHERE id = '%s';
""" % (datetime.datetime.now(), check_result[0]['id'])
await sor.sqlExe(update_sql, {})
return {
'status': False,
'msg': 'The user has browsed this product within the last 10 hours'
'status': True,
'msg': 'Browse history recorded successfully',
'data': {'record_id': check_result[0]['id']}
}
# 生成记录ID
record_id = uuid()
# 插入浏览记录
insert_sql = """
INSERT INTO user_browse_history (
id, userid, product_id, ip_address, user_agent, del_flg
) VALUES ('%s', '%s', '%s', '%s', '%s', '0');
""" % (record_id, ns.get('userid'), ns.get('product_id'),
ns.get('ip_address', ''), ns.get('user_agent', ''))
id, userid, productid, ip_address, user_agent, del_flg, tag
) VALUES ('%s', '%s', '%s', '%s', '%s', '0', '%s');
""" % (record_id, ns.get('userid'), ns.get('productid'),
ns.get('ip_address', ''), ns.get('user_agent', ''), ns.get('tag', ''))
await sor.sqlExe(insert_sql, {})
return {
'status': True,

View File

@ -1,20 +1,17 @@
async def user_browse_history_search(ns={}):
async def user_browse_history_search(ns={}):
# 处理userid
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = await get_user()
userid = None
if not userid:
return {
'status': False,
'msg': 'no match user'
}
server_error(401)
# 参数处理
product_id = ns.get('product_id')
productid = ns.get('productid')
start_time = ns.get('start_time')
end_time = ns.get('end_time')
page_size = int(ns.get('page_size', 10))
page_size = int(ns.get('page_size', 1000))
current_page = int(ns.get('current_page', 1))
offset = (current_page - 1) * page_size
@ -25,8 +22,8 @@ async def user_browse_history_search(ns={}):
base_conditions = ["del_flg = '0'"]
if userid:
base_conditions.append(f"userid = '{userid}'")
if product_id:
base_conditions.append(f"product_id = '{product_id}'")
if productid:
base_conditions.append(f"productid = '{productid}'")
if start_time and end_time:
end_time += ' 23:59:59'
base_conditions.append(f"browse_time BETWEEN '{start_time}' AND '{end_time}'")
@ -34,8 +31,6 @@ async def user_browse_history_search(ns={}):
# 构建查询SQL
where_clause = " AND ".join(base_conditions)
# 根据product_id查询product_info
find_sql = f"""
SELECT * FROM user_browse_history
WHERE {where_clause}
@ -51,6 +46,22 @@ async def user_browse_history_search(ns={}):
# 执行查询
result = await sor.sqlExe(find_sql, {})
total_count = (await sor.sqlExe(count_sql, {}))[0]['total_count']
# 根据result中productid查询product_info
for product in result:
# 查询product_info
product_sql = f"""
SELECT * FROM user_publish_product
WHERE id = '{product['productid']}' AND del_flg = '0' AND listing_status = 'listing';
"""
product_info_li = await sor.sqlExe(product_sql, {})
if product_info_li:
product['product_info'] = product_info_li[0]
if product['product_info'].get('phone_number'):
product['product_info']['phone_number'] = '***************'
else:
product['product_info'] = None
return {
'status': True,