InfoController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 App\Models\SystemInfoModel;
  16. use Illuminate\Http\Request;
  17. use App\Repositories\Base\Criteria\OrderBy;
  18. use App\Repositories\Dream\Criteria\MultiWhere;
  19. use App\Repositories\Dream\InfoRepository;
  20. use App\Helper\JpushHelper;
  21. class InfoController extends Controller
  22. {
  23. use JpushHelper;
  24. private $repository;
  25. public function __construct(InfoRepository $repository) {
  26. if(!$this->repository) $this->repository = $repository;
  27. }
  28. function index(Request $reqeust) {
  29. $search['keyword'] = $reqeust->input('keyword');
  30. $query = $this->repository->pushCriteria(new MultiWhere($search));
  31. $request = $reqeust->all();
  32. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  33. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  34. }
  35. $list = $query->paginate();
  36. return view('admin.dream.info.index',compact('list'));
  37. }
  38. function check(Request $reqeust) {
  39. $request = $reqeust->all();
  40. $search['keyword'] = $reqeust->input('keyword');
  41. $orderby = array();
  42. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  43. $orderby[$request['sort_field']] = $request['sort_field_by'];
  44. }
  45. $list = $this->repository->search($search,$orderby);
  46. return view('admin.dream.info.check',compact('list'));
  47. }
  48. /**
  49. * 添加
  50. *
  51. */
  52. public function create(Request $reqeust)
  53. {
  54. if($reqeust->method() == 'POST') {
  55. return $this->_createSave();
  56. }
  57. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  58. return view('admin.dream.info.edit',compact('signs'));
  59. }
  60. /**
  61. * 保存修改
  62. */
  63. private function _createSave(){
  64. $data = (array) request('data');
  65. $pics = (array) request('pic');
  66. if (empty($pics)) {
  67. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  68. return $this->showWarning('请添加图片',$url);
  69. }
  70. $data['created_at'] = date('Y-m-d H:i:s');
  71. $data['updated_at'] = date('Y-m-d H:i:s');
  72. $id = DreamInfoModel::insertGetId($data);
  73. // 生成二维码
  74. /* $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  75. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  76. $code_url = env('APP_URL').'/user/meet?dream_id='.$id;
  77. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  78. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  79. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  80. DreamInfoModel::where('id',$id)->update(compact('code'));*/
  81. if($id) {
  82. $arr = [];
  83. foreach ($pics['url'] as $pic) {
  84. $arr[] = [
  85. 'pic'=>getenv('APP_URL').$pic,
  86. 'dream_id'=>$id,
  87. 'created_at'=>date("Y-m-d H:i:s"),
  88. 'updated_at'=>date("Y-m-d H:i:s"),
  89. ];
  90. }
  91. DreamImages::insert($arr);
  92. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  93. $url[] = array('url'=>U( 'Dream/Info/create'),'title'=>'继续添加');
  94. $this->showMessage('添加成功',$url);
  95. }else{
  96. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  97. return $this->showWarning('添加失败',$url);
  98. }
  99. }
  100. /**
  101. *
  102. * 修改
  103. *
  104. *
  105. */
  106. public function update(Request $reqeust) {
  107. if($reqeust->method() == 'POST') {
  108. return $this->_updateSave();
  109. }
  110. $data = $this->repository->find($reqeust->get('id'));
  111. $imgs = $data->imgs;
  112. $arr = [];
  113. foreach ($imgs as $pic) {
  114. $arr[] = $pic['pic'];
  115. }
  116. $data->imgs = $arr;
  117. $signs = BaseSettingsModel::where('category','sign')->orderBy('id')->get();
  118. return view('admin.dream.info.edit',compact('data','signs'));
  119. }
  120. /**
  121. * 保存修改
  122. */
  123. private function _updateSave() {
  124. $data = (array) request('data');
  125. $pics = (array) request('pic');
  126. if (empty($pics)) {
  127. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  128. return $this->showWarning('请添加图片',$url);
  129. }
  130. $old_data_pics = $this->repository->find(request('id'))->imgs->toArray();
  131. $old_pics = array_column($old_data_pics,'pic');
  132. $a = array_diff($pics['url'],$old_pics);
  133. $b = array_diff($old_pics,$pics['url']);
  134. if (!empty($a) || !empty($b)) { //有图片变化执行
  135. // 删除以前图片 重新插入
  136. if (!empty($b)) {
  137. foreach ($b as $old_pic){
  138. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  139. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  140. }
  141. BaseAttachmentModel::where('url',$old_pic)->delete();
  142. DreamImages::where('pic',$old_pic)->delete();
  143. }
  144. }
  145. $arr = []; //插入新的图片
  146. foreach ($a as $pic) {
  147. $arr[] = [
  148. 'pic'=>getenv('APP_URL').$pic,
  149. 'dream_id'=>request('id'),
  150. 'created_at'=>date('Y-m-d H:i:s'),
  151. 'updated_at'=>date('Y-m-d H:i:s'),
  152. ];
  153. }
  154. DreamImages::insert($arr);
  155. }
  156. $ok = $this->repository->update(request('id'),$data);
  157. if ($data['status'] == 1) { //审核通过
  158. $message = '你的梦想《'.$data['name'].'》已被批准,离你实现梦想又更进一步啦~ ';
  159. $info = [
  160. 'user_id' => $data['user_id'],
  161. 'message' => $message,
  162. ];
  163. SystemInfoModel::create($info);
  164. // 长连接
  165. $this->jPush($message,'',$data['user_id']);
  166. }
  167. if($ok) {
  168. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  169. return $this->showMessage('操作成功',urldecode(request('_referer')));
  170. }else{
  171. $url[] = array('url'=>U( 'Dream/Info/index'),'title'=>'返回列表');
  172. return $this->showWarning('操作失败',$url);
  173. }
  174. }
  175. public function view(Request $reqeust) {
  176. $data = $this->repository->find(request('id'));
  177. return view('admin.dream.info.view',compact('data'));
  178. }
  179. /**
  180. *
  181. * 状态改变
  182. *
  183. */
  184. public function status(Request $reqeust) {
  185. $ok = $this->repository->updateStatus(request('id'),request('status'));
  186. if($ok) {
  187. return $this->showMessage('操作成功');
  188. }else{
  189. return $this->showWarning('操作失败');
  190. }
  191. }
  192. /**
  193. * 删除
  194. */
  195. public function destroy(Request $reqeust) {
  196. $dream = DreamInfoModel::find($reqeust->get('id'));
  197. if($dream) {
  198. $old_data_pics = $dream->imgs->toArray();
  199. $old_pics = array_column($old_data_pics,'pic');
  200. if (!empty($old_pics)) {
  201. foreach ($old_pics as $old_pic){
  202. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_pic))) {
  203. unlink('.'.str_replace(getenv('APP_URL'),'',$old_pic));
  204. }
  205. BaseAttachmentModel::where('url',$old_pic)->delete();
  206. }
  207. BaseAttachmentModel::where('url',$old_pic)->delete();
  208. }
  209. DreamImages::where('dream_id',$reqeust->get('id'))->delete();
  210. $this->repository->destroy($reqeust->get('id'));
  211. return $this->showMessage('操作成功');
  212. }else{
  213. return $this->showWarning("操作失败");
  214. }
  215. }
  216. public function show_code(Request $request)
  217. {
  218. $code = $request->code;
  219. return view('admin.dream.info.show_code',compact('code'));
  220. }
  221. }