BannersController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Http\Controllers\Banner;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Http\Requests\BannerCreateRequest;
  5. use App\Http\Requests\BannerUpdateRequest;
  6. use App\Models\Goods;
  7. use App\Models\GoodsClass;
  8. use App\Models\Menu;
  9. use App\Repositories\Eloquent\BannerRepositoryEloquent;
  10. use App\Validators\BannerValidator;
  11. /**
  12. * Class BannersController.
  13. *
  14. * @package namespace App\Http\Controllers;
  15. */
  16. class BannersController extends AdminBaseController
  17. {
  18. /**
  19. * @var BannerRepositoryEloquent
  20. */
  21. protected $repository;
  22. /**
  23. * @var BannerValidator
  24. */
  25. protected $validator;
  26. /**
  27. * BannersController constructor.
  28. *
  29. * @param BannerRepositoryEloquent $repository
  30. * @param BannerValidator $validator
  31. */
  32. public function __construct(BannerRepositoryEloquent $repository, BannerValidator $validator)
  33. {
  34. parent::__construct($repository, $validator);
  35. }
  36. /**
  37. * 数据检索
  38. */
  39. public function _indexScopeQuery()
  40. {
  41. $where = [];
  42. $search = explode(";", request()->input('search', ""));
  43. $start = $end = null;
  44. $fieldSearchable = $this->repository->getFieldsSearchable();
  45. foreach ($search as $value) {
  46. if (!empty($value)) {
  47. list($one, $tow) = explode(":", $value);
  48. if (!empty($fieldSearchable[$one])) {
  49. if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
  50. $where[] = [$one, $fieldSearchable[$one], $tow];
  51. } elseif ($one == 'start') {
  52. $start = $tow . " 00:00:00";
  53. } elseif ($one == 'end') {
  54. $end = $tow . " 23:59:59";
  55. } else {
  56. continue;
  57. }
  58. }
  59. }
  60. return function ($query) use ($where, $start, $end) {
  61. if ($start) $where[] = ['created_at', '>=', $start];
  62. if ($end) $where[] = ['created_at', '<=', $end];
  63. if ($where) {
  64. $query->where($where);
  65. }
  66. };
  67. }
  68. /**
  69. * 数据更新
  70. */
  71. protected function _indexPost($datum)
  72. {
  73. foreach ($datum as $value) {
  74. $this->getValue($value);
  75. }
  76. return $datum;
  77. }
  78. private function getValue(&$value)
  79. {
  80. $to_name='无';
  81. if($value->{'type'}==0){
  82. $value->{'type'}='无';
  83. }elseif ($value->{'type'}==1){
  84. $value->{'type'}='链接商品';
  85. $to_name=Goods::where('id', $value->{'to_id'})->value('good_name');
  86. }elseif ($value->{'type'}==2){
  87. $value->{'type'}='链接分类';
  88. $to_name=GoodsClass::where('id', $value->{'to_id'})->value('class_name');
  89. }elseif ($value->{'type'}==3){
  90. $to_name='秒杀';
  91. $value->{'type'}='链接秒杀';
  92. }else{
  93. $value->{'type'}='外部链接';
  94. }
  95. $value->{'to_name'}=$to_name;
  96. }
  97. /**
  98. * 创建数据组建
  99. * @return array
  100. */
  101. function _storeGet()
  102. {
  103. $type_list = $this->getTypeList('0');
  104. return ['type_list' => json_encode($type_list, JSON_UNESCAPED_SLASHES)];
  105. }
  106. /**
  107. * 修改数据组建
  108. * @param $id
  109. * @return array
  110. */
  111. function _editGet($id)
  112. {
  113. $model = $this->repository->find($id);
  114. $type_list = $this->getTypeList($model->{'type'});
  115. $this->getBannerInfo($model);
  116. return ['model' => $model, 'type_list' => json_encode($type_list, JSON_UNESCAPED_SLASHES)];
  117. }
  118. private function getBannerInfo(&$model)
  119. {
  120. $goods_name = '';
  121. $class_id = 0;
  122. if ($model->{'type'} == 1) {
  123. $goods_name = Goods::where('id', $model->{'to_id'})->value('good_name');
  124. } elseif ($model->{'type'} == 2) {
  125. $class_id = GoodsClass::where('id', $model->{'to_id'})->value('p_id');
  126. }
  127. $model->{'to_name'} = $goods_name;
  128. $model->{'class_id'} = $class_id;
  129. }
  130. /**
  131. * 轮播链接类型
  132. * @param $type
  133. * @return array
  134. */
  135. private function getTypeList($type)
  136. {
  137. $option = [
  138. 'checked' => $type . '',
  139. 'attr' => [
  140. [
  141. 'value' => '0',
  142. 'label' => "无链接",
  143. 'disabled' => false,
  144. 'notice' => '*'
  145. ],
  146. [
  147. 'value' => '1',
  148. 'label' => "链接商品",
  149. 'disabled' => false,
  150. 'notice' => '*'
  151. ],
  152. [
  153. 'value' => '2',
  154. 'label' => "链接分类",
  155. 'disabled' => false,
  156. 'notice' => '*'
  157. ],
  158. [
  159. 'value' => '3',
  160. 'label' => "链接秒杀",
  161. 'disabled' => false,
  162. 'notice' => '*'
  163. ],
  164. [
  165. 'value' => '4',
  166. 'label' => "外部链接",
  167. 'disabled' => false,
  168. 'notice' => '*'
  169. ],
  170. ]
  171. ];
  172. return $option;
  173. }
  174. }