|
|
@ -14,7 +14,7 @@ from rest_framework.response import Response |
|
|
|
from api.models import UserDomainInfo, Apps |
|
|
|
from api.models import UserDomainInfo, Apps |
|
|
|
import logging |
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
|
|
from api.utils.storage.caches import set_default_app_wx_easy, del_cache_response_by_short |
|
|
|
from api.utils.storage.caches import del_cache_response_by_short, reset_app_wx_easy_type, reset_short_response_cache |
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
@ -31,6 +31,32 @@ def get_domain_filter(request): |
|
|
|
return filter_dict |
|
|
|
return filter_dict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_domain_wx_easy(app_obj, user_obj): |
|
|
|
|
|
|
|
if app_obj and not get_user_domain_name(user_obj): |
|
|
|
|
|
|
|
app_obj.wxeasytype = True |
|
|
|
|
|
|
|
app_obj.save(update_fields=['wxeasytype']) |
|
|
|
|
|
|
|
del_cache_response_by_short(app_obj.app_id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_new_domain_info(res, request, domain_name, domain_type): |
|
|
|
|
|
|
|
min_domain_cname_info_obj = get_min_default_domain_cname_obj(False) |
|
|
|
|
|
|
|
if min_domain_cname_info_obj: |
|
|
|
|
|
|
|
res.data = {'cname_domain': min_domain_cname_info_obj.domain_record} |
|
|
|
|
|
|
|
data_dict = { |
|
|
|
|
|
|
|
'user_id': request.user, |
|
|
|
|
|
|
|
'cname_id': min_domain_cname_info_obj, |
|
|
|
|
|
|
|
'domain_name': domain_name, |
|
|
|
|
|
|
|
'app_id': None, |
|
|
|
|
|
|
|
'domain_type': domain_type |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
app_id = request.data.get("app_id", None) |
|
|
|
|
|
|
|
if app_id: |
|
|
|
|
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
|
|
|
|
if app_obj: |
|
|
|
|
|
|
|
data_dict['app_id'] = app_obj |
|
|
|
|
|
|
|
UserDomainInfo.objects.create(**data_dict) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DomainCnameView(APIView): |
|
|
|
class DomainCnameView(APIView): |
|
|
|
authentication_classes = [ExpiringTokenAuthentication, ] |
|
|
|
authentication_classes = [ExpiringTokenAuthentication, ] |
|
|
|
|
|
|
|
|
|
|
@ -49,6 +75,7 @@ class DomainCnameView(APIView): |
|
|
|
res = BaseResponse() |
|
|
|
res = BaseResponse() |
|
|
|
domain_name = request.data.get("domain_name", None) |
|
|
|
domain_name = request.data.get("domain_name", None) |
|
|
|
domain_type = request.data.get('domain_type', 1) |
|
|
|
domain_type = request.data.get('domain_type', 1) |
|
|
|
|
|
|
|
force_bind = request.data.get('force_bind', 0) |
|
|
|
if domain_type not in [x[0] for x in list(UserDomainInfo.domain_type_choices)]: |
|
|
|
if domain_type not in [x[0] for x in list(UserDomainInfo.domain_type_choices)]: |
|
|
|
res.code = 1001 |
|
|
|
res.code = 1001 |
|
|
|
res.msg = "绑定失败" |
|
|
|
res.msg = "绑定失败" |
|
|
@ -56,33 +83,37 @@ class DomainCnameView(APIView): |
|
|
|
if domain_name: |
|
|
|
if domain_name: |
|
|
|
domain_name = domain_name.strip(" ") |
|
|
|
domain_name = domain_name.strip(" ") |
|
|
|
if domain_name and len(domain_name) > 3 and is_valid_domain(domain_name): |
|
|
|
if domain_name and len(domain_name) > 3 and is_valid_domain(domain_name): |
|
|
|
if UserDomainInfo.objects.filter(domain_name=domain_name, is_enable=True, |
|
|
|
filter_dict = get_domain_filter(request) |
|
|
|
domain_type=domain_type).count() != 0: |
|
|
|
if not force_bind: |
|
|
|
res.code = 1001 |
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(domain_name=domain_name, is_enable=True).first() |
|
|
|
res.msg = "该域名已经被绑定,请更换其他域名" |
|
|
|
if user_domain_obj: |
|
|
|
|
|
|
|
res.code = 1011 |
|
|
|
|
|
|
|
if user_domain_obj.user_id.uid == request.user.uid: |
|
|
|
|
|
|
|
app_obj = user_domain_obj.app_id |
|
|
|
|
|
|
|
if app_obj: |
|
|
|
|
|
|
|
res.msg = f"该域名已经被应用 {app_obj.name} 绑定" |
|
|
|
|
|
|
|
elif user_domain_obj.domain_type == 0: |
|
|
|
|
|
|
|
res.msg = f"该域名已经被 应用下载码绑定" |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
res.msg = f"该域名已经被 预览下载页绑定" |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
res.msg = "该域名已经被其他用户绑定" |
|
|
|
|
|
|
|
res.msg += ",请更换要绑定的域名,或者进行强制绑定域名" |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request), |
|
|
|
|
|
|
|
domain_name=domain_name).first() |
|
|
|
|
|
|
|
if user_domain_obj: |
|
|
|
|
|
|
|
res.data = {'cname_domain': user_domain_obj.cname_id.domain_record} |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
UserDomainInfo.objects.filter(**filter_dict, is_enable=False).delete() |
|
|
|
|
|
|
|
add_new_domain_info(res, request, domain_name, domain_type) |
|
|
|
else: |
|
|
|
else: |
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request), |
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**filter_dict, |
|
|
|
domain_name=domain_name).first() |
|
|
|
domain_name=domain_name).first() |
|
|
|
if user_domain_obj: |
|
|
|
if user_domain_obj: |
|
|
|
res.data = {'cname_domain': user_domain_obj.cname_id.domain_record} |
|
|
|
res.data = {'cname_domain': user_domain_obj.cname_id.domain_record} |
|
|
|
else: |
|
|
|
else: |
|
|
|
UserDomainInfo.objects.filter(**get_domain_filter(request), is_enable=False).delete() |
|
|
|
add_new_domain_info(res, request, domain_name, domain_type) |
|
|
|
min_domain_cname_info_obj = get_min_default_domain_cname_obj(False) |
|
|
|
|
|
|
|
if min_domain_cname_info_obj: |
|
|
|
|
|
|
|
res.data = {'cname_domain': min_domain_cname_info_obj.domain_record} |
|
|
|
|
|
|
|
data_dict = { |
|
|
|
|
|
|
|
'user_id': request.user, |
|
|
|
|
|
|
|
'cname_id': min_domain_cname_info_obj, |
|
|
|
|
|
|
|
'domain_name': domain_name, |
|
|
|
|
|
|
|
'app_id': None, |
|
|
|
|
|
|
|
'domain_type': domain_type |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
app_id = request.data.get("app_id", None) |
|
|
|
|
|
|
|
if app_id: |
|
|
|
|
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
|
|
|
|
if app_obj: |
|
|
|
|
|
|
|
data_dict['app_id'] = app_obj |
|
|
|
|
|
|
|
UserDomainInfo.objects.create(**data_dict) |
|
|
|
|
|
|
|
else: |
|
|
|
else: |
|
|
|
res.code = 1002 |
|
|
|
res.code = 1002 |
|
|
|
res.msg = "该域名校验失败,请检查" |
|
|
|
res.msg = "该域名校验失败,请检查" |
|
|
@ -94,19 +125,26 @@ class DomainCnameView(APIView): |
|
|
|
if user_domain_obj: |
|
|
|
if user_domain_obj: |
|
|
|
cname = get_cname_from_domain(user_domain_obj.domain_name) |
|
|
|
cname = get_cname_from_domain(user_domain_obj.domain_name) |
|
|
|
if cname == user_domain_obj.cname_id.domain_record + '.': |
|
|
|
if cname == user_domain_obj.cname_id.domain_record + '.': |
|
|
|
user_domain_obj.is_enable = True |
|
|
|
user_domain_obj_list = UserDomainInfo.objects.filter(domain_name=user_domain_obj.domain_name, |
|
|
|
user_domain_obj.save(update_fields=["is_enable"]) |
|
|
|
is_enable=True).all() |
|
|
|
UserDomainInfo.objects.filter(domain_name=user_domain_obj.domain_name, is_enable=False, |
|
|
|
if len(user_domain_obj_list) < 2: |
|
|
|
domain_type=user_domain_obj.domain_type).delete() |
|
|
|
if len(user_domain_obj_list) == 1: |
|
|
|
app_id = request.data.get("app_id", None) |
|
|
|
user_obj = user_domain_obj_list.first().user_id |
|
|
|
if app_id: |
|
|
|
app_obj = user_domain_obj_list.first().app_id |
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
user_domain_obj_list.delete() |
|
|
|
if app_obj: |
|
|
|
reset_app_wx_easy_type(user_obj, app_obj) |
|
|
|
app_obj.wxeasytype = False |
|
|
|
|
|
|
|
app_obj.save(update_fields=['wxeasytype']) |
|
|
|
user_domain_obj.is_enable = True |
|
|
|
del_cache_response_by_short(app_obj.app_id) |
|
|
|
user_domain_obj.save(update_fields=["is_enable"]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app_id = request.data.get("app_id", None) |
|
|
|
|
|
|
|
app_obj = None |
|
|
|
|
|
|
|
if app_id: |
|
|
|
|
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
|
|
|
|
reset_app_wx_easy_type(request.user, app_obj) |
|
|
|
else: |
|
|
|
else: |
|
|
|
set_default_app_wx_easy(request.user, True) |
|
|
|
res.code = 1002 |
|
|
|
|
|
|
|
res.msg = "该域名查询校验失败,请检查" |
|
|
|
else: |
|
|
|
else: |
|
|
|
res.code = 1003 |
|
|
|
res.code = 1003 |
|
|
|
res.msg = "系统未检出到您的CNAME记录" |
|
|
|
res.msg = "系统未检出到您的CNAME记录" |
|
|
@ -117,14 +155,12 @@ class DomainCnameView(APIView): |
|
|
|
|
|
|
|
|
|
|
|
def delete(sele, request): |
|
|
|
def delete(sele, request): |
|
|
|
res = BaseResponse() |
|
|
|
res = BaseResponse() |
|
|
|
if UserDomainInfo.objects.filter(**get_domain_filter(request)).delete()[0]: |
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request)).first() |
|
|
|
|
|
|
|
if user_domain_obj: |
|
|
|
app_id = request.query_params.get("app_id", None) |
|
|
|
app_id = request.query_params.get("app_id", None) |
|
|
|
|
|
|
|
app_obj = None |
|
|
|
if app_id: |
|
|
|
if app_id: |
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
if app_obj and not get_user_domain_name(request.user): |
|
|
|
reset_app_wx_easy_type(request.user, app_obj) |
|
|
|
app_obj.wxeasytype = True |
|
|
|
user_domain_obj.delete() |
|
|
|
app_obj.save(update_fields=['wxeasytype']) |
|
|
|
|
|
|
|
del_cache_response_by_short(app_obj.app_id) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
set_default_app_wx_easy(request.user) |
|
|
|
|
|
|
|
return Response(res.dict) |
|
|
|
return Response(res.dict) |
|
|
|