parent
							
								
									d15b583804
								
							
						
					
					
						commit
						fc1963c8a9
					
				| @ -0,0 +1,65 @@ | ||||
| export function GetRandomNum(Min, Max) { | ||||
|     const Range = Max - Min; | ||||
|     const Rand = Math.random(); | ||||
|     return (Min + Math.round(Rand * Range)); | ||||
| } | ||||
| 
 | ||||
| export function getRandomStr(str_length = 32) { | ||||
|     const SIGNING_v1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'//36
 | ||||
|     // const SIGNING_v2 ='ABCDEFGHJKMNPQRSTUVWXYZ23456789'//31
 | ||||
|     // const SIGNING_v3 ='1234567890'//10
 | ||||
|     // const SIGNING_v4 ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'//26
 | ||||
|     const SIGNING_v5 = 'abcdefghigklmnopqrstuvwxyz'//26
 | ||||
|     let random_str = SIGNING_v1 + SIGNING_v5 | ||||
|     let random_code = '' | ||||
|     str_length -= 1 | ||||
|     for (let i = 0; i < str_length; i++) { | ||||
|         random_code = random_code + random_str[GetRandomNum(0, str_length)] | ||||
|     } | ||||
|     return random_code | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| export function checkEmail(email) { | ||||
|     let re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/; | ||||
|     return re.test(email); | ||||
| } | ||||
| 
 | ||||
| export function checkphone(email) { | ||||
|     let re = /^1\d{10}$/; | ||||
|     return re.test(email); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| export function geetestbase(func, self, uid, params, callback, errback, readyback) { | ||||
|     func(res => { | ||||
|         if (res.code === 1000) { | ||||
|             let data = res.data; | ||||
|             // eslint-disable-next-line no-undef
 | ||||
|             initGeetest({ | ||||
|                 gt: data.gt, | ||||
|                 challenge: data.challenge, | ||||
|                 new_captcha: data.new_captcha, // 用于宕机时表示是新验证码的宕机
 | ||||
|                 offline: !data.success, // 表示用户后台检测极验服务器是否宕机,一般不需要关注
 | ||||
|                 product: "float", // 产品形式,包括:float,popup
 | ||||
|                 width: "100%" | ||||
|             }, (captchaObj) => { | ||||
|                 self.$refs.captcha.innerHTML = ''; | ||||
|                 captchaObj.appendTo("#captcha"); | ||||
|                 captchaObj.onReady(() => { | ||||
|                     readyback() | ||||
|                 }).onSuccess(() => { | ||||
|                     params.geetest = captchaObj.getValidate(); | ||||
|                     callback(params); | ||||
|                 }).onError(() => { | ||||
|                     captchaObj.destroy(); | ||||
|                 }); | ||||
|             }); | ||||
|         } else { | ||||
|             errback(res) | ||||
|         } | ||||
|     }, { | ||||
|         "methods": "PUT", | ||||
|         "data": {user_id: uid} | ||||
|     }); | ||||
| } | ||||
| @ -0,0 +1,81 @@ | ||||
| #!/usr/bin/env python | ||||
| # -*- coding:utf-8 -*- | ||||
| # project: 3月  | ||||
| # author: NinEveN | ||||
| # date: 2022/3/15 | ||||
| import logging | ||||
| import time | ||||
| 
 | ||||
| from django.core.cache import cache | ||||
| 
 | ||||
| from common.cache.storage import PendingStateCache | ||||
| 
 | ||||
| logger = logging.getLogger(__name__) | ||||
| 
 | ||||
| 
 | ||||
| def set_pending_cache(unique_key, cache_data, cache_obj, timeout): | ||||
|     if unique_key in cache_data: | ||||
|         cache_data.remove(unique_key) | ||||
|     logger.warning(f'return unique_key:{unique_key}  cache_data: {cache_data}  ') | ||||
|     cache_obj.set_storage_cache(cache_data, timeout) | ||||
| 
 | ||||
| 
 | ||||
| def get_pending_result(func, expect_func, loop_count=10, sleep_time=3, unique_key='default_key', | ||||
|                        run_func_count=2, pop_first=True, *args, **kwargs): | ||||
|     """ | ||||
|     :param func:            将要运行的函数对象 | ||||
|     :param expect_func:     期待的运行结果函数 | ||||
|     :param loop_count:      执行次数 | ||||
|     :param sleep_time:      执行循环等待时间 | ||||
|     :param unique_key:      11请求唯一标识,用与函数并发请求 | ||||
|     :param run_func_count:  11函数并发请求数,超过该次数,会自动移除多余等待 | ||||
|     :param pop_first:       11超出,是否自动移除最老的请求,True: 移除最老的, False: 移除最新的 | ||||
|     :param args: | ||||
|     :param kwargs: | ||||
|     :return: | ||||
|     """ | ||||
|     locker_key = kwargs.pop('locker_key') | ||||
|     cache_timeout = loop_count * sleep_time * (run_func_count + 1) | ||||
|     cache_obj = PendingStateCache(locker_key) | ||||
|     cache_data = cache_obj.get_storage_cache() | ||||
|     is_pop = False | ||||
|     if cache_data and isinstance(cache_data, list): | ||||
|         if unique_key not in cache_data: | ||||
|             cache_data.append(unique_key) | ||||
|             if len(cache_data) > run_func_count and len(cache_data) > 0: | ||||
|                 is_pop = True | ||||
|                 if pop_first: | ||||
|                     cache_data.pop(0) | ||||
|                 else: | ||||
|                     cache_data.pop() | ||||
|     else: | ||||
|         cache_data = [unique_key] | ||||
| 
 | ||||
|     cache_obj.set_storage_cache(cache_data, cache_timeout) | ||||
|     if not pop_first and len(cache_data) == run_func_count and is_pop: | ||||
|         logger.warning(f'unique_key:{unique_key}  cache_data: {cache_data}  ') | ||||
|         return True, {'err_msg': '请求重复,请稍后再试'} | ||||
|     try: | ||||
|         with cache.lock(f"get_pending_result_{locker_key}", timeout=loop_count * sleep_time): | ||||
|             count = 1 | ||||
|             while True: | ||||
|                 cache_data = cache_obj.get_storage_cache() | ||||
|                 logger.warning(f'unique_key:{unique_key}  cache_data: {cache_data}  ') | ||||
|                 if cache_data and isinstance(cache_data, list) and unique_key in cache_data: | ||||
|                     result = func(*args, **kwargs) | ||||
|                     if expect_func(result, *args, **kwargs): | ||||
|                         set_pending_cache(unique_key, cache_data, cache_obj, cache_timeout) | ||||
|                         return True, {'data': result} | ||||
|                     time.sleep(sleep_time) | ||||
|                     if loop_count < count: | ||||
|                         set_pending_cache(unique_key, cache_data, cache_obj, cache_timeout) | ||||
|                         return False, {'err_msg': '请求超时'} | ||||
|                     count += 1 | ||||
|                 else: | ||||
|                     set_pending_cache(unique_key, cache_data, cache_obj, cache_timeout) | ||||
|                     return True, {'err_msg': '请求重复,请稍后再试'} | ||||
| 
 | ||||
|     except Exception as e: | ||||
|         logger.warning(f'get pending result exception: {e}') | ||||
|         set_pending_cache(unique_key, cache_data, cache_obj, cache_timeout) | ||||
|         return False, {'err_msg': '内部错误'} | ||||
					Loading…
					
					
				
		Reference in new issue