ActionLogService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use common\helpers\DebrisHelper;
  6. use common\models\common\ActionLog;
  7. use common\queues\ActionLogJob;
  8. use common\enums\StatusEnum;
  9. use common\components\Service;
  10. use Zhuzhichao\IpLocationZh\Ip;
  11. /**
  12. * Class ActionLogService
  13. * @package services\common
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class ActionLogService extends Service
  17. {
  18. /**
  19. * 队列
  20. *
  21. * @var bool
  22. */
  23. public $queueSwitch = false;
  24. /**
  25. * 行为日志
  26. *
  27. * @param $behavior
  28. * @param $remark
  29. * @param int $mapId
  30. * @param array $mapData
  31. * @param bool $noRecordData
  32. * @return ActionLog|string|null
  33. * @throws \yii\base\InvalidConfigException
  34. */
  35. public function create($behavior, $remark, $mapId = 0, $mapData = [], $noRecordData = true)
  36. {
  37. $model = new ActionLog();
  38. $model->behavior = $behavior;
  39. $model->remark = $remark;
  40. $model->url = DebrisHelper::getUrl();
  41. $model->app_id = Yii::$app->id;
  42. $model->get_data = Yii::$app->request->get();
  43. $model->post_data = $noRecordData == true ? Yii::$app->request->post() : [];
  44. $model->header_data = ArrayHelper::toArray(Yii::$app->request->headers);
  45. $model->method = Yii::$app->request->method;
  46. $model->module = Yii::$app->controller->module->id ?? '';
  47. $model->controller = Yii::$app->controller->id ?? '';
  48. $model->action = Yii::$app->controller->action->id ?? '';
  49. $model->map_id = $mapId;
  50. $model->map_data = $mapData;
  51. $model->device = Yii::$app->services->extendDetection->detectVersion();
  52. $model->ip = Yii::$app->services->base->getUserIp();
  53. $model->member_id = Yii::$app->services->member->getAutoId();
  54. if ($member = Yii::$app->services->member->get($model->member_id)) {
  55. $model->member_name = $member->nickname;
  56. empty($model->member_name) && $model->member_name = $member->username;
  57. }
  58. // 插件里面
  59. if (Yii::$app->params['inAddon'] == true) {
  60. $model->addon_name = Yii::$app->params['addon']['name'];
  61. $model->is_addon = StatusEnum::ENABLED;
  62. }
  63. if ($this->queueSwitch == true) {
  64. $messageId = Yii::$app->queue->push(new ActionLogJob([
  65. 'actionLog' => $model,
  66. ]));
  67. return $messageId;
  68. }
  69. return $this->realCreate($model);
  70. }
  71. /**
  72. * @param ActionLog $actionLog
  73. */
  74. public function realCreate(ActionLog $actionLog)
  75. {
  76. // ip转地区
  77. if (!empty($actionLog->ip) && ip2long($actionLog->ip) && ($ipData = Ip::find($actionLog->ip))) {
  78. $actionLog->country = $ipData[0];
  79. $actionLog->provinces = $ipData[1];
  80. $actionLog->city = $ipData[2];
  81. }
  82. // !$actionLog->save() && $this->error($actionLog);
  83. $actionLog->save();
  84. return $actionLog;
  85. }
  86. /**
  87. * @return int|string
  88. */
  89. public function getCount($merchant_id = '')
  90. {
  91. return ActionLog::find()
  92. ->select('id')
  93. ->andWhere(['status' => StatusEnum::ENABLED])
  94. ->andFilterWhere(['merchant_id' => $merchant_id])
  95. ->count() ?? 0;
  96. }
  97. /**
  98. * @param $behavior
  99. * @param $map_id
  100. * @param $addon_name
  101. * @return array|\yii\db\ActiveRecord[]
  102. */
  103. public function findByBehavior($behavior, $map_id, $addon_name = '')
  104. {
  105. $where = ['is_addon' => StatusEnum::DISABLED];
  106. if (!empty($addon_name)) {
  107. $where = [
  108. 'addon_name' => $addon_name,
  109. 'is_addon' => StatusEnum::ENABLED,
  110. ];
  111. }
  112. return ActionLog::find()
  113. ->where([
  114. 'behavior' => $behavior,
  115. 'map_id' => $map_id,
  116. 'status' => StatusEnum::ENABLED
  117. ])
  118. ->andWhere($where)
  119. ->orderBy('id desc')
  120. ->asArray()
  121. ->all();
  122. }
  123. }
粤ICP备19079148号