Browse Source

shopify官网检测账号 + 登录

linhaohong 10 months ago
parent
commit
a89cda352d
1 changed files with 84 additions and 43 deletions
  1. 84 43
      Controller/ShopifyController.py

+ 84 - 43
Controller/ShopifyController.py

@@ -86,13 +86,13 @@ class ShopifyView(View):
     def validation(self, request, request_dict, operation):
         language = request_dict.get('language', 'cn')
         response = ResponseObject(language)
-        if operation == 'shopifyLogin':  # APP查詢定制客户信息
+        if operation == 'shopifyLogin':
             return self.shopify_login(request_dict, response)
-        elif operation == 'shopifyRegister':  # APP注册定制客户信息
+        elif operation == 'shopifyRegister':
             return self.shopify_register(request_dict, response)
-        elif operation == 'searchCustomer':
+        elif operation == 'searchCustomer': # 查询APP注册账号情况
             return self.search_customer(request_dict, response)
-        elif operation == 'searchAccount':
+        elif operation == 'searchAccount': # 官网检测账号接口
             return self.search_account(request_dict, response)
         else:
             return response.json(414)
@@ -101,6 +101,9 @@ class ShopifyView(View):
     def shopify_login(request_dict, response):
         email = request_dict.get("email", None)
         password = request_dict.get("password", None)
+        account_region = request_dict.get("accountRegion", "")
+        account_iso2 = request_dict.get("accountIso2", "")
+        shopify_country = request_dict.get("shopifyCountry", "")
 
         if not all([email, password]):
             return response.json(444)
@@ -108,30 +111,51 @@ class ShopifyView(View):
         user_qs = Device_User.objects.filter(Q(username=email) | Q(userEmail=email))
         if not user_qs.exists():
             return response.json(104)
-        users = user_qs.values('role__rid', 'role__roleName', 'userID', 'NickName', 'username', 'userEmail',
-                               'phone', 'password', 'userIconPath')[0]
 
-        check_flag = check_password(password, users['password'])
-        if not check_flag:
-            return response.json(111)
+        users = user_qs.values(
+            'role__rid', 'role__roleName', 'userID', 'NickName',
+            'username', 'userEmail', 'phone', 'password', 'userIconPath'
+        )[0]
 
-        # 获取当前时间
-        now = datetime.now(pytz.timezone('America/New_York'))  # 你可以根据需要更改时区
+        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,
         }
-        multipass_secret = SHOPIFY_CONFIG["eu_multipass_secret"]  # multipass密钥
-        token = ShopifyMultipass.generate_multipass_token(multipass_secret, customer_data)
 
-        # 构造重定向URL
-        redirect_url = f"https://eu.zositech.com/account/login/multipass/{token}"
+        # 定义默认配置键
+        secret_key = "eu_multipass_secret"
+        store_name_key = "eu_store_name"
+
+        # 根据条件选择配置键
+        if shopify_country:
+            secret_key = f"{shopify_country}_multipass_secret"
+            store_name_key = f"{shopify_country}_store_name"
+        elif account_region == "us" and account_iso2 == "jp":
+            secret_key = "jp_multipass_secret"
+            store_name_key = "jp_store_name"
+        elif account_region == "us":
+            secret_key = "us_multipass_secret"
+            store_name_key = "us_store_name"
+        elif account_region == "eu" and account_iso2 == "de":
+            secret_key = "de_multipass_secret"
+            store_name_key = "de_store_name"
+        elif account_region == "eu" and account_iso2 == "uk":
+            secret_key = "uk_multipass_secret"
+            store_name_key = "uk_store_name"
+
+        # 获取配置并生成重定向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)
 
@@ -198,37 +222,54 @@ class ShopifyView(View):
         def search_customer(store_name, token):
             return ShopifyMultipass.search_customer_by_email(store_name, token, email)
 
-        shopify_results = {}
-
-        with concurrent.futures.ThreadPoolExecutor() as executor:
-            future_to_country = {executor.submit(search_customer, store_name, token): country for
-                                 country, store_name, token in store_configs}
-            for future in concurrent.futures.as_completed(future_to_country):
-                country = future_to_country[future]
-                shopify_results[country] = future.result()
-
-        shopify_country = next((country for country, result in shopify_results.items() if result["customers"]), "")
-
-        account_country = self.call_search_customer(email)
-        servers_country = "us" if account_country["us"] else "eu" if account_country["eu"] else ""
-
-        if servers_country and shopify_country:
-            status = 4
-        elif servers_country:
-            status = 3
-        elif shopify_country:
-            status = 2
-        else:
-            status = 1
+        try:
+            shopify_results = {}
+
+            with concurrent.futures.ThreadPoolExecutor() as executor:
+                future_to_country = {executor.submit(search_customer, store_name, token): country for
+                                     country, store_name, token in store_configs}
+                for future in concurrent.futures.as_completed(future_to_country):
+                    country = future_to_country[future]
+                    shopify_results[country] = future.result()
+
+            shopify_country = next((country for country, result in shopify_results.items() if result["customers"]), "")
+
+            account_country = self.call_search_customer(email)
+            servers_continent = "us" if account_country["us"] else "eu" if account_country["eu"] else ""
+
+            if servers_continent:
+                account_region_list = []
+                if account_country.get("us"):
+                    account_region_list.append({
+                        "region": "us",
+                        "url": "https://www.dvema.com/shopify/shopifyLogin",
+                        "accountCountry": account_country["us"],
+                        "shopifyCountry": shopify_country
+                    })
+                if account_country.get("eu"):
+                    account_region_list.append({
+                        "region": "eu",
+                        "url": "https://api.zositeche.com/shopify/shopifyLogin",
+                        "accountCountry": account_country["eu"],
+                        "shopifyCountry": shopify_country
+                    })
+
+                return response.json(0, {"accountStatus": 3, "accountRegionList": account_region_list})
+            elif shopify_country:
+                return response.json(0, {"accountStatus": 2, "url": "官网登录链接"})
+            else:
+                return response.json(0, {"accountStatus": 1, "url": "注册链接"})
+
+        except Exception as e:
+            print(e)
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
-        account_status = {"status": status, "shopifyCountry": shopify_country, "ServersCountry": servers_country}
-        return response.json(0, account_status)
 
     @staticmethod
     def call_search_customer(email):
         urls = {
-            "us": "https://www.dvema.com/shopify/searchCustomer",
-            "eu": "https://api.zositeche.com/shopify/searchCustomer"
+            "us": "https://test.zositechc.cn/shopify/searchCustomer",
+            "eu": "https://test.zositechc.cn/shopify/searchCustomer"
         }
         params = {"email": email}  # Use the provided email parameter