140 lines
5.2 KiB
Plaintext
140 lines
5.2 KiB
Plaintext
async def get_user_role(ns={}):
|
|
sor = ns['sor']
|
|
# get role
|
|
ns['del_flg'] = '0'
|
|
res_role = await sor.R('userrole', ns)
|
|
if res_role:
|
|
user_role = res_role[0]
|
|
else:
|
|
return {
|
|
"status": False,
|
|
"msg": "userrole table, user id can not find..."
|
|
}
|
|
roleid = user_role.get('roleid')
|
|
# get role name
|
|
role_name = await sor.R('role', {'id': roleid})
|
|
if role_name:
|
|
role = role_name[0].get('role')
|
|
else:
|
|
return {
|
|
"status": False,
|
|
"msg": "role table, can not get role name"
|
|
}
|
|
|
|
return role
|
|
|
|
async def user_browse_history_add(ns={}):
|
|
# 必要参数校验
|
|
if not ns.get('userid') or not ns.get('productid'):
|
|
return {
|
|
'status': False,
|
|
'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)
|
|
|
|
# 检查产品的publish_type
|
|
product_sql = """
|
|
SELECT publish_type FROM user_publish_product
|
|
WHERE id = '%s' AND del_flg = '0';
|
|
""" % ns.get('productid')
|
|
product_result = await sor.sqlExe(product_sql, {})
|
|
if product_result:
|
|
publish_type = product_result[0]['publish_type']
|
|
else:
|
|
publish_type = 99
|
|
|
|
check_sql = """
|
|
SELECT * FROM user_browse_history
|
|
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': 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, productid, ip_address, user_agent, del_flg, tag, publish_type
|
|
) VALUES ('%s', '%s', '%s', '%s', '%s', '0', '%s', '%s');
|
|
""" % (record_id, ns.get('userid'), ns.get('productid'),
|
|
ns.get('ip_address', ''), ns.get('user_agent', ''), ns.get('tag', ''), publish_type)
|
|
await sor.sqlExe(insert_sql, {})
|
|
return {
|
|
'status': True,
|
|
'msg': 'Browse history recorded successfully',
|
|
'data': {'record_id': record_id}
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'status': False,
|
|
'msg': 'Failed to record browse history, %s' % str(e)
|
|
}
|
|
|
|
async def publish_product_search_detail(ns={}):
|
|
"""
|
|
:param ns:
|
|
:return:
|
|
"""
|
|
if ns.get('userid'):
|
|
userid = ns.get('userid')
|
|
else:
|
|
userid = await get_user()
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
user_role = None
|
|
orgid = None
|
|
# 区分运营和普通客户
|
|
if userid:
|
|
user_list = await sor.R('users', {'id': userid})
|
|
orgid = user_list[0]['orgid']
|
|
user_role = await get_user_role({'userid': userid, 'sor': sor})
|
|
|
|
# 如果用户角色是运营并且from='b' product_list中的phone_number和email做加星号处理
|
|
product_list = await sor.R('user_publish_product', {'id': ns.get('id'), 'del_flg': '0'})
|
|
product_category = await sor.R('user_publish_product_category', {'id': product_list[0]['product_category'].split(',')[0]})
|
|
product_list[0]['product_category'] = product_category[0]['product_category']
|
|
if user_role == '运营' and ns.get('from') == 'b':
|
|
pass
|
|
elif orgid == product_list[0]['orgid']:
|
|
pass
|
|
else:
|
|
for product in product_list:
|
|
if product.get('phone_number'):
|
|
product['phone_number'] = product['phone_number'][:3] + '****' + product['phone_number'][7:]
|
|
else:
|
|
product['phone_number'] = '198****8601'
|
|
if product.get('email'):
|
|
product['email'] = product['email'][:2] + '****' + product['email'][6:]
|
|
else:
|
|
product['email'] = None
|
|
|
|
# 保存浏览记录
|
|
if userid:
|
|
res = await user_browse_history_add({'userid': userid, 'productid': ns.get('id'), 'tag': 'upp', 'publish_type': product_list[0]['publish_type']})
|
|
|
|
return {
|
|
'status': True,
|
|
'msg': 'Product retrieved successfully',
|
|
'data': product_list[0]
|
|
}
|
|
|
|
ret = await publish_product_search_detail(params_kw)
|
|
return ret |