sys.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. use library\File;
  15. use library\service\AdminService;
  16. use library\service\CaptchaService;
  17. use library\service\SystemService;
  18. use think\Db;
  19. use think\facade\Middleware;
  20. use think\facade\Route;
  21. use think\Request;
  22. if (!function_exists('auth')) {
  23. /**
  24. * 节点访问权限检查
  25. * @param string $node 需要检查的节点
  26. * @return boolean
  27. * @throws ReflectionException
  28. */
  29. function auth($node)
  30. {
  31. return AdminService::instance()->check($node);
  32. }
  33. }
  34. if (!function_exists('sysdata')) {
  35. /**
  36. * JSON 数据读取与存储
  37. * @param string $name 数据名称
  38. * @param mixed $value 数据内容
  39. * @return mixed
  40. * @throws \think\Exception
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. * @throws \think\exception\PDOException
  45. */
  46. function sysdata($name, $value = null)
  47. {
  48. if (is_null($value)) {
  49. return SystemService::instance()->getData($name);
  50. } else {
  51. return SystemService::instance()->setData($name, $value);
  52. }
  53. }
  54. }
  55. if (!function_exists('sysoplog')) {
  56. /**
  57. * 写入系统日志
  58. * @param string $action 日志行为
  59. * @param string $content 日志内容
  60. * @return boolean
  61. */
  62. function sysoplog($action, $content)
  63. {
  64. return SystemService::instance()->setOplog($action, $content);
  65. }
  66. }
  67. if (!function_exists('sysqueue')) {
  68. /**
  69. * 创建异步处理任务
  70. * @param string $title 任务名称
  71. * @param string $loade 执行内容
  72. * @param integer $later 延时执行时间
  73. * @param array $data 任务附加数据
  74. * @param integer $double 任务多开
  75. * @return boolean
  76. * @throws \think\Exception
  77. */
  78. function sysqueue($title, $loade, $later = 0, $data = [], $double = 1)
  79. {
  80. $map = [['type', 'eq', $data['type']], ['status', 'in', [1, 2]]];
  81. if (empty($double) && Db::name('SystemQueue')->where($map)->count() > 0) {
  82. $array = [
  83. 'status'=>0,
  84. 'info'=>'该表任务已经创建,请耐心等待处理完成!'
  85. ];
  86. //throw new \think\Exception('该任务已经创建,请耐心等待处理完成!');
  87. }else{
  88. $result = Db::name('SystemQueue')->insertGetId([
  89. 'title' => $title, 'preload' => $loade,
  90. 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
  91. 'time' => $later > 0 ? time() + $later : time(),
  92. 'double' => intval($double), 'create_at' => date('Y-m-d H:i:s'),
  93. 'type'=>$data['type'],
  94. 'local_url'=>$data['local_url'],
  95. 'import_name'=>$data['import_name'],
  96. ]);
  97. if ($result){
  98. $array= [
  99. 'status'=>1,
  100. 'info'=>'上传成功,任务已创建'
  101. ];
  102. }else{
  103. $array= [
  104. 'status'=>0,
  105. 'info'=>'创建记录失败,请稍后重试'
  106. ];
  107. }
  108. }
  109. return $array;
  110. //return $result !== false;
  111. }
  112. }
  113. if (!function_exists('local_image')) {
  114. /**
  115. * 下载远程文件到本地
  116. * @param string $url 远程图片地址
  117. * @param boolean $force 是否强制重新下载
  118. * @param integer $expire 强制本地存储时间
  119. * @return string
  120. */
  121. function local_image($url, $force = false, $expire = 0)
  122. {
  123. $result = File::down($url, $force, $expire);
  124. if (isset($result['url'])) {
  125. return $result['url'];
  126. } else {
  127. return $url;
  128. }
  129. }
  130. }
  131. if (!function_exists('base64_image')) {
  132. /**
  133. * base64 图片上传接口
  134. * @param string $content 图片base64内容
  135. * @param string $dirname 图片存储目录
  136. * @return string
  137. */
  138. function base64_image($content, $dirname = 'base64/')
  139. {
  140. try {
  141. if (preg_match('|^data:image/(.*?);base64,|i', $content)) {
  142. list($ext, $base) = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $content));
  143. $info = File::save($dirname . md5($base) . '.' . (empty($ext) ? 'tmp' : $ext), base64_decode($base));
  144. return $info['url'];
  145. } else {
  146. return $content;
  147. }
  148. } catch (\Exception $e) {
  149. return $content;
  150. }
  151. }
  152. }
  153. // 访问权限检查中间键
  154. Middleware::add(function (Request $request, \Closure $next) {
  155. if (AdminService::instance()->check()) {
  156. return $next($request);
  157. } elseif (AdminService::instance()->isLogin()) {
  158. return json(['code' => 0, 'msg' => '抱歉,没有访问该操作的权限!']);
  159. } else {
  160. return json(['code' => 0, 'msg' => '抱歉,需要登录获取访问权限!', 'url' => url('@admin/login')]);
  161. }
  162. });
  163. // ThinkAdmin 图形验证码
  164. Route::get('/think/admin/captcha', function () {
  165. $image = CaptchaService::instance();
  166. return json(['code' => '1', 'info' => '生成验证码', 'data' => [
  167. 'uniqid' => $image->getUniqid(), 'image' => $image->getData()
  168. ]]);
  169. });