InfoController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * 梦想列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2017-06-28 14:50:22
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Dream;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\BaseAttachmentModel;
  12. use App\Models\BaseSettingsModel;
  13. use App\Models\DreamImages;
  14. use App\Models\DreamInfoModel;
  15. use Illuminate\Http\Request;
  16. use App\Repositories\Base\Criteria\OrderBy;
  17. use App\Repositories\Dream\Criteria\MultiWhere;
  18. use App\Repositories\Dream\InfoRepository;
  19. class InfoController extends Controller
  20. {
  21. private $repository;
  22. public function __construct(InfoRepository $repository) {
  23. if(!$this->repository) $this->repository = $repository;
  24. }
  25. function index(Request $reqeust) {
  26. $search['keyword'] = $reqeust->input('keyword');
  27. $query = $this->repository->pushCriteria(new MultiWhere($search));
  28. $request = $reqeust->all();
  29. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  30. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  31. }
  32. $list = $query->paginate();
  33. return view('admin.dream.info.index',compact('list'));
  34. }
  35. function check(Request $reqeust) {
  36. $request = $reqeust->all();
  37. $search['keyword'] = $reqeust->input('keyword');
  38. $orderby = array();
  39. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  40. $orderby[$request['sort_field']] = $request['sort_field_by'];
  41. }
  42. $list = $this->repository->search($search,$orderby);
  43. return view('admin.dream.info.check',compact('list'));
  44. }
  45. /**
  46. * 添加
  47. *
  48. */
  49. public function create(Request $reqeust)
  50. {
  51. if($reqeust->method() == 'POST') {
  52. return $this->_createSave();
  53. }
  54. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  55. return view('admin.dream.info.edit',compact('signs'));
  56. }
  57. /**
  58. * 保存修改
  59. */
  60. private function _createSave(){
  61. $data = (array) request('data');
  62. $pics = (array) request('pic');
  63. if (empty($pics)) {
  64. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  65. return $this->showWarning('请添加图片',$url);
  66. }
  67. $id = DreamInfoModel::insertGetId($data);
  68. if($id) {
  69. $arr = [];
  70. foreach ($pics['url'] as $pic) {
  71. $arr[] = [
  72. 'pic'=>getenv('APP_URL').$pic,
  73. 'dream_id'=>$id,
  74. 'created_at'=>date("Y-m-d H:i:s"),
  75. 'updated_at'=>date("Y-m-d H:i:s"),
  76. ];
  77. }
  78. DreamImages::insert($arr);
  79. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  80. $url[] = array('url'=>U( 'Dream/Info/create'),'title'=>'继续添加');
  81. $this->showMessage('添加成功',$url);
  82. }else{
  83. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  84. return $this->showWarning('添加失败',$url);
  85. }
  86. }
  87. /**
  88. *
  89. * 修改
  90. *
  91. *
  92. */
  93. public function update(Request $reqeust) {
  94. if($reqeust->method() == 'POST') {
  95. return $this->_updateSave();
  96. }
  97. $data = $this->repository->find($reqeust->get('id'));
  98. $imgs = $data->imgs;
  99. $arr = [];
  100. foreach ($imgs as $pic) {
  101. $arr[] = $pic['pic'];
  102. }
  103. $data->imgs = $arr;
  104. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  105. return view('admin.dream.info.edit',compact('data','signs'));
  106. }
  107. /**
  108. * 保存修改
  109. */
  110. private function _updateSave() {
  111. $data = (array) request('data');
  112. $pics = (array) request('pic');
  113. if (empty($pics)) {
  114. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  115. return $this->showWarning('请添加图片',$url);
  116. }
  117. $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
  118. $old_pics = array_column($old_data_pics,'pic');
  119. $a = array_diff($pics['url'],$old_pics);
  120. $b = array_diff($old_pics,$pics['url']);
  121. if (!empty($a) || !empty($b)) { //有图片变化执行
  122. // 删除以前图片 重新插入
  123. if (!empty($b)) {
  124. foreach ($b as $old_pic){
  125. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  126. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  127. }
  128. BaseAttachmentModel::where('url',$old_pic)->delete();
  129. DreamImages::where('pic',$old_pic)->delete();
  130. }
  131. }
  132. $arr = []; //插入新的图片
  133. foreach ($a as $pic) {
  134. $arr[] = [
  135. 'pic'=>getenv('APP_URL').$pic,
  136. 'dream_id'=>request('id'),
  137. 'created_at'=>date('Y-m-d H:i:s'),
  138. 'updated_at'=>date('Y-m-d H:i:s'),
  139. ];
  140. }
  141. DreamImages::insert($arr);
  142. }
  143. $ok = $this->repository->update(request('id'),$data);
  144. if($ok) {
  145. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  146. return $this->showMessage('操作成功',urldecode(request('_referer')));
  147. }else{
  148. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  149. return $this->showWarning('操作失败',$url);
  150. }
  151. }
  152. public function view(Request $reqeust) {
  153. $data = $this->repository->find(request('id'));
  154. return view('admin.dream.info.view',compact('data'));
  155. }
  156. /**
  157. *
  158. * 状态改变
  159. *
  160. */
  161. public function status(Request $reqeust) {
  162. $ok = $this->repository->updateStatus(request('id'),request('status'));
  163. if($ok) {
  164. return $this->showMessage('操作成功');
  165. }else{
  166. return $this->showWarning('操作失败');
  167. }
  168. }
  169. /**
  170. * 删除
  171. */
  172. public function destroy(Request $reqeust) {
  173. $bool = $this->repository->destroy($reqeust->get('id'));
  174. if($bool) {
  175. $old_data_pics = DreamInfoModel::find($reqeust->get('id'))->imgs->toArray();
  176. $old_pics = array_column($old_data_pics,'pic');
  177. if (!empty($old_pics)) {
  178. foreach ($old_pics as $old_pic){
  179. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  180. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  181. }
  182. BaseAttachmentModel::where('url',$old_pic)->delete();
  183. }
  184. }
  185. DreamImages::where('dream_id',$reqeust->get('id'))->delete();
  186. return $this->showMessage('操作成功');
  187. }else{
  188. return $this->showWarning("操作失败");
  189. }
  190. }
  191. }