SpiderLog.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\IntCode;
  4. use addons\cms\library\Service;
  5. use app\common\library\Auth;
  6. use Hashids\Hashids;
  7. use think\Cache;
  8. use think\Db;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. /**
  12. * 搜索引擎蜘蛛来访记录
  13. */
  14. class SpiderLog extends Model
  15. {
  16. protected $name = "cms_spider_log";
  17. // 开启自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'firsttime';
  21. protected $updateTime = 'lasttime';
  22. // 追加属性
  23. protected $append = [
  24. ];
  25. protected static $config = [];
  26. /**
  27. * 记录搜索引擎蜘蛛请求记录
  28. * @param string $type
  29. * @param int $aid
  30. */
  31. public static function record($type, $aid = 0)
  32. {
  33. $config = get_addon_config('cms');
  34. if (!($config['spiderrecord'] ?? false)) {
  35. return;
  36. }
  37. $spiderName = Service::isSpider();
  38. if (!$spiderName) {
  39. return;
  40. }
  41. $spider = SpiderLog::where('type', $type)->where('aid', $aid)->where('name', $spiderName)->find();
  42. if (!$spider) {
  43. $lastdata = [time()];
  44. $data = [
  45. 'type' => $type,
  46. 'aid' => $aid,
  47. 'url' => request()->url(true),
  48. 'name' => $spiderName,
  49. 'nums' => 1,
  50. 'lastdata' => implode(',', $lastdata)
  51. ];
  52. SpiderLog::create($data);
  53. } else {
  54. $lastdata = explode(',', $spider['lastdata']);
  55. $lastdata[] = time();
  56. $lastdata = array_slice($lastdata, -5);
  57. $spider->save(['lastdata' => implode(',', $lastdata), 'nums' => $spider['nums'] + 1]);
  58. }
  59. }
  60. }