InfoController.php 7.8 KB

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