User.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/3/8 16:32
  6. * Description:
  7. */
  8. namespace api\common\models;
  9. use api\modules\v1\models\Category;
  10. use api\modules\v1\models\Circle;
  11. use api\modules\v1\models\Clock;
  12. use api\modules\v1\models\ClockLike;
  13. use api\modules\v1\models\Comment;
  14. use api\modules\v1\models\UserCar;
  15. use common\models\Article;
  16. use common\models\Footprint;
  17. use common\models\Friend;
  18. use common\models\SearchRecord;
  19. use common\models\Suggest;
  20. use yii\helpers\Url;
  21. class User extends \common\modules\user\models\User
  22. {
  23. /**
  24. * 默认属性
  25. * @return array|false
  26. * @author nodelog
  27. */
  28. public function fields()
  29. {
  30. return [
  31. 'nickname',
  32. 'tel',
  33. 'avatar' => function ($model) {
  34. return Url::to($model->getAvatar(), true);
  35. },
  36. 'profile',
  37. 'count',
  38. 'type',
  39. 'chain'=> function ($model) {
  40. return Category::find()->where(['id' => $model->chain])->one();
  41. },
  42. ];
  43. }
  44. /**
  45. * 扩展属性
  46. * 请求时接口url加参数,如:?expand=profile
  47. * @return array|false
  48. * @author nodelog
  49. */
  50. public function extraFields()
  51. {
  52. return [
  53. 'profile',
  54. 'count',
  55. ];
  56. }
  57. /**
  58. * 数据统计
  59. * @author nodelog
  60. */
  61. public function getCount()
  62. {
  63. $data = [];
  64. $where = ['user_id' => \Yii::$app->user->id];
  65. $searchCount = SearchRecord::find()->where($where)->andWhere(['del' => 0])->sum('count');
  66. if (empty($searchCount)) {
  67. $searchCount = 0;
  68. }
  69. $pagePathList = \Yii::$app->config->get('page_path_list');
  70. $data[] = [
  71. 'title' => '搜索',
  72. 'count' => $searchCount,
  73. 'link' => $pagePathList['search_page'],
  74. 'link_type' => 1
  75. ];
  76. $data[] = [
  77. 'title' => '足迹',
  78. 'count' => Footprint::find()->my()->count(),
  79. 'link' => $pagePathList['footprint_page'],
  80. 'link_type' => 1
  81. ];
  82. $data[] = [
  83. 'title' => '企业反馈',
  84. 'count' => Article::find()->my()->count(),
  85. 'link' => $pagePathList['company_page'],
  86. 'link_type' => 1
  87. ];
  88. $data[] = [
  89. 'title' => '意见反馈',
  90. 'count' => Suggest::find()->where($where)->count(),
  91. 'link' => $pagePathList['feedback_page'],
  92. 'link_type' => 1
  93. ];
  94. return $data;
  95. }
  96. /**
  97. * 获取用户信息
  98. * @return \yii\db\ActiveQuery
  99. * @author nodelog
  100. */
  101. public function getProfile()
  102. {
  103. return $this->hasOne(Profile::className(), ['user_id' => 'id']);
  104. }
  105. }