common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. declare (strict_types = 1);
  3. use app\common\model\Addons;
  4. use think\facade\Cache;
  5. use app\common\model\Config;
  6. use app\common\service\LangService;
  7. // if (!function_exists('site_config')) {
  8. // /**
  9. // * 获取/设置系统配置
  10. // * @param string $name 属性名
  11. // * @param mixed $vars 属性值
  12. // * @return mixed
  13. // */
  14. // function site_config(string $name,mixed $vars='')
  15. // {
  16. // if(strpos($name,'.')!==false){
  17. // $name=explode('.',$name);
  18. // $group=$name[0];
  19. // $name=$name[1];
  20. // }else{
  21. // $group=$name;
  22. // $name='';
  23. // }
  24. // if(!$vars){
  25. // $groupval=Cache::get('site_config_'.$group);
  26. // if(!$groupval){
  27. // $groupval=Config::where('group',$group)->column('value','name');
  28. // foreach ($groupval as $key=>$val){
  29. // if(is_string($val)){
  30. // if (str_starts_with($val, '{') && str_ends_with($val, '}')) {
  31. // $groupval[$key]=json_decode($val,true);
  32. // continue;
  33. // }
  34. // if(str_starts_with($val, '[') && str_ends_with($val, ']')){
  35. // $groupval[$key]=json_decode($val,true);
  36. // continue;
  37. // }
  38. // }
  39. // $groupval[$key]=$val;
  40. // }
  41. // Cache::set('site_config_'.$group,$groupval);
  42. // }
  43. // if($name) {
  44. // $type=Config::where(['group'=>$group,'name'=>$name])->value('type');
  45. // if($type=='array'){
  46. // $array=str_replace(' ', '', $groupval[$name]);
  47. // $array=str_replace(' ', '', $array);
  48. // return explode(',', $array);
  49. // }
  50. // return $groupval[$name];
  51. // }else{
  52. // return $groupval;
  53. // }
  54. // }else{
  55. // if($name) {
  56. // if(is_array($vars)){
  57. // $vars=json_encode($vars,JSON_UNESCAPED_UNICODE);
  58. // }
  59. // Config::where(['group'=>$group,'name'=>$name])->update(['value'=>$vars]);
  60. // }else{
  61. // foreach ($vars as $key=>$val){
  62. // if(is_array($val)){
  63. // $val=json_encode($val,JSON_UNESCAPED_UNICODE);
  64. // }
  65. // Config::where(['group'=>$group,'name'=>$key])->update(['value'=>$val]);
  66. // }
  67. // }
  68. // Cache::delete('site_config_'.$group);
  69. // }
  70. // }
  71. // }
  72. if (!function_exists('site_config')) {
  73. /**
  74. * 获取/设置系统配置
  75. * @param string $name 属性名
  76. * @param mixed $vars 属性值
  77. * @return mixed
  78. */
  79. function site_config(string $name,mixed $vars='')
  80. {
  81. if(strpos($name,'.')!==false){
  82. $name=explode('.',$name);
  83. $group=$name[0];
  84. $name=$name[1];
  85. }else{
  86. $group=$name;
  87. $name='';
  88. }
  89. if(!$vars){
  90. // $groupval=Cache::get('site_config_'.$group);
  91. $groupval=null;
  92. if(!$groupval){
  93. $groupval=Config::where('group',$group)->column('value','name');
  94. foreach ($groupval as $key=>$val){
  95. if(is_string($val)){
  96. if (str_starts_with($val, '{') && str_ends_with($val, '}')) {
  97. $groupval[$key]=json_decode($val,true);
  98. continue;
  99. }
  100. if(str_starts_with($val, '[') && str_ends_with($val, ']')){
  101. $groupval[$key]=json_decode($val,true);
  102. continue;
  103. }
  104. }
  105. $groupval[$key]=$val;
  106. }
  107. }
  108. if($name) {
  109. $type=Config::where(['group'=>$group,'name'=>$name])->value('type');
  110. if($type=='array'){
  111. $array=str_replace(' ', '', $groupval[$name]);
  112. $array=str_replace(' ', '', $array);
  113. return explode(',', $array);
  114. }
  115. return $groupval[$name];
  116. }else{
  117. return $groupval;
  118. }
  119. }else{
  120. if($name) {
  121. if(is_array($vars)){
  122. $vars=json_encode($vars,JSON_UNESCAPED_UNICODE);
  123. }
  124. Config::where(['group'=>$group,'name'=>$name])->update(['value'=>$vars]);
  125. }else{
  126. foreach ($vars as $key=>$val){
  127. if(is_array($val)){
  128. $val=json_encode($val,JSON_UNESCAPED_UNICODE);
  129. }
  130. Config::where(['group'=>$group,'name'=>$key])->update(['value'=>$val]);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. if (!function_exists('__')) {
  137. /**
  138. * 获取语言变量值
  139. * @param string $name 语言变量名
  140. * @param array $vars 动态变量值
  141. * @param string $lang 语言
  142. * @return mixed
  143. */
  144. function __(string $name,array $vars = [])
  145. {
  146. return LangService::newInstance()->get($name,$vars);
  147. }
  148. }
  149. if (!function_exists('str_rand')) {
  150. /**
  151. * 获取随机字符串
  152. * @return string
  153. */
  154. function str_rand(int $num,string $str=''):string
  155. {
  156. if(!$str){
  157. $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  158. }
  159. $len=strlen($str)-1;
  160. $rand='';
  161. for($i=0;$i<$num;$i++){
  162. $rand.=$str[mt_rand(0,$len)];
  163. }
  164. return $rand;
  165. }
  166. }
  167. if (!function_exists('uuid')) {
  168. /**
  169. * 获取全球唯一标识
  170. * @return string
  171. */
  172. function uuid()
  173. {
  174. return sprintf(
  175. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  176. mt_rand(0, 0xffff),
  177. mt_rand(0, 0xffff),
  178. mt_rand(0, 0xffff),
  179. mt_rand(0, 0x0fff) | 0x4000,
  180. mt_rand(0, 0x3fff) | 0x8000,
  181. mt_rand(0, 0xffff),
  182. mt_rand(0, 0xffff),
  183. mt_rand(0, 0xffff)
  184. );
  185. }
  186. }
  187. if (!function_exists('get_module_alis')) {
  188. /**
  189. * 获取模块别名
  190. * @return string
  191. */
  192. function get_module_alis(string $module='')
  193. {
  194. if(!$module){
  195. $module=app('http')->getName();
  196. }
  197. $arr=config('app.app_map');
  198. foreach ($arr as $key=>$vars){
  199. if($vars==$module){
  200. return $key;
  201. }
  202. }
  203. return $module;
  204. }
  205. }
  206. if (!function_exists('build_url')) {
  207. /**
  208. * 生成url地址
  209. * @return string
  210. */
  211. function build_url(string $url,string $module=''):string
  212. {
  213. $arr=parse_url($url);
  214. $modulename=get_module_alis($module);
  215. $url_html_suffix='.'.config('route.url_html_suffix');
  216. if(strpos($arr['path'],$url_html_suffix)===false){
  217. $arr['path'].=$url_html_suffix;
  218. }
  219. $r='';
  220. if(isset($arr['scheme'])){
  221. $r.=$arr['scheme'].'://';
  222. }
  223. if(isset($arr['host'])){
  224. $r.=$arr['host'];
  225. }
  226. if(isset($arr['path'])){
  227. if(!str_starts_with($arr['path'],'/')) {
  228. $r .= '/'.$modulename.'/';
  229. }
  230. $r.=$arr['path'];
  231. }
  232. if(isset($arr['query'])){
  233. $r.='?'.$arr['query'];
  234. }
  235. return $r;
  236. }
  237. }
  238. if (!function_exists('rmdirs')) {
  239. /**
  240. * 删除文件夹
  241. * @param string $dirname 目录
  242. * @param bool $withself 是否删除自身
  243. * @return boolean
  244. */
  245. function rmdirs(string $dirname, bool $withself = true)
  246. {
  247. if (!is_dir($dirname)) {
  248. return false;
  249. }
  250. $files = new RecursiveIteratorIterator(
  251. new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS),
  252. RecursiveIteratorIterator::CHILD_FIRST
  253. );
  254. foreach ($files as $fileinfo) {
  255. $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
  256. $todo($fileinfo->getRealPath());
  257. }
  258. if ($withself) {
  259. @rmdir($dirname);
  260. }
  261. return true;
  262. }
  263. }
  264. if (!function_exists('create_file')) {
  265. /**
  266. * 创建文件并写入内容,如果所在文件夹不存在,则创建
  267. */
  268. function create_file(string $filepath, string $content = ''){
  269. $dir = dirname($filepath);
  270. if (!is_dir($dir)) {
  271. mkdir($dir, 0755, true);
  272. }
  273. file_put_contents($filepath, $content);
  274. }
  275. }
  276. if (!function_exists('get_addons')) {
  277. /**
  278. * 获取插件信息
  279. * @param string $pack 插件标识
  280. * @return array|bool
  281. */
  282. function get_addons(string $pack='')
  283. {
  284. $addons=Cache::get('download-addons');
  285. if(!$addons){
  286. $addons=Addons::field('id,key,type,name,install,open')->select();
  287. Cache::set('download-addons',$addons);
  288. }
  289. if(!$pack){
  290. return $addons;
  291. }
  292. foreach ($addons as $addon){
  293. if($addon['pack']==$pack){
  294. return $addon;
  295. }
  296. }
  297. return false;
  298. }
  299. }
  300. if (!function_exists('addons_installed')) {
  301. /**
  302. * 判断是否安装插件
  303. * @param string $pack 插件标识
  304. * @return array|bool
  305. */
  306. function addons_installed(string $pack)
  307. {
  308. $addons=Cache::get('download-addons');
  309. if(!$addons){
  310. $addons=Addons::field('id,key,pack,type,name,install,open')->select();
  311. Cache::set('download-addons',$addons);
  312. }
  313. foreach ($addons as $addon){
  314. if($addon['pack']==$pack && $addon['install']==1){
  315. return true;
  316. }
  317. }
  318. return false;
  319. }
  320. }
  321. if(!function_exists('getDbPrefix')){
  322. function getDbPrefix()
  323. {
  324. $config = \think\facade\Db::getConfig();
  325. $default=$config['default'];
  326. $prefix=$config['connections'][$default]['prefix'];
  327. return $prefix;
  328. }
  329. }