InfoController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $data['created_at'] = date('Y-m-d H:i:s');
  68. $data['updated_at'] = date('Y-m-d H:i:s');
  69. $id = DreamInfoModel::insertGetId($data);
  70. // 生成二维码
  71. /* $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  72. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  73. $code_url = env('APP_URL').'/user/meet?dream_id='.$id;
  74. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  75. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  76. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  77. DreamInfoModel::where('id',$id)->update(compact('code'));*/
  78. if($id) {
  79. $arr = [];
  80. foreach ($pics['url'] as $pic) {
  81. $arr[] = [
  82. 'pic'=>getenv('APP_URL').$pic,
  83. 'dream_id'=>$id,
  84. 'created_at'=>date("Y-m-d H:i:s"),
  85. 'updated_at'=>date("Y-m-d H:i:s"),
  86. ];
  87. }
  88. DreamImages::insert($arr);
  89. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  90. $url[] = array('url'=>U( 'Dream/Info/create'),'title'=>'继续添加');
  91. $this->showMessage('添加成功',$url);
  92. }else{
  93. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  94. return $this->showWarning('添加失败',$url);
  95. }
  96. }
  97. /**
  98. *
  99. * 修改
  100. *
  101. *
  102. */
  103. public function update(Request $reqeust) {
  104. if($reqeust->method() == 'POST') {
  105. return $this->_updateSave();
  106. }
  107. $data = $this->repository->find($reqeust->get('id'));
  108. $imgs = $data->imgs;
  109. $arr = [];
  110. foreach ($imgs as $pic) {
  111. $arr[] = $pic['pic'];
  112. }
  113. $data->imgs = $arr;
  114. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  115. return view('admin.dream.info.edit',compact('data','signs'));
  116. }
  117. /**
  118. * 保存修改
  119. */
  120. private function _updateSave() {
  121. $data = (array) request('data');
  122. $pics = (array) request('pic');
  123. if (empty($pics)) {
  124. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  125. return $this->showWarning('请添加图片',$url);
  126. }
  127. $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
  128. $old_pics = array_column($old_data_pics,'pic');
  129. $a = array_diff($pics['url'],$old_pics);
  130. $b = array_diff($old_pics,$pics['url']);
  131. if (!empty($a) || !empty($b)) { //有图片变化执行
  132. // 删除以前图片 重新插入
  133. if (!empty($b)) {
  134. foreach ($b as $old_pic){
  135. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  136. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  137. }
  138. BaseAttachmentModel::where('url',$old_pic)->delete();
  139. DreamImages::where('pic',$old_pic)->delete();
  140. }
  141. }
  142. $arr = []; //插入新的图片
  143. foreach ($a as $pic) {
  144. $arr[] = [
  145. 'pic'=>getenv('APP_URL').$pic,
  146. 'dream_id'=>request('id'),
  147. 'created_at'=>date('Y-m-d H:i:s'),
  148. 'updated_at'=>date('Y-m-d H:i:s'),
  149. ];
  150. }
  151. DreamImages::insert($arr);
  152. }
  153. $ok = $this->repository->update(request('id'),$data);
  154. if($ok) {
  155. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  156. return $this->showMessage('操作成功',urldecode(request('_referer')));
  157. }else{
  158. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  159. return $this->showWarning('操作失败',$url);
  160. }
  161. }
  162. public function view(Request $reqeust) {
  163. $data = $this->repository->find(request('id'));
  164. return view('admin.dream.info.view',compact('data'));
  165. }
  166. /**
  167. *
  168. * 状态改变
  169. *
  170. */
  171. public function status(Request $reqeust) {
  172. $ok = $this->repository->updateStatus(request('id'),request('status'));
  173. if($ok) {
  174. return $this->showMessage('操作成功');
  175. }else{
  176. return $this->showWarning('操作失败');
  177. }
  178. }
  179. /**
  180. * 删除
  181. */
  182. public function destroy(Request $reqeust) {
  183. $dream = DreamInfoModel::find($reqeust->get('id'));
  184. if($dream) {
  185. $old_data_pics = $dream->imgs->toArray();
  186. $old_pics = array_column($old_data_pics,'pic');
  187. if (!empty($old_pics)) {
  188. foreach ($old_pics as $old_pic){
  189. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  190. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  191. }
  192. BaseAttachmentModel::where('url',$old_pic)->delete();
  193. }
  194. BaseAttachmentModel::where('url',$old_pic)->delete();
  195. }
  196. DreamImages::where('dream_id',$reqeust->get('id'))->delete();
  197. $this->repository->destroy($reqeust->get('id'));
  198. return $this->showMessage('操作成功');
  199. }else{
  200. return $this->showWarning("操作失败");
  201. }
  202. }
  203. public function show_code(Request $request)
  204. {
  205. $code = $request->code;
  206. return view('admin.dream.info.show_code',compact('code'));
  207. }
  208. }