InfoController.php 7.7 KB

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