123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- use library\File;
- use library\service\AdminService;
- use library\service\CaptchaService;
- use library\service\SystemService;
- use think\Db;
- use think\facade\Middleware;
- use think\facade\Route;
- use think\Request;
- if (!function_exists('auth')) {
-
- function auth($node)
- {
- return AdminService::instance()->check($node);
- }
- }
- if (!function_exists('sysdata')) {
-
- function sysdata($name, $value = null)
- {
- if (is_null($value)) {
- return SystemService::instance()->getData($name);
- } else {
- return SystemService::instance()->setData($name, $value);
- }
- }
- }
- if (!function_exists('sysoplog')) {
-
- function sysoplog($action, $content)
- {
- return SystemService::instance()->setOplog($action, $content);
- }
- }
- if (!function_exists('sysqueue')) {
-
- function sysqueue($title, $loade, $later = 0, $data = [], $double = 1)
- {
- $map = [['title', 'eq', $title], ['status', 'in', [1, 2]]];
- if (empty($double) && Db::name('SystemQueue')->where($map)->count() > 0) {
- throw new \think\Exception('该任务已经创建,请耐心等待处理完成!');
- }
- $result = Db::name('SystemQueue')->insert([
- 'title' => $title, 'preload' => $loade,
- 'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
- 'time' => $later > 0 ? time() + $later : time(),
- 'double' => intval($double), 'create_at' => date('Y-m-d H:i:s'),
- ]);
- return $result !== false;
- }
- }
- if (!function_exists('local_image')) {
-
- function local_image($url, $force = false, $expire = 0)
- {
- $result = File::down($url, $force, $expire);
- if (isset($result['url'])) {
- return $result['url'];
- } else {
- return $url;
- }
- }
- }
- if (!function_exists('base64_image')) {
-
- function base64_image($content, $dirname = 'base64/')
- {
- try {
- if (preg_match('|^data:image/(.*?);base64,|i', $content)) {
- list($ext, $base) = explode('|||', preg_replace('|^data:image/(.*?);base64,|i', '$1|||', $content));
- $info = File::save($dirname . md5($base) . '.' . (empty($ext) ? 'tmp' : $ext), base64_decode($base));
- return $info['url'];
- } else {
- return $content;
- }
- } catch (\Exception $e) {
- return $content;
- }
- }
- }
- Middleware::add(function (Request $request, \Closure $next) {
- if (AdminService::instance()->check()) {
- return $next($request);
- } elseif (AdminService::instance()->isLogin()) {
- return json(['code' => 0, 'msg' => '抱歉,没有访问该操作的权限!']);
- } else {
- return json(['code' => 0, 'msg' => '抱歉,需要登录获取访问权限!', 'url' => url('@admin/login')]);
- }
- });
- Route::get('/think/admin/captcha', function () {
- $image = CaptchaService::instance();
- return json(['code' => '1', 'info' => '生成验证码', 'data' => [
- 'uniqid' => $image->getUniqid(), 'image' => $image->getData()
- ]]);
- });
|