InfoController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * 用户表
  4. * @author system
  5. * @version 1.0
  6. * @date 2017-05-30 12:16:56
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\User;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\BaseAttachmentModel;
  12. use App\Models\SupportDreamModel;
  13. use Illuminate\Http\Request;
  14. use App\Repositories\Base\Criteria\OrderBy;
  15. use App\Repositories\User\Criteria\MultiWhere;
  16. use App\Repositories\User\InfoRepository;
  17. class InfoController extends Controller
  18. {
  19. private $repository;
  20. public function __construct(InfoRepository $repository) {
  21. if(!$this->repository) $this->repository = $repository;
  22. }
  23. function index(Request $reqeust) {
  24. $search['keyword'] = $reqeust->input('keyword');
  25. $query = $this->repository->pushCriteria(new MultiWhere($search));
  26. $request = $reqeust->all();
  27. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  28. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  29. }
  30. $list = $query->paginate();
  31. foreach ($list as $item) {
  32. $dream_number = count($item->dreams);
  33. $score_array = SupportDreamModel::where('user_id',$item->id)->select('score')->get()->toArray();
  34. $coin_array = $item->dreams->toArray();
  35. if (count($coin_array) > 0) {
  36. $coin_list = array_column($coin_array,'get_coin');
  37. $get_coin = array_sum($coin_list);
  38. }else{
  39. $get_coin = 0;
  40. }
  41. if (count($score_array) > 0) {
  42. $score_list = array_column($score_array,'score');
  43. $sup_score = array_sum($score_list);
  44. }else{
  45. $sup_score = 0;
  46. }
  47. $item->dream_number = $dream_number; //梦想数量
  48. $item->sup_score = $sup_score; //捐赠分数
  49. $item->get_coin = $get_coin; //收到的梦想币
  50. }
  51. return view('admin.user.info.index',compact('list'));
  52. }
  53. function check(Request $reqeust) {
  54. $request = $reqeust->all();
  55. $search['keyword'] = $reqeust->input('keyword');
  56. $orderby = array();
  57. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  58. $orderby[$request['sort_field']] = $request['sort_field_by'];
  59. }
  60. $list = $this->repository->search($search,$orderby);
  61. return view('admin.user.info.check',compact('list'));
  62. }
  63. /**
  64. * 添加
  65. *
  66. */
  67. public function create(Request $reqeust)
  68. {
  69. if($reqeust->method() == 'POST') {
  70. $validator = \Validator::make($reqeust->all(),
  71. [
  72. 'data.address' => 'required',
  73. 'data.phone' => 'required|unique:user_info,phone',
  74. 'data.sex' => 'required',
  75. 'data.emotion' => 'required',
  76. ],
  77. [
  78. 'data.address.required' => '请选择省市区地址',
  79. 'data.sex.required' => '请选择性别',
  80. 'data.phone.unique' => '电话号码唯一',
  81. 'data.emotion.required' => '请选择情感状态',
  82. ]
  83. );
  84. if($validator->fails()){
  85. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  86. return $this->showWarning($validator->messages()->all(),$url);
  87. }
  88. $avatar = request('avatar');
  89. $data = (array) request('data');
  90. $data['avatar'] = getenv('APP_URL').$avatar;
  91. $id = $this->repository->create($data);
  92. if($id) {
  93. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  94. $url[] = array('url'=>U( 'User/Info/create'),'title'=>'继续添加');
  95. $this->showMessage('添加成功',$url);
  96. }else{
  97. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  98. return $this->showWarning('添加失败',$url);
  99. }
  100. }
  101. return view('admin.user.info.edit');
  102. }
  103. /**
  104. * 保存修改
  105. */
  106. private function _createSave(){
  107. $avatar = request('avatar');
  108. $data = (array) request('data');
  109. $data['avatar'] = getenv('APP_URL').$avatar;
  110. $id = $this->repository->create($data);
  111. if($id) {
  112. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  113. $url[] = array('url'=>U( 'User/Info/create'),'title'=>'继续添加');
  114. $this->showMessage('添加成功',$url);
  115. }else{
  116. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  117. return $this->showWarning('添加失败',$url);
  118. }
  119. }
  120. /**
  121. *
  122. * 修改
  123. *
  124. *
  125. */
  126. public function update(Request $reqeust) {
  127. if($reqeust->method() == 'POST') {
  128. return $this->_updateSave();
  129. }
  130. $data = $this->repository->find($reqeust->get('id'));
  131. return view('admin.user.info.edit',compact('data'));
  132. }
  133. /**
  134. * 保存修改
  135. */
  136. private function _updateSave() {
  137. $old_avatar = $this->repository->find(request('id'))->avatar;
  138. $data = (array) request('data');
  139. if (!empty(request('avatar'))) {
  140. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_avatar))) {
  141. unlink('.'.str_replace(getenv('APP_URL'),'',$old_avatar));
  142. }
  143. BaseAttachmentModel::where('url',$old_avatar)->delete();
  144. $data['avatar'] = getenv('APP_URL').request('avatar');
  145. }
  146. $ok = $this->repository->update(request('id'),$data);
  147. if($ok) {
  148. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  149. return $this->showMessage('操作成功',urldecode(request('_referer')));
  150. }else{
  151. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  152. return $this->showWarning('操作失败',$url);
  153. }
  154. }
  155. public function view(Request $reqeust) {
  156. $data = $this->repository->find(request('id'));
  157. return view('admin.user.info.view',compact('data'));
  158. }
  159. /**
  160. *
  161. * 状态改变
  162. *
  163. */
  164. public function status(Request $reqeust) {
  165. $ok = $this->repository->updateStatus(request('id'),request('status'));
  166. if($ok) {
  167. return $this->showMessage('操作成功');
  168. }else{
  169. return $this->showWarning('操作失败');
  170. }
  171. }
  172. /**
  173. * 删除
  174. */
  175. public function destroy(Request $reqeust) {
  176. $bool = $this->repository->destroy($reqeust->get('id'));
  177. if($bool) {
  178. return $this->showMessage('操作成功');
  179. }else{
  180. return $this->showWarning("操作失败");
  181. }
  182. }
  183. }