sys.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. if (!function_exists('auth')) {
  14. /**
  15. * 节点访问权限检查
  16. * @param string $node
  17. * @return boolean
  18. */
  19. function auth($node)
  20. {
  21. $real = \library\tools\Node::get($node);
  22. return \app\admin\service\AuthService::checkAuthNode($real);
  23. }
  24. }
  25. if (!function_exists('sysdata')) {
  26. /**
  27. * JSON 数据读取与存储
  28. * @param string $name 数据名称
  29. * @param array|null $value 数据内容
  30. * @return mixed
  31. * @throws \think\Exception
  32. * @throws \think\exception\PDOException
  33. */
  34. function sysdata($name, array $value = null)
  35. {
  36. if (is_null($value)) {
  37. $data = json_decode(\think\Db::name('SystemData')->where(['name' => $name])->value('value'), true);
  38. return empty($data) ? [] : $data;
  39. } else {
  40. return data_save('SystemData', ['name' => $name, 'value' => json_encode($value, JSON_UNESCAPED_UNICODE)], 'name');
  41. }
  42. }
  43. }
  44. if (!function_exists('_sysmsg')) {
  45. /**
  46. * 增加系统消息
  47. * @param string $title 消息标题
  48. * @param string $desc 消息描述
  49. * @param string $url 访问链接
  50. * @param string $node 权限节点
  51. * @return boolean
  52. */
  53. function _sysmsg($title, $desc, $url, $node)
  54. {
  55. return \app\admin\service\MessageService::add($title, $desc, $url, $node);
  56. }
  57. }
  58. if (!function_exists('_syslog')) {
  59. /**
  60. * 写入系统日志
  61. * @param string $action 日志行为
  62. * @param string $content 日志内容
  63. * @return boolean
  64. */
  65. function _syslog($action, $content)
  66. {
  67. return \app\admin\service\OplogService::write($action, $content);
  68. }
  69. }
  70. if (!function_exists('local_image')) {
  71. /**
  72. * 下载远程文件到本地
  73. * @param string $url 远程图片地址
  74. * @return string
  75. */
  76. function local_image($url)
  77. {
  78. $result = \library\File::down($url);
  79. if (isset($result['url'])) {
  80. return $result['url'];
  81. } else {
  82. return $url;
  83. }
  84. }
  85. }
  86. if (!function_exists('base64_image')) {
  87. /**
  88. * base64 图片上传接口
  89. * @param string $content
  90. * @param string $predir
  91. * @return string
  92. */
  93. function base64_image($content, $predir = 'base64/')
  94. {
  95. try {
  96. if (preg_match('|^data:image/(.*?);base64,|i', $content)) {
  97. list($ext, $base) = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $content));
  98. $info = \library\File::save($predir . md5($base) . '.' . (empty($ext) ? 'tmp' : $ext), base64_decode($base));
  99. return $info['url'];
  100. } else {
  101. return $content;
  102. }
  103. } catch (\Exception $e) {
  104. return $content;
  105. }
  106. }
  107. }
  108. // 系统权限检查中间键
  109. \think\facade\Middleware::add(function (\think\Request $request, \Closure $next) {
  110. // 系统消息处理
  111. if (($code = $request->get('messagecode')) > 0) \app\admin\service\MessageService::set($code);
  112. // 节点忽略跳过
  113. $node = \library\tools\Node::current();
  114. foreach (\app\admin\service\AuthService::getIgnore() as $str) if (stripos($node, $str) === 0) return $next($request);
  115. // 节点权限查询
  116. $auth = \think\Db::name('SystemNode')->cache(true, 60)->field('is_auth,is_login')->where(['node' => $node])->find();
  117. $info = ['is_auth' => $auth['is_auth'], 'is_login' => $auth['is_auth'] ? 1 : $auth['is_login']];
  118. // 登录状态检查
  119. if (!empty($info['is_login']) && !\app\admin\service\AuthService::isLogin()) {
  120. $message = ['code' => 0, 'msg' => '抱歉,您还没有登录获取访问权限!', 'url' => url('@admin/login')];
  121. return $request->isAjax() ? json($message) : redirect($message['url']);
  122. }
  123. // 访问权限检查
  124. if (!empty($info['is_auth']) && !\app\admin\service\AuthService::checkAuthNode($node)) {
  125. return json(['code' => 0, 'msg' => '抱歉,您没有访问该模块的权限!']);
  126. } else {
  127. return $next($request);
  128. }
  129. });