ThemeService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use common\helpers\ArrayHelper;
  5. use common\models\common\Theme;
  6. use common\enums\ThemeColorEnum;
  7. /**
  8. * Class ThemeService
  9. * @package services\common
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class ThemeService
  13. {
  14. /**
  15. * @return void
  16. */
  17. public function autoSwitcher()
  18. {
  19. if ($theme = $this->findByMemberId(Yii::$app->user->id)) {
  20. Yii::$app->params['theme'] = ArrayHelper::merge(Yii::$app->params['theme'], [
  21. 'layout' => $theme->layout,
  22. 'color' => $theme->color,
  23. ]);
  24. }
  25. }
  26. /**
  27. * @param $layout
  28. * @param $color
  29. * @return void
  30. */
  31. public function update($layout, $color = ThemeColorEnum::BLACK)
  32. {
  33. $memberId = Yii::$app->user->id;
  34. $theme = $this->findByMemberId($memberId);
  35. if (empty($theme)) {
  36. $theme = new Theme();
  37. $theme = $theme->loadDefaultValues();
  38. $theme->app_id = Yii::$app->id;
  39. $theme->member_id = $memberId;
  40. $theme->member_type = Yii::$app->user->identity->type;
  41. }
  42. $theme->layout = $layout;
  43. $theme->color = $color;
  44. $theme->save();
  45. }
  46. /**
  47. * @param $memberId
  48. * @param $memberType
  49. * @return array|\yii\db\ActiveRecord|null|Theme
  50. */
  51. public function findByMemberId($memberId)
  52. {
  53. return Theme::find()
  54. ->where(['member_id' => $memberId])
  55. ->one();
  56. }
  57. }
粤ICP备19079148号