SettingsController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers\Admin\Settings;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Models\BaseSettingsModel;
  5. use Illuminate\Http\Request;
  6. use App\Repositories\Settings\Banner\Criteria\MultiWhere;
  7. use App\Repositories\Settings\SettingsRepository;
  8. use App\Repositories\Base\Criteria\OrderBy;
  9. class SettingsController extends Controller
  10. {
  11. private $repository;
  12. public function __construct(SettingsRepository $repository) {
  13. if(!$this->repository) $this->repository = $repository;
  14. }
  15. public function banner(Request $request)
  16. {
  17. $search['keyword'] = $request->input('keyword');
  18. $query = $this->repository->pushCriteria(new MultiWhere($search));
  19. $request = $request->all();
  20. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  21. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  22. }
  23. $list = $query->paginate();
  24. return view('admin.settings.banner.index',compact('list'));
  25. }
  26. public function bannerCreate(Request $reqeust)
  27. {
  28. if ($reqeust->method() == 'POST') {
  29. return $this->_createSave();
  30. }
  31. return view('admin.settings.banner.edit');
  32. }
  33. /**
  34. * 保存修改
  35. */
  36. private function _createSave(){
  37. $data = (array) request('data');
  38. $pic = (array) request('value');
  39. $data['category'] = 'banner';
  40. $data['key'] = '0';
  41. $data['pid'] = '0';
  42. $data['value'] = $pic[0];
  43. $id = $this->repository->create($data);
  44. if($id) {
  45. $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
  46. $url[] = array('url'=>U( 'setting/banner/create'),'title'=>'继续添加');
  47. $this->showMessage('添加成功',$url);
  48. }else{
  49. $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
  50. return $this->showWarning('添加失败',$url);
  51. }
  52. }
  53. /**
  54. *
  55. * 修改
  56. *
  57. *
  58. */
  59. public function bannerUpdate(Request $reqeust) {
  60. if($reqeust->method() == 'POST') {
  61. return $this->_updateSave();
  62. }
  63. $data = $this->repository->find($reqeust->get('id'));
  64. return view('admin.settings.banner.edit',compact('data'));
  65. }
  66. /**
  67. * 保存修改
  68. */
  69. private function _updateSave() {
  70. $data = (array) request('data');
  71. $data2 = $this->repository->find(request('id')) ;
  72. $old_pics = $data2->value;
  73. $pic = (array) request('value');
  74. if (!empty($pic)) {
  75. $data['value'] = $pic[0];
  76. if (is_file('.'.$old_pics)) {
  77. unlink('.'.$old_pics);
  78. }
  79. }
  80. $ok = $this->repository->update(request('id'),$data);
  81. if($ok) {
  82. $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
  83. return $this->showMessage('操作成功',urldecode(request('_referer')));
  84. }else{
  85. $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
  86. return $this->showWarning('操作失败',$url);
  87. }
  88. }
  89. public function bannerDestroy(Request $request)
  90. {
  91. $data2 = $this->repository->find(request('id')) ;
  92. $old_pics = $data2->value;
  93. $bool = $this->repository->destroy($request->id);
  94. if($bool) {
  95. if (is_file('.'.$old_pics)) {
  96. unlink('.'.$old_pics);
  97. }
  98. return $this->showMessage('操作成功');
  99. }else {
  100. return $this->showWarning("操作失败");
  101. }
  102. }
  103. // 参数设置
  104. public function paihangEdit(Request $reqeust) {
  105. if ($reqeust->method() == 'POST') {
  106. $data = (array) request('data');
  107. $result = BaseSettingsModel::where('category','paihang')->get();
  108. if (count($result) == 0) {
  109. $data['category'] = 'paihang';
  110. $data['sort'] = 0;
  111. $data['pid'] = 0;
  112. $ok = BaseSettingsModel::create($data);
  113. if ($ok) {
  114. return $this->showMessage('操作成功');
  115. }else{
  116. return $this->showWarning('操作失败');
  117. }
  118. }else {
  119. $ok = BaseSettingsModel::where('category', 'paihang')->update($data);
  120. if ($ok) {
  121. return $this->showMessage('操作成功');
  122. } else {
  123. return $this->showWarning('操作失败');
  124. }
  125. }
  126. }
  127. $data = BaseSettingsModel::where('category','paihang')->first();
  128. return view('admin.settings.paihang.edit',compact('data'));
  129. }
  130. public function scoreEdit(Request $reqeust) {
  131. if ($reqeust->method() == 'POST') {
  132. $data = (array) request('data');
  133. $result = BaseSettingsModel::where('category','score')->get();
  134. if (count($result) == 0) {
  135. $data['category'] = 'score';
  136. $data['sort'] = 0;
  137. $data['pid'] = 0;
  138. $ok = BaseSettingsModel::create($data);
  139. if ($ok) {
  140. return $this->showMessage('操作成功');
  141. }else{
  142. return $this->showWarning('操作失败');
  143. }
  144. }else {
  145. $ok = BaseSettingsModel::where('category', 'score')->update($data);
  146. if ($ok) {
  147. return $this->showMessage('操作成功');
  148. } else {
  149. return $this->showWarning('操作失败');
  150. }
  151. }
  152. }
  153. $data = BaseSettingsModel::where('category','score')->first();
  154. return view('admin.settings.score.edit',compact('data'));
  155. }
  156. }