UserLoginLog.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace common\models\user;
  3. use common\models\base\BaseActiveRecord;
  4. use PHPUnit\Util\Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_user_login_log".
  8. *
  9. *
  10. * @property int $id
  11. * @property int $user_id
  12. * @property int $created_at
  13. * @property int $updated_at
  14. * @property string $platform 登录渠道:h5,WXAPP、WECHAT、MP-WX
  15. */
  16. class UserLoginLog extends BaseActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%user_login_log}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['user_id', 'created_at', 'updated_at'], 'integer'],
  32. [['platform'], 'string', 'max' => 45],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'user_id' => 'User ID',
  43. 'created_at' => 'Created At',
  44. 'updated_at' => 'Updated At',
  45. 'platform' => '登录渠道:h5,WXAPP、WECHAT、MP-WX',
  46. ];
  47. }
  48. /**
  49. * 保存数据
  50. * @param array $params
  51. * @return bool|UserLoginLog
  52. */
  53. public static function setData(array $params)
  54. {
  55. try {
  56. if (!empty($params['user_id'])) {
  57. if (empty($model)) {
  58. $model = new self();
  59. $model->loadDefaultValues();
  60. }
  61. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  62. return $model;
  63. }
  64. } catch (Exception $e) {
  65. self::$static_error = $e->getMessage();
  66. return false;
  67. }
  68. }
  69. /**
  70. * 获取登录渠道
  71. * @param $user_id
  72. * @return string
  73. */
  74. public static function getAllPlatform($user_id){
  75. $all_platform = '';
  76. $where[]=['user_id'=>$user_id];
  77. $select = 'user_id,platform';
  78. $group = 'platform';
  79. $params = ['where'=>$where,'select'=>$select,'group'=>$group];
  80. $user_login_log = self::listPage($params);
  81. if(!empty($user_login_log['list'])){
  82. foreach ($user_login_log['list'] as $v){
  83. if(isset(User::TYPE_NAME_LIST[$v['platform']])){
  84. $all_platform .=User::TYPE_NAME_LIST[$v['platform']].'/';
  85. }
  86. }
  87. }
  88. return trim($all_platform,'/');
  89. }
  90. }