]+?)([\'\"\s]+on[a-z]+)([^<>]+>)/i', '$1$3', $str); $str = preg_replace('/(<[^>]+?)(javascript\:)([^<>]+>)/i', '$1$3', $str); if (strlen($str) == $l) { break; } } } return $str; } /** * @param $str * @param array $data * @return string */ function router($str, $data = []) { $url = array_get(\One\Http\Router::$as_info, $str); if ($data) { $key = array_map(function ($v) { return '{' . $v . '}'; }, array_keys($data)); $data = array_map(function ($v) { return urlencode($v); }, $data); $url = str_replace($key, array_values($data), $url); } return $url; } /** * 统一格式json输出 */ function format_json($data, $code, $id) { $arr = ['err' => $code, 'rid' => $id]; if ($code) { $arr['msg'] = $data; } else { $arr['msg'] = ''; $arr['res'] = $data; } return json_encode($arr); } /** * 设置数组的key * @param $arr * @param $key * @param bool $unique * @return array */ function set_arr_key($arr, $key, $unique = true) { $r = []; foreach ($arr as $v) { if ($unique) { $r[$v[$key]] = $v; } else { $r[$v[$key]][] = $v; } } return $r; } /** * 创建协程id * @param $call * @return string 返回协程id */ function one_go($call) { if (_CLI_) { $co_id = get_co_id(); return go(function () use ($call, $co_id) { $go_id = \One\Facades\Log::bindTraceId($co_id, true); try { $call(); } catch (\Throwable $e) { \One\Facades\Log::flushTraceId($go_id); error_report($e); } \One\Facades\Log::flushTraceId($go_id); }); } else { return $call(); } } /** * 获取协程id */ function get_co_id() { if (_CLI_) { return \Swoole\Coroutine::getuid(); } else { return -1; } } /** * 分布式redis加锁 * @param $tag */ function redis_lock($tag) { $time = time(); $key = 'linelock:' . $tag; while (!\One\Facades\Redis::setnx($key, $time + 3)) { if ($time > \One\Facades\Redis::get($key) && $time > \One\Facades\Redis::getSet($key, $time + 3)) { break; } else { usleep(10); } } } /** * 分布式redis解锁 * @param $tag */ function redis_unlock($tag) { $key = 'linelock:' . $tag; \One\Facades\Redis::del($key); } /** * @param $key * @param null $default * @return mixed|null */ function env($key, $default = null) { static $arr = []; if (empty($arr) && file_exists(_APP_PATH_ . '/app.ini')) { $arr = parse_ini_file(_APP_PATH_ . '/app.ini', true); } return array_get($arr, $key, $default); } function one_get_object_vars($obj) { return get_object_vars($obj); } function error_report(\Throwable $e) { \One\Facades\Log::error([ 'file' => $e->getFile() . ':' . $e->getLine(), 'msg' => $e->getMessage(), 'code' => $e->getCode(), 'trace' => $e->getTrace() ]); }