common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Library for ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 仓库地址 :https://gitee.com/zoujingli/ThinkLibrary
  12. // | github 仓库地址 :https://github.com/zoujingli/ThinkLibrary
  13. // +----------------------------------------------------------------------
  14. use think\admin\extend\HttpExtend;
  15. use think\admin\service\AdminService;
  16. use think\admin\service\QueueService;
  17. use think\admin\service\SystemService;
  18. use think\admin\service\TokenService;
  19. use think\admin\Storage;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\db\Query;
  24. use think\Model;
  25. if (!function_exists('p')) {
  26. /**
  27. * 打印输出数据到文件
  28. * @param mixed $data 输出的数据
  29. * @param boolean $new 强制替换文件
  30. * @param null|string $file 保存文件名称
  31. * @return false|int
  32. */
  33. function p($data, bool $new = false, ?string $file = null)
  34. {
  35. return SystemService::instance()->putDebug($data, $new, $file);
  36. }
  37. }
  38. if (!function_exists('auth')) {
  39. /**
  40. * 访问权限检查
  41. * @param null|string $node
  42. * @return boolean
  43. * @throws ReflectionException
  44. */
  45. function auth(?string $node): bool
  46. {
  47. return AdminService::instance()->check($node);
  48. }
  49. }
  50. if (!function_exists('sysuri')) {
  51. /**
  52. * 生成最短 URL 地址
  53. * @param string $url 路由地址
  54. * @param array $vars PATH 变量
  55. * @param boolean|string $suffix 后缀
  56. * @param boolean|string $domain 域名
  57. * @return string
  58. */
  59. function sysuri(string $url = '', array $vars = [], $suffix = true, $domain = false): string
  60. {
  61. return SystemService::instance()->sysuri($url, $vars, $suffix, $domain);
  62. }
  63. }
  64. if (!function_exists('sysconf')) {
  65. /**
  66. * 获取或配置系统参数
  67. * @param string $name 参数名称
  68. * @param mixed $value 参数内容
  69. * @return mixed
  70. * @throws DataNotFoundException
  71. * @throws DbException
  72. * @throws ModelNotFoundException
  73. */
  74. function sysconf(string $name = '', $value = null)
  75. {
  76. if (is_null($value) && is_string($name)) {
  77. return SystemService::instance()->get($name);
  78. } else {
  79. return SystemService::instance()->set($name, $value);
  80. }
  81. }
  82. }
  83. if (!function_exists('sysdata')) {
  84. /**
  85. * JSON 数据读取与存储
  86. * @param string $name 数据名称
  87. * @param mixed $value 数据内容
  88. * @return mixed
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. */
  93. function sysdata(string $name, $value = null)
  94. {
  95. if (is_null($value)) {
  96. return SystemService::instance()->getData($name);
  97. } else {
  98. return SystemService::instance()->setData($name, $value);
  99. }
  100. }
  101. }
  102. if (!function_exists('sysqueue')) {
  103. /**
  104. * 注册异步处理任务
  105. * @param string $title 任务名称
  106. * @param string $command 执行内容
  107. * @param integer $later 延时执行时间
  108. * @param array $data 任务附加数据
  109. * @param integer $rscript 任务类型(0单例,1多例)
  110. * @param integer $loops 循环等待时间
  111. * @return string
  112. * @throws \think\admin\Exception
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. */
  117. function sysqueue(string $title, string $command, int $later = 0, array $data = [], int $rscript = 1, int $loops = 0): string
  118. {
  119. return QueueService::instance()->register($title, $command, $later, $data, $rscript, $loops)->code;
  120. }
  121. }
  122. if (!function_exists('systoken')) {
  123. /**
  124. * 生成 CSRF-TOKEN 参数
  125. * @param null|string $node
  126. * @return string
  127. */
  128. function systoken(?string $node = null): string
  129. {
  130. $result = TokenService::instance()->buildFormToken($node);
  131. return $result['token'] ?? '';
  132. }
  133. }
  134. if (!function_exists('sysoplog')) {
  135. /**
  136. * 写入系统日志
  137. * @param string $action 日志行为
  138. * @param string $content 日志内容
  139. * @return boolean
  140. */
  141. function sysoplog(string $action, string $content): bool
  142. {
  143. return SystemService::instance()->setOplog($action, $content);
  144. }
  145. }
  146. if (!function_exists('str2arr')) {
  147. /**
  148. * 字符串转数组
  149. * @param string $text 待转内容
  150. * @param string $separ 分隔字符
  151. * @param null|array $allow 限定规则
  152. * @return array
  153. */
  154. function str2arr(string $text, string $separ = ',', ?array $allow = null): array
  155. {
  156. $text = trim($text, $separ);
  157. $data = strlen($text) ? explode($separ, $text) : [];
  158. if (is_array($allow)) foreach ($data as $key => $item) {
  159. if (!in_array($item, $allow)) unset($data[$key]);
  160. }
  161. foreach ($data as $key => $item) {
  162. if ($item === '') unset($data[$key]);
  163. }
  164. return $data;
  165. }
  166. }
  167. if (!function_exists('arr2str')) {
  168. /**
  169. * 数组转字符串
  170. * @param array $data 待转数组
  171. * @param string $separ 分隔字符
  172. * @param null|array $allow 限定规则
  173. * @return string
  174. */
  175. function arr2str(array $data, string $separ = ',', ?array $allow = null): string
  176. {
  177. if (is_array($allow)) foreach ($data as $key => $item) {
  178. if (!in_array($item, $allow)) unset($data[$key]);
  179. }
  180. foreach ($data as $key => $item) {
  181. if ($item === '') unset($data[$key]);
  182. }
  183. return $separ . join($separ, $data) . $separ;
  184. }
  185. }
  186. if (!function_exists('encode')) {
  187. /**
  188. * 加密 UTF8 字符串
  189. * @param string $content
  190. * @return string
  191. */
  192. function encode(string $content): string
  193. {
  194. [$chars, $length] = ['', strlen($string = iconv('UTF-8', 'GBK//TRANSLIT', $content))];
  195. for ($i = 0; $i < $length; $i++) $chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
  196. return $chars;
  197. }
  198. }
  199. if (!function_exists('decode')) {
  200. /**
  201. * 解密 UTF8 字符串
  202. * @param string $content
  203. * @return string
  204. */
  205. function decode(string $content): string
  206. {
  207. $chars = '';
  208. foreach (str_split($content, 2) as $char) {
  209. $chars .= chr(intval(base_convert($char, 36, 10)));
  210. }
  211. return iconv('GBK//TRANSLIT', 'UTF-8', $chars);
  212. }
  213. }
  214. if (!function_exists('enbase64url')) {
  215. /**
  216. * Base64安全URL编码
  217. * @param string $string
  218. * @return string
  219. */
  220. function enbase64url(string $string): string
  221. {
  222. return rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
  223. }
  224. }
  225. if (!function_exists('debase64url')) {
  226. /**
  227. * Base64安全URL解码
  228. * @param string $string
  229. * @return string
  230. */
  231. function debase64url(string $string): string
  232. {
  233. return base64_decode(str_pad(strtr($string, '-_', '+/'), strlen($string) % 4, '='));
  234. }
  235. }
  236. if (!function_exists('http_get')) {
  237. /**
  238. * 以get模拟网络请求
  239. * @param string $url HTTP请求URL地址
  240. * @param array|string $query GET请求参数
  241. * @param array $options CURL参数
  242. * @return boolean|string
  243. */
  244. function http_get(string $url, $query = [], array $options = [])
  245. {
  246. return HttpExtend::get($url, $query, $options);
  247. }
  248. }
  249. if (!function_exists('http_post')) {
  250. /**
  251. * 以post模拟网络请求
  252. * @param string $url HTTP请求URL地址
  253. * @param array|string $data POST请求数据
  254. * @param array $options CURL参数
  255. * @return boolean|string
  256. */
  257. function http_post(string $url, $data, array $options = [])
  258. {
  259. return HttpExtend::post($url, $data, $options);
  260. }
  261. }
  262. if (!function_exists('data_save')) {
  263. /**
  264. * 数据增量保存
  265. * @param Model|Query|string $dbQuery
  266. * @param array $data 需要保存或更新的数据
  267. * @param string $key 条件主键限制
  268. * @param array $where 其它的where条件
  269. * @return boolean|integer
  270. * @throws DataNotFoundException
  271. * @throws DbException
  272. * @throws ModelNotFoundException
  273. * @throws \think\admin\Exception
  274. */
  275. function data_save($dbQuery, array $data, string $key = 'id', array $where = [])
  276. {
  277. return SystemService::instance()->save($dbQuery, $data, $key, $where);
  278. }
  279. }
  280. if (!function_exists('format_bytes')) {
  281. /**
  282. * 文件字节单位转换
  283. * @param string|integer $size
  284. * @return string
  285. */
  286. function format_bytes($size): string
  287. {
  288. if (is_numeric($size)) {
  289. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  290. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  291. return round($size, 2) . ' ' . $units[$i];
  292. } else {
  293. return $size;
  294. }
  295. }
  296. }
  297. if (!function_exists('format_datetime')) {
  298. /**
  299. * 日期格式标准输出
  300. * @param int|string $datetime 输入日期
  301. * @param string $format 输出格式
  302. * @return string
  303. */
  304. function format_datetime($datetime, string $format = 'Y年m月d日 H:i:s'): string
  305. {
  306. if (empty($datetime)) return '-';
  307. if (is_numeric($datetime)) {
  308. return date($format, $datetime);
  309. } else {
  310. return date($format, strtotime($datetime));
  311. }
  312. }
  313. }
  314. if (!function_exists('down_file')) {
  315. /**
  316. * 下载远程文件到本地
  317. * @param string $source 远程文件地址
  318. * @param boolean $force 是否强制重新下载
  319. * @param integer $expire 强制本地存储时间
  320. * @return string
  321. */
  322. function down_file(string $source, bool $force = false, int $expire = 0): string
  323. {
  324. return Storage::down($source, $force, $expire)['url'] ?? $source;
  325. }
  326. }