|
|
|
from django.http import JsonResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
import pymysql
|
|
|
|
from pymysql.cursors import DictCursor
|
|
|
|
import redis
|
|
|
|
|
|
|
|
zy_db_config = {
|
|
|
|
'host': '172.16.3.223',
|
|
|
|
'port': 3306,
|
|
|
|
'user': 'zyds_select',
|
|
|
|
'password': 'select',
|
|
|
|
'db': 'suyi_cinema',
|
|
|
|
}
|
|
|
|
|
|
|
|
zy_redis_config = {
|
|
|
|
'host': '172.16.3.223',
|
|
|
|
'port': 6379,
|
|
|
|
'db': 0,
|
|
|
|
'decode_responses': True
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def zy_switch_svip(request):
|
|
|
|
"""
|
|
|
|
接口用于切换自营环境
|
|
|
|
http://172.16.1.168:8000/config/zy_switch_svip?svip=on
|
|
|
|
"""
|
|
|
|
# 切换自营环境
|
|
|
|
svip = request.GET.get('svip')
|
|
|
|
if svip == 'on':
|
|
|
|
result = zy_switch_svip_db('svip')
|
|
|
|
elif svip == 'off':
|
|
|
|
result = zy_switch_svip_db('normal')
|
|
|
|
else:
|
|
|
|
result = False
|
|
|
|
|
|
|
|
# 清除缓存
|
|
|
|
clear_result = False
|
|
|
|
if result:
|
|
|
|
clear_result = zy_clear_cache_redis()
|
|
|
|
|
|
|
|
result_dict = {
|
|
|
|
'status': 'success' if clear_result else 'error',
|
|
|
|
'message': ('切换环境成功,' if result else '切换环境失败,') + ('清除缓存成功' if clear_result else '清除缓存失败'),
|
|
|
|
}
|
|
|
|
return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
def zy_clear_cache(request):
|
|
|
|
"""
|
|
|
|
接口用于清除自营缓存
|
|
|
|
http://172.16.1.168:8000/config/zy_clear_cache
|
|
|
|
"""
|
|
|
|
result = zy_clear_cache_redis()
|
|
|
|
result_dict = {
|
|
|
|
'status': 'success' if result else 'error',
|
|
|
|
'message': '清除缓存成功' if result else '清除缓存失败',
|
|
|
|
}
|
|
|
|
return JsonResponse(result_dict, json_dumps_params={'ensure_ascii': False})
|
|
|
|
|
|
|
|
|
|
|
|
def zy_switch_svip_db(env_type):
|
|
|
|
db_conn = pymysql.Connect(**zy_db_config)
|
|
|
|
cursor = db_conn.cursor(cursor=DictCursor)
|
|
|
|
|
|
|
|
select_sql = "SELECT value FROM suyi_cinema.group_kv_config gkc WHERE gkc.key = 'svip_switch';"
|
|
|
|
cursor.execute(select_sql)
|
|
|
|
query_result = cursor.fetchone()
|
|
|
|
|
|
|
|
# value 0-非华谊 1-华谊
|
|
|
|
sql_str = "UPDATE suyi_cinema.group_kv_config gkc SET gkc.value = %s WHERE gkc.key = 'svip_switch';"
|
|
|
|
if env_type == 'svip':
|
|
|
|
if query_result['value'] == '1':
|
|
|
|
r = 1
|
|
|
|
else:
|
|
|
|
r = cursor.execute(sql_str, ('1',))
|
|
|
|
print('zy_switch_svip_db', r)
|
|
|
|
else:
|
|
|
|
if query_result['value'] == '1':
|
|
|
|
r = cursor.execute(sql_str, ('0',))
|
|
|
|
print('zy_switch_svip_db', r)
|
|
|
|
else:
|
|
|
|
r = 1
|
|
|
|
db_conn.commit()
|
|
|
|
db_conn.close()
|
|
|
|
return True if r == 1 else False
|
|
|
|
|
|
|
|
|
|
|
|
def zy_clear_cache_redis():
|
|
|
|
r = redis.Redis(**zy_redis_config)
|
|
|
|
result = r.flushdb()
|
|
|
|
print('zy_clear_cache_redis', result)
|
|
|
|
return result
|
|
|
|
# Create your views here.
|