LoginLog.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace common\models\elasticsearch\user;
  3. use common\models\base\EsBaseActiveRecord;
  4. use common\models\user\User;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\elasticsearch\ActiveRecord;
  7. /**
  8. * Class LoginLog
  9. * @package addons\RfExample\common\models
  10. */
  11. class LoginLog extends EsBaseActiveRecord
  12. {
  13. public static $currentIndex;
  14. /**
  15. * 数据库名称
  16. * @return string
  17. */
  18. public static function index()
  19. {
  20. return 'es_log';
  21. }
  22. /**
  23. * 表名
  24. *
  25. * @return string
  26. */
  27. public static function type()
  28. {
  29. return 'user_login_log';
  30. }
  31. public function attributes()
  32. {
  33. $mapConfig = self::mapConfig();
  34. return array_keys($mapConfig['properties']);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['mall_id','nickname','user_id','ip','platform'], 'required'],
  43. ];
  44. }
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'mall_id' => '商城id',
  49. 'user_id' => '用户id',
  50. 'nickname' => '昵称',
  51. 'platform' => '登录渠道',
  52. 'ip' => '登录ip',
  53. 'created_at' => '创建时间',
  54. 'updated_at' => '更新时间',
  55. ];
  56. }
  57. /**
  58. * mapping配置(表字段说明)
  59. *
  60. * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
  61. * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
  62. * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
  63. *
  64. * @return array
  65. */
  66. public static function mapConfig()
  67. {
  68. return [
  69. 'properties' => [
  70. // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
  71. // index 默认可不设置
  72. 'mall_id' => ['type' => 'keyword'],
  73. 'user_id' => ['type' => 'keyword'],
  74. 'nickname' => ['type' => 'keyword'],
  75. 'platform' => ['type' => 'keyword'],
  76. 'ip' => ['type' => 'keyword'],
  77. 'created_at' => ['type' => 'long'],
  78. 'updated_at' => ['type' => 'long'],
  79. ],
  80. ];
  81. }
  82. /**
  83. * @return array
  84. */
  85. public static function mapping()
  86. {
  87. return [
  88. static::type() => self::mapConfig(),
  89. ];
  90. }
  91. /**
  92. * 更新字段
  93. *
  94. * @throws \yii\base\InvalidConfigException
  95. */
  96. public static function updateMapping()
  97. {
  98. $db = self::getDb();
  99. $command = $db->createCommand();
  100. if (!$command->indexExists(self::index())) {
  101. $command->createIndex(self::index());
  102. }
  103. $command->setMapping(self::index(), self::type(), self::mapping(),['include_type_name'=>'true']);
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function behaviors()
  109. {
  110. return [
  111. [
  112. 'class' => TimestampBehavior::class,
  113. 'attributes' => [
  114. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  115. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  116. ],
  117. ],
  118. ];
  119. }
  120. /**
  121. * 设置数据
  122. * @param $params
  123. */
  124. public static function setData($params){
  125. $add_data = ['LoginLog'=>$params];
  126. $model = new self();
  127. $model->load($add_data);
  128. $model->save();
  129. }
  130. /**
  131. * 获取登录渠道
  132. * @param $user_id
  133. * @return string
  134. */
  135. public static function getAllPlatform($user_id){
  136. $query = self::find();
  137. $count = $query->where(['user_id'=>$user_id])->count();
  138. $page_size = 100;
  139. $page = ceil($count/$page_size);
  140. $platform_arr = [];
  141. for ($i=0;$i<=$page;$i++){
  142. $list = $query->offset($i*$page_size)->limit($page_size)->asArray()->all();
  143. if(!empty($list)){
  144. foreach ($list as $v){
  145. if(!empty($v['_source']['platform'])){
  146. if(isset(User::TYPE_NAME_LIST[$v['_source']['platform']])){
  147. if(!in_array(User::TYPE_NAME_LIST[$v['_source']['platform']],$platform_arr)){
  148. $platform_arr[]=User::TYPE_NAME_LIST[$v['_source']['platform']];
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. return join('/',$platform_arr);
  156. }
  157. /**
  158. * 获取登录日志列表
  159. * @param $params
  160. * @return array
  161. */
  162. public static function getLoginLogList($params){
  163. return self::listPage($params,true);
  164. }
  165. }