123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969 |
- <?php
- use think\Container;
- use think\Response;
- use think\facade\Lang;
- use think\facade\Queue;
- use think\facade\Cache;
- use core\util\Snowflake;
- use app\service\core\upload\CoreImageService;
- use app\service\core\sys\CoreSysConfigService;
- function success($msg = 'SUCCESS', $data = [], int $code = 1, int $http_code = 200): Response
- {
- if (is_array($msg)) {
- $data = $msg;
- $msg = 'SUCCESS';
- }
- return Response::create([ 'data' => $data, 'msg' => get_lang($msg), 'code' => $code ], 'json', $http_code);
- }
- function fail($msg = 'FAIL', ?array $data = [], int $code = 0, int $http_code = 200) : Response
- {
- if (is_array($msg)) {
- $data = $msg;
- $msg = 'FAIL';
- }
- return Response::create([ 'data' => $data, 'msg' => get_lang($msg), 'code' => $code ], 'json', $http_code);
- }
- function get_lang($str)
- {
- return Lang::get($str);
- }
- function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
- {
-
- $tree = array ();
- if (is_array($list)) {
-
- $refer = array ();
- foreach ($list as $key => $data) {
- $refer[ $data[ $pk ] ] =& $list[ $key ];
- }
- foreach ($list as $key => $data) {
-
- $parent_id = $data[ $pid ];
- if ($root == $parent_id) {
- $tree[] =& $list[ $key ];
- } else {
- if (isset($refer[ $parent_id ])) {
- $parent =& $refer[ $parent_id ];
- $parent[ $child ][] =& $list[ $key ];
- }
- }
- }
- }
- return $tree;
- }
- function create_password($password, $salt = '')
- {
- return password_hash($password, PASSWORD_DEFAULT);
- }
- function check_password($password, $hash)
- {
- if (!password_verify($password, $hash)) return false;
- return true;
- }
- function array_keys_search($array, $keys, $index = '', $is_sort = true)
- {
- if (empty($array))
- return $array;
- if (empty($keys))
- return [];
- if (!empty($index) && count($array) != count($array, COUNT_RECURSIVE))
- $array = array_column($array, null, $index);
- $list = array ();
- foreach ($keys as $key) {
- if (isset($array[ $key ])) {
- if ($is_sort) {
- $list[] = $array[ $key ];
- } else {
- $list[ $key ] = $array[ $key ];
- }
- }
- }
- return $list;
- }
- function del_target_dir($path, $delDir)
- {
-
- if (!file_exists($path)) {
- return false;
- }
-
- $handle = opendir($path);
- if ($handle) {
- while (false !== ( $item = readdir($handle) )) {
- if ($item != "." && $item != "..") {
- if (is_dir("$path/$item")) {
- del_target_dir("$path/$item", $delDir);
- } else {
- unlink("$path/$item");
- }
- }
- }
- closedir($handle);
- if ($delDir) {
- return rmdir($path);
- }
- } else {
- if (file_exists($path)) {
- return unlink($path);
- }
- return false;
- }
- }
- function system_name(?string $key = '')
- {
- $params = [
- 'admin_token_name' => env('system.admin_token_name', 'token'),
- 'api_token_name' => env('system.api_token_name', 'token'),
- 'admin_site_id_name' => env('system.admin_site_id_name', 'site-id'),
- 'api_site_id_name' => env('system.api_site_id_name', 'site-id'),
- 'channel_name' => env('system.channel_name', 'channel'),
- ];
- if (!empty($key)) {
- return $params[ $key ];
- } else {
- return $params;
- }
- }
- function get_date_by_time(?int $time = null)
- {
- return date('Y-m-d h:i:s', $time);
- }
- function get_start_and_end_time_by_day($day = '')
- {
- $date = $day ? : date('Y-m-d');
- $day_start_time = strtotime($date);
-
- $day_end_time = $day_start_time + 86400;
- return [ $day_start_time, $day_end_time ];
- }
- function path_to_url($path)
- {
- return trim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '.');
- }
- function url_to_path($url)
- {
- if (str_contains($url, 'http://') || str_contains($url, 'https://')) return $url;
- return public_path() . trim(str_replace('/', DIRECTORY_SEPARATOR, $url));
- }
- function get_file_url(string $path)
- {
- if (!$path) return '';
- if (!str_contains($path, 'http://') && !str_contains($path, 'https://')) {
- return request()->domain() . '/' . path_to_url($path);
- } else {
- return path_to_url($path);
- }
- }
- function create_job($job, $data = '', $delay = 0, $queue = null)
- {
- if ($delay > 0) {
- $is_success = Queue::later($delay, $job, $data, $queue);
- } else {
- $is_success = Queue::push($job, $data, $queue);
- }
- if ($is_success !== false) {
- return true;
- } else {
- return false;
- }
- }
- function addon_resource($addon, $file_name)
- {
- return "addon/" . $addon . "/" . $file_name;
- }
- function is_write($file)
- {
- if (is_dir($file)) {
- $dir = $file;
- if ($fp = @fopen("$dir/test.txt", 'wb')) {
- @fclose($fp);
- @unlink("$dir/test.txt");
- $writeable = true;
- } else {
- $writeable = false;
- }
- } else {
- if ($fp = @fopen($file, 'ab+')) {
- @fclose($fp);
- $writeable = true;
- } else {
- $writeable = false;
- }
- }
- return $writeable;
- }
- function format_money($number)
- {
- if ($number == intval($number)) {
- return intval($number);
- } elseif ($number == sprintf('%.2f', $number)) {
- return sprintf('%.2f', $number);
- }
- return $number;
- }
- function format_float_money($number, $precision = 2)
- {
- if ($precision > 0) {
- return sprintf('%.' . $precision . 'f', floor($number * ( 10 ** $precision )) / ( 10 ** $precision ));
- } else {
- return sprintf('%.' . $precision . 'f', floor($number));
- }
- }
- function format_round_money($number)
- {
- return round($number, 2);
- }
- function filter($string)
- {
- return $string;
- }
- function create_no(string $prefix = '', string $tag = '')
- {
- $data_center_id = 1;
- $machine_id = 2;
- $snowflake = new Snowflake($data_center_id, $machine_id);
- $id = $snowflake->generateId();
- return $prefix . date('Ymd') . $tag . $id;
- }
- function mkdirs($dir, $mode = 0777)
- {
- if (str_contains($dir, '.')) $dir = dirname($dir);
- if (is_dir($dir) || @mkdir($dir, $mode)) return true;
- if (!mkdirs(dirname($dir), $mode)) return false;
- return @mkdir($dir, $mode);
- }
- function mkdirs_or_notexist($dir, $mode = 0777)
- {
- if (!is_dir($dir) && !mkdir($dir, $mode, true) && !is_dir($dir)) {
- throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
- }
- return true;
- }
- function rmdirs($dir)
- {
- $dh = opendir($dir);
- while ($file = readdir($dh)) {
- if ($file != "." && $file != "..") {
- $fullpath = $dir . "/" . $file;
- if (is_dir($fullpath)) {
- rmdirs($fullpath);
- } else {
- unlink($fullpath);
- }
- }
- }
- closedir($dh);
- }
- function unique_random($len = 10)
- {
- $str = 'qwertyuiopasdfghjklzxcvbnmasdfgh';
- str_shuffle($str);
- return substr(str_shuffle($str), 0, $len);
- }
- function check_event_result($result)
- {
- if (empty($result) || is_array($result)) {
- return true;
- }
- foreach ($result as $v) {
- if (!$v) return false;
- }
- return true;
- }
- function array_merge2(array $array1, array $array2)
- {
- foreach ($array2 as $array2_k => $array2_v) {
- if (array_key_exists($array2_k, $array1)) {
- if (is_array($array2_v)) {
- foreach ($array2_v as $array2_kk => $array2_vv) {
- if (array_key_exists($array2_kk, $array1[ $array2_k ])) {
- if (is_array($array2_vv)) {
- $array1[ $array2_k ][ $array2_kk ] = array_merge($array1[ $array2_k ][ $array2_kk ], $array2_vv);
- }
- } else {
- $array1[ $array2_k ][ $array2_kk ] = $array2_vv;
- }
- }
- } else {
- $array1[ $array2_k ] = $array2_v;
- }
- } else {
- $array1[ $array2_k ] = $array2_v;
- }
- }
- return $array1;
- }
- function get_files_by_dir($dir)
- {
- $dh = @opendir($dir);
- $return = array ();
- while ($file = @readdir($dh)) {
- if ($file != '.' and $file != '..') {
- $path = $dir . DIRECTORY_SEPARATOR . $file;
- if (is_dir($path)) {
- $return[] = $file;
- }
- }
- }
- @closedir($dh);
- return $return;
- }
- function dir_copy(string $src = '', string $dst = '', &$files = [], $exclude_dirs = [], $exclude_files = [])
- {
- if (empty($src) || empty($dst)) {
- return false;
- }
- if (!file_exists($src)) {
- return false;
- }
- $dir = opendir($src);
- dir_mkdir($dst);
- while (false !== ( $file = readdir($dir) )) {
- if (( $file != '.' ) && ( $file != '..' )) {
- if (is_dir($src . '/' . $file)) {
-
- if (count($exclude_dirs) && in_array($file, $exclude_dirs)) continue;
- dir_copy($src . '/' . $file, $dst . '/' . $file, $files);
- } else {
-
- if (count($exclude_files) && in_array($file, $exclude_files)) continue;
- copy($src . '/' . $file, $dst . '/' . $file);
- $files[] = $dst . '/' . $file;
- }
- }
- }
- closedir($dir);
- return true;
- }
- function dir_remove(string $dst = '', array $dirs = [])
- {
- if (empty($dirs) || empty($dst)) {
- return false;
- }
- foreach ($dirs as $v) {
- @unlink($dst . $v);
- }
- return true;
- }
- function dir_mkdir($path = '', $mode = 0777, $recursive = true)
- {
- clearstatcache();
- if (!is_dir($path)) {
- if (mkdir($path, $mode, $recursive)) {
- return chmod($path, $mode);
- } else {
- throw new \core\exception\CommonException("目录{$path}创建失败请检查是否有足够的权限");
- }
- }
- return true;
- }
- function parse_sql($content = '', $string = false, $replace = [])
- {
-
- $pure_sql = [];
-
- $from = '';
-
- $to = '';
-
- if (!empty($replace)) {
- $to = current($replace);
- $from = current(array_flip($replace));
- }
- if ($content != '') {
-
- $comment = false;
-
- $content = str_replace([ "\r\n", "\r" ], "\n", $content);
- $content = explode("\n", trim($content));
-
- foreach ($content as $line) {
-
- if ($line == '') {
- continue;
- }
-
- if (preg_match("/^(#|--)/", $line)) {
- continue;
- }
-
- if (preg_match("/^\/\*(.*?)\*\//", $line)) {
- continue;
- }
-
- if (str_starts_with($line, '/*')) {
- $comment = true;
- continue;
- }
-
- if (str_ends_with($line, '*/')) {
- $comment = false;
- continue;
- }
-
- if ($comment) {
- continue;
- }
-
- if ($from != '') {
- $line = str_replace('`' . $from, '`' . $to, $line);
- }
-
- $pure_sql[] = $line;
- }
-
- if ($string) {
- return implode("", $pure_sql);
- }
-
- $pure_sql = implode("\n", $pure_sql);
- $pure_sql = explode(";\n", $pure_sql);
- }
- return $pure_sql;
- }
- function search_dir($path, &$data, $search = '')
- {
- if (is_dir($path)) {
- $path .= DIRECTORY_SEPARATOR;
- $fp = dir($path);
- while ($file = $fp->read()) {
- if ($file != '.' && $file != '..') {
- search_dir($path . $file, $data, $search);
- }
- }
- $fp->close();
- }
- if (is_file($path)) {
- if ($search) $path = str_replace($search, '', $path);
- $data[] = $path;
- }
- }
- function remove_empty_dir($dirs)
- {
- }
- function getFileMap($path, $arr = [])
- {
- if (is_dir($path)) {
- $dir = scandir($path);
- foreach ($dir as $file_path) {
- if ($file_path != '.' && $file_path != '..') {
- $temp_path = $path . '/' . $file_path;
- if (is_dir($temp_path)) {
- $arr[ $temp_path ] = $file_path;
- $arr = getFileMap($temp_path, $arr);
- } else {
- $arr[ $temp_path ] = $file_path;
- }
- }
- }
- return $arr;
- }
- }
- function cache_remember(string $name = null, $value = '', $tag = null, $options = null)
- {
- if (!empty($hit = Cache::get($name)))
- return $hit;
- if ($value instanceof Closure) {
-
- $value = Container::getInstance()->invokeFunction($value);
- }
- if (is_null($tag)) {
- Cache::set($name, $value, $options[ 'expire' ] ?? null);
- } else {
- Cache::tag($tag)->set($name, $value, $options[ 'expire' ] ?? null);
- }
- return $value;
- }
- function project_path()
- {
- return dirname(root_path()) . DIRECTORY_SEPARATOR;
- }
- function image_to_base64(string $path, $is_delete = false)
- {
- if (!file_exists($path)) return 'image not exist';
- $mime = getimagesize($path)[ 'mime' ];
- $image_data = file_get_contents($path);
-
- $base64_data = base64_encode($image_data);
- if ($is_delete) @unlink($path);
- return "data:$mime;base64,$base64_data";
- }
- function get_thumb_images($site_id, $image, $thumb_type = 'all', bool $is_throw_exception = false)
- {
- return ( new CoreImageService() )->thumb($site_id, $image, $thumb_type, $is_throw_exception);
- }
- function version_to_int($version)
- {
- $version_array = explode(".", $version);
- $v1 = sprintf('%03s', (int) $version_array[ 0 ] ?? 0);
- $v2 = sprintf('%03s', (int) $version_array[ 1 ] ?? 0);
- $v3 = sprintf('%03s', (int) $version_array[ 2 ] ?? 0);
- return (int) "{$v1}{$v2}{$v3}";
- }
- function version_to_string($ver)
- {
- if ($ver > 999) {
- if ($ver > 999999) {
- $ver .= "";
- $v3 = (int) substr($ver, -3);
- $v2 = (int) substr($ver, -6, 3);
- $v1 = (int) substr($ver, 0, -6);
- } else {
- $ver .= "";
- $v3 = (int) substr($ver, -3);
- $v2 = (int) substr($ver, 0, -3);
- $v1 = 0;
- }
- } else {
- $v3 = $ver;
- $v2 = 0;
- $v1 = 0;
- }
- return "{$v1}.{$v2}.{$v3}";
- }
- function check_file_is_remote(string $file_path)
- {
- return str_contains($file_path, 'https://') || str_contains($file_path, 'http://') || str_contains($file_path, '.com');
- }
- function file_copy(string $source_file, string $to_file)
- {
- if (!file_exists($source_file)) return false;
-
- if (!file_exists($to_file)) {
-
- $directory = dirname($to_file);
- if (!file_exists($directory)) {
- mkdir($directory, 0777, true);
- }
- }
- if (copy($source_file, $to_file)) {
- return true;
- } else {
- return false;
- }
- }
- function qrcode($url, $page, $data, $site_id, $dir = '', $channel = 'h5', $style = [ 'is_transparent' => true ], $outfile = true)
- {
- $site_id = 0;
- if ($outfile) {
- $dir = $dir ?: 'upload' . '/' . 'qrcode' . '/' . $site_id;
- if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
- throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
- }
- $file_path = md5($url . $page . serialize($data) . serialize($style) . $channel);
- $path = $dir . '/' . $file_path . '.png';
- if (file_exists($path)) {
- return $path;
- }
- }
- $result = array_values(array_filter(event('GetQrcodeOfChannel', [
- 'filepath' => $path ?? '',
- 'url' => $url,
- 'page' => $page,
- 'data' => $data,
- 'site_id' => $site_id,
- 'channel' => $channel,
- 'outfile' => $outfile
- ])));
- if (!empty($result[ 0 ])) {
- $path = $result[ 0 ];
- }
- return $path;
- }
- function poster(int $site_id, $id, $type, array $param = [], string $channel = '', bool $is_throw_exception = true)
- {
- return ( new \app\service\core\poster\CorePosterService() )->get($site_id, $id, $type, $param, $channel, $is_throw_exception);
- }
- function get_site_addons($site_id = 0) : array
- {
- $addons = $site_id ? Cache::get("local_install_addons_{$site_id}") : Cache::get("local_install_addons");
- return is_null($addons) ? [] : $addons;
- }
- function get_wap_domain()
- {
- $wap_url = ( new CoreSysConfigService() )->getSceneDomain()[ 'wap_url' ];
- return $wap_url;
- }
- function str_sub($str, $length = 10, $is_need_apostrophe = true)
- {
- return mb_substr($str, 0, $length, 'UTF-8') . ( $is_need_apostrophe ? '...' : '' );
- }
|