Jelajahi Sumber

shopify同步正式服代码

linhaohong 8 bulan lalu
induk
melakukan
0f26795d95
1 mengubah file dengan 100 tambahan dan 33 penghapusan
  1. 100 33
      Controller/ShopifyController.py

+ 100 - 33
Controller/ShopifyController.py

@@ -70,6 +70,21 @@ class ShopifyMultipass:
         # 返回响应的JSON数据
         return response.json()
 
+    @staticmethod
+    def get_timezone_by_country(iso2):
+        timezone_map = {
+            "us": "America/New_York",  # 美国时区(例如:纽约)
+            "jp": "Asia/Tokyo",  # 日本时区
+            "de": "Europe/Berlin",  # 德国时区
+            "uk": "Europe/London",  # 英国时区
+            "eu": "Europe/Paris",  # 欧盟时区(默认设为法国巴黎)
+        }
+        # 获取当前时间并格式化时间戳
+        now = datetime.now(pytz.timezone(timezone_map[iso2]))
+        timestamp = now.strftime('%Y-%m-%dT%H:%M:%S%z')
+        timestamp = timestamp[:-2] + ':' + timestamp[-2:]
+        return timestamp
+
 
 class ShopifyView(View):
     def get(self, request, *args, **kwargs):
@@ -129,56 +144,95 @@ class ShopifyView(View):
         if not check_password(password, users['password']):
             return response.json(111)
 
-        # 获取当前时间并格式化时间戳
-        now = datetime.now(pytz.timezone('America/New_York'))
-        timestamp = now.strftime('%Y-%m-%dT%H:%M:%S%z')
-        timestamp = timestamp[:-2] + ':' + timestamp[-2:]
-
-        customer_data = {
-            "email": email,
-            "created_at": timestamp,
-        }
-
         # 根据条件选择配置键
         if shopify_country:
+            timestamp = ShopifyMultipass.get_timezone_by_country(shopify_country)
             secret_key = f"{shopify_country}_multipass_secret"
-            store_name_key = f"{shopify_country}_store_name"
-        elif account_iso2 == "jp":
-            secret_key = "jp_multipass_secret"
-            store_name_key = "jp_store_name"
-        elif account_iso2 == "de":
-            secret_key = "de_multipass_secret"
-            store_name_key = "de_store_name"
+            multipass_secret = SHOPIFY_CONFIG[secret_key]
+            if shopify_country == "eu":
+                customer_data = {
+                    "email": email,
+                    "created_at": timestamp,
+                }
+                token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+                redirect_url = f"https://eu.zositech.com/account/login/multipass/{token}"
+            elif shopify_country == "uk":
+                customer_data = {
+                    "email": email,
+                    "created_at": timestamp,
+                }
+                token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+                redirect_url = f"https://www.zositech.co.uk/account/login/multipass/{token}"
+            elif shopify_country == "us":
+                customer_data = {
+                    "email": email,
+                    "created_at": timestamp,
+                }
+                token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+                redirect_url = f"https://www.zositech.com/account/login/multipass/{token}"
+            else:
+                customer_data = {
+                    "email": email,
+                    "created_at": timestamp,
+                }
+                token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+                redirect_url = f"https://www.zositech.{shopify_country}/account/login/multipass/{token}"
+            return response.json(0, redirect_url)
+        elif account_iso2 in ["de", "jp"]:
+            timestamp = ShopifyMultipass.get_timezone_by_country(account_iso2)
+            secret_key = f"{account_iso2}_multipass_secret"
+            multipass_secret = SHOPIFY_CONFIG[secret_key]
+            customer_data = {
+                "email": email,
+                "created_at": timestamp,
+            }
+            token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+            redirect_url = f"https://www.zositech.{account_iso2}/account/login/multipass/{token}"
+            return response.json(0, redirect_url)
         elif account_iso2 == "uk":
-            secret_key = "uk_multipass_secret"
-            store_name_key = "uk_store_name"
+            timestamp = ShopifyMultipass.get_timezone_by_country(account_iso2)
+            secret_key = f"{account_iso2}_multipass_secret"
+            multipass_secret = SHOPIFY_CONFIG[secret_key]
+            customer_data = {
+                "email": email,
+                "created_at": timestamp,
+            }
+            token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+            redirect_url = f"https://www.zositech.co.uk/account/login/multipass/{token}"
+            return response.json(0, redirect_url)
         elif CONFIG_INFO == CONFIG_EUR:
+            timestamp = ShopifyMultipass.get_timezone_by_country("eu")
             secret_key = "eu_multipass_secret"
-            store_name_key = "eu_store_name"
+            multipass_secret = SHOPIFY_CONFIG[secret_key]
+            customer_data = {
+                "email": email,
+                "created_at": timestamp,
+            }
+            token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
+            redirect_url = f"https://eu.zositech.com/account/login/multipass/{token}"
+            return response.json(0, redirect_url)
         elif CONFIG_INFO == CONFIG_US:
+            timestamp = ShopifyMultipass.get_timezone_by_country("us")
             secret_key = "us_multipass_secret"
             multipass_secret = SHOPIFY_CONFIG[secret_key]
+            customer_data = {
+                "email": email,
+                "created_at": timestamp,
+            }
             token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
             redirect_url = f"https://www.zositech.com/account/login/multipass/{token}"
             return response.json(0, redirect_url)
         else:
             return response.json(444)
 
-        # 获取配置并生成重定向URL
-        multipass_secret = SHOPIFY_CONFIG[secret_key]
-        store_name = SHOPIFY_CONFIG[store_name_key]
-        token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
-        redirect_url = f"https://{store_name}.zositech.com/account/login/multipass/{token}"
-
-        return response.json(0, redirect_url)
-
     @staticmethod
     def shopify_register(request_dict, response):
         email = request_dict.get("email", None)
+        country_code = request_dict.get("countryCode", None)
         password = request_dict.get("password", None)
         authcode = request_dict.get("authCode", None)
 
-        if not all([email, password, authcode]):
+        if not all([email, password, authcode, country_code]):
             return response.json(444)
         data_valid = DataValid()
         if data_valid.email_validate(email) is not True:
@@ -210,6 +264,12 @@ class ShopifyView(View):
         # 创建用户
         password = make_password(password)
         new_userID = CommonService.getUserID(μs=False, setOTAID=True)
+        country_code = country_code.upper()
+        countr_qs = CountryModel.objects.filter(country_code=country_code).values("id")
+        if countr_qs.exists():
+            region_country = countr_qs[0]['id']
+        else:
+            return response.json(173)
         user_data = {
             "username": email,
             "NickName": email,
@@ -218,6 +278,8 @@ class ShopifyView(View):
             "userID": new_userID,
             "is_active": True,
             "user_isValid": True,
+            "region_country": region_country,
+            "region_status": 1
         }
         Device_User.objects.create(**user_data)
         reds.del_data(key=email + '_identifyingCode')
@@ -328,8 +390,13 @@ class ShopifyView(View):
 
         user_region_id = user_qs.values_list('region_country', flat=True).first()
         country_code = CountryModel.objects.filter(id=user_region_id).values_list("country_code", flat=True).first()
-
-        return response.json(0, country_code)
+        if country_code:
+            return response.json(0, country_code)
+        elif CONFIG_INFO == CONFIG_EUR:
+            return response.json(0, "uk")
+        elif CONFIG_INFO == CONFIG_US:
+            return response.json(0, "us")
+        return response.json(173)
 
     @staticmethod
     def get_country_domain_list(request_dict, response):
@@ -349,10 +416,10 @@ class ShopifyView(View):
             language = lang_qs[0]
             country_qs = CountryLanguageModel.objects.filter(language_id=language.id)
             country_qs = country_qs.annotate(api=F('country__region__zosi_api'), country_code=F('country__country_code'))
-            country_qs = country_qs.values('country_id', 'country_name', 'country_code', 'api').order_by('country_id')
+            country_qs = country_qs.values('country_id', 'country_name', 'country_code', 'api').order_by('country_name')
             country_list = []
             for country in country_qs:
-                country['api'] = country['api'] + 'shopify/shopifyRegister'
+                country['api'] = country['api']
                 country_list.append(country)
             return response.json(0, country_list)
         except Exception as e: