|
@@ -6,11 +6,15 @@
|
|
|
@Email : zhangdongming@asj6.wecom.work
|
|
|
@Software: PyCharm
|
|
|
"""
|
|
|
+import threading
|
|
|
+import time
|
|
|
+
|
|
|
from django.http import QueryDict
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import Device_User
|
|
|
+from Model.models import Device_User, CountryModel, UserEmailSubscriptions
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
+from Object.TokenObject import TokenObject
|
|
|
from Object.YotpoCoreObject import YotpoCoreObject
|
|
|
from Ansjer.config import LOGGER
|
|
|
|
|
@@ -42,31 +46,141 @@ class UserSubscriptionControllerView(View):
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
response = ResponseObject('cn')
|
|
|
- if operation == 'subscription':
|
|
|
- return self.subscription(request_dict, response)
|
|
|
+ tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
+ if tko.code != 0:
|
|
|
+ return response.json(tko.code)
|
|
|
+ response.lang = tko.lang
|
|
|
+ userID = tko.userID
|
|
|
+ if operation == 'checkSubscriptionStatus':
|
|
|
+ return self.check_subscription_status(userID, response)
|
|
|
+ elif operation == 'switchSubscription':
|
|
|
+ return self.switch_subscription(userID, request_dict, response)
|
|
|
+ else:
|
|
|
+ return response.json(414)
|
|
|
|
|
|
@staticmethod
|
|
|
- def subscription(request_dict, response):
|
|
|
- user_id = request_dict.get('userId', '')
|
|
|
- if not user_id:
|
|
|
- return response.json(444)
|
|
|
- user_qs = Device_User.objects.filter(userID=user_id).values('username', 'userEmail')
|
|
|
+ def check_subscription_status(user_id, response):
|
|
|
+ """
|
|
|
+ 检查订阅状态
|
|
|
+ @param user_id: str
|
|
|
+ @param response: 响应
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ user_qs = Device_User.objects.filter(userID=user_id)
|
|
|
+ if not user_qs.exists():
|
|
|
+ return response.json(104)
|
|
|
+ user_sub = UserEmailSubscriptions.objects.filter(user_id=user_id).values('status').first()
|
|
|
+ if not user_sub:
|
|
|
+ return response.json(0, {"status": 0})
|
|
|
+ user_sub = {"status": user_sub['status']}
|
|
|
+ return response.json(0, user_sub)
|
|
|
+
|
|
|
+ def switch_subscription(self, user_id, request_dict, response):
|
|
|
+ """
|
|
|
+ 订阅开关
|
|
|
+ @param user_id: str
|
|
|
+ @param request_dict: dict
|
|
|
+ @param response
|
|
|
+ """
|
|
|
+ user_qs = Device_User.objects.filter(userID=user_id)
|
|
|
+ status = request_dict.get('status', None)
|
|
|
+ if not status:
|
|
|
+ return response.json(444, "status")
|
|
|
if not user_qs.exists():
|
|
|
- return response.json(703)
|
|
|
+ return response.json(104)
|
|
|
+
|
|
|
+ # 修改数据库中订阅状态
|
|
|
+ status = int(status)
|
|
|
+ user_sub_qs = UserEmailSubscriptions.objects.filter(user_id=user_id)
|
|
|
+ # 订阅
|
|
|
+ if status == 1:
|
|
|
+ subscribers = Device_User.objects.filter(userID=user_id).values('NickName', 'userEmail',
|
|
|
+ 'region_country').first()
|
|
|
+ if not subscribers["userEmail"]:
|
|
|
+ LOGGER.info(f'subscribers{user_id}邮箱为空,无法订阅')
|
|
|
+ return response.json(183)
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ user_sub_qs.update(status=1, updated_time=int(time.time()))
|
|
|
+ else:
|
|
|
+ UserEmailSubscriptions.objects.create(user_id=user_id, status=1, email=subscribers["userEmail"],
|
|
|
+ created_time=int(time.time()), updated_time=int(time.time()))
|
|
|
+ subscription_thread = threading.Thread(target=self.subscription, args=(subscribers,))
|
|
|
+ subscription_thread.start()
|
|
|
+ # 取消订阅
|
|
|
+ else:
|
|
|
+ device_user = Device_User.objects.filter(userID=user_id).values('userEmail').first()
|
|
|
+ if device_user:
|
|
|
+ customer = {
|
|
|
+ "email": device_user["userEmail"],
|
|
|
+ "first_name": device_user["userEmail"],
|
|
|
+ "last_name": "APP",
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ return response.json(0, {"status": status})
|
|
|
+ yotpo = YotpoCoreObject()
|
|
|
+ list_id = 8589406
|
|
|
+ subscription_thread = threading.Thread(target=yotpo.close_subscribers, args=(customer, list_id))
|
|
|
+ subscription_thread.start()
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ customer["status"] = "unsubscription"
|
|
|
+ user_sub_qs.update(status=0, sub_result=customer)
|
|
|
+ return response.json(0, {"status": status})
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def subscription(subscribers):
|
|
|
+ """
|
|
|
+ 订阅
|
|
|
+ @param subscribers: dict
|
|
|
+ @return: boolean
|
|
|
+ """
|
|
|
|
|
|
yotpo = YotpoCoreObject()
|
|
|
|
|
|
- email = '1920098158@qq.com'
|
|
|
- customer = {
|
|
|
- "email": email, "first_name": "test", "last_name": "22",
|
|
|
- 'address': {
|
|
|
- "country_code": 'GB',
|
|
|
- }
|
|
|
- }
|
|
|
+ try:
|
|
|
+ # 查询顾客所在地区
|
|
|
+ if subscribers["region_country"]:
|
|
|
+ country = CountryModel.objects.filter(id=subscribers["region_country"]).values('country_code').first()
|
|
|
+ if country:
|
|
|
+ country_code = country["country_code"]
|
|
|
+ else:
|
|
|
+ country_code = ''
|
|
|
+ # 构建顾客订阅格式
|
|
|
+ customer = {
|
|
|
+ "email": subscribers["userEmail"],
|
|
|
+ "first_name": subscribers["userEmail"],
|
|
|
+ "last_name": "APP",
|
|
|
+ 'address': {
|
|
|
+ "country_code": country_code,
|
|
|
+ },
|
|
|
+ "custom_properties": {
|
|
|
+ "subscription_office": "ZosiApp",
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ customer = {
|
|
|
+ "email": subscribers["userEmail"],
|
|
|
+ "first_name": subscribers["userEmail"],
|
|
|
+ "last_name": "APP",
|
|
|
+ "custom_properties": {
|
|
|
+ "subscription_office": "ZosiApp",
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- result = yotpo.creat_and_update_customers(customer)
|
|
|
+ result = yotpo.creat_and_update_customers(customer)
|
|
|
+ list_id = 8589406
|
|
|
+ sub_status, sub_result = yotpo.create_subscribers(customer, list_id)
|
|
|
|
|
|
- list_id = 8589406
|
|
|
- subscribers_result = yotpo.create_subscribers(customer, list_id)
|
|
|
- print()
|
|
|
- LOGGER.info(f'创建客户:{result},订阅:{subscribers_result}')
|
|
|
+ if result and sub_status:
|
|
|
+ # 创建结果写入数据库
|
|
|
+ user_sub_qs = UserEmailSubscriptions.objects.filter(email=subscribers["userEmail"])
|
|
|
+ if user_sub_qs.exists():
|
|
|
+ user_sub_qs.update(email=subscribers["userEmail"], status=1, sub_result=sub_result, list_id=list_id,
|
|
|
+ updated_time=int(time.time()))
|
|
|
+ LOGGER.info(f'在yotpo创建客户并订阅成功,customer:{customer}')
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ LOGGER.info(f'在yotpo创建客户并订阅失败,customer:{customer}')
|
|
|
+ return False
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error(f'{subscribers["userEmail"]}订阅失败:{e}')
|
|
|
+ return False
|