InfoController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // 'avatar' => 'dimensions:width=100,height=100'
  77. ],
  78. [
  79. 'data.address.required' => '请选择省市区地址',
  80. 'data.sex.required' => '请选择性别',
  81. 'data.phone.unique' => '电话号码唯一',
  82. 'data.emotion.required' => '请选择情感状态',
  83. ]
  84. );
  85. if($validator->fails()){
  86. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  87. return $this->showWarning($validator->messages()->all(),$url);
  88. }
  89. $avatar = request('avatar');
  90. $data = (array) request('data');
  91. $data['avatar'] = getenv('APP_URL').$avatar;
  92. $id = $this->repository->create($data);
  93. if($id) {
  94. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  95. $url[] = array('url'=>U( 'User/Info/create'),'title'=>'继续添加');
  96. $this->showMessage('添加成功',$url);
  97. }else{
  98. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  99. return $this->showWarning('添加失败',$url);
  100. }
  101. }
  102. return view('admin.user.info.edit');
  103. }
  104. /**
  105. * 保存修改
  106. */
  107. private function _createSave(){
  108. $avatar = request('avatar');
  109. $data = (array) request('data');
  110. $data['avatar'] = getenv('APP_URL').$avatar;
  111. $id = $this->repository->create($data);
  112. if($id) {
  113. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  114. $url[] = array('url'=>U( 'User/Info/create'),'title'=>'继续添加');
  115. $this->showMessage('添加成功',$url);
  116. }else{
  117. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  118. return $this->showWarning('添加失败',$url);
  119. }
  120. }
  121. /**
  122. *
  123. * 修改
  124. *
  125. *
  126. */
  127. public function update(Request $reqeust) {
  128. if($reqeust->method() == 'POST') {
  129. return $this->_updateSave();
  130. }
  131. $data = $this->repository->find($reqeust->get('id'));
  132. return view('admin.user.info.edit',compact('data'));
  133. }
  134. /**
  135. * 保存修改
  136. */
  137. private function _updateSave() {
  138. $old_avatar = $this->repository->find(request('id'))->avatar;
  139. $data = (array) request('data');
  140. if (!empty(request('avatar'))) {
  141. if (is_file('.'.str_replace(getenv('APP_URL'),'',$old_avatar))) {
  142. unlink('.'.str_replace(getenv('APP_URL'),'',$old_avatar));
  143. }
  144. BaseAttachmentModel::where('url',$old_avatar)->delete();
  145. $data['avatar'] = getenv('APP_URL').request('avatar');
  146. }
  147. $ok = $this->repository->update(request('id'),$data);
  148. if($ok) {
  149. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  150. return $this->showMessage('操作成功',urldecode(request('_referer')));
  151. }else{
  152. $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
  153. return $this->showWarning('操作失败',$url);
  154. }
  155. }
  156. public function view(Request $reqeust) {
  157. $data = $this->repository->find(request('id'));
  158. return view('admin.user.info.view',compact('data'));
  159. }
  160. /**
  161. *
  162. * 状态改变
  163. *
  164. */
  165. public function status(Request $reqeust) {
  166. $ok = $this->repository->updateStatus(request('id'),request('status'));
  167. if($ok) {
  168. return $this->showMessage('操作成功');
  169. }else{
  170. return $this->showWarning('操作失败');
  171. }
  172. }
  173. /**
  174. * 删除
  175. */
  176. public function destroy(Request $reqeust) {
  177. $bool = $this->repository->destroy($reqeust->get('id'));
  178. if($bool) {
  179. return $this->showMessage('操作成功');
  180. }else{
  181. return $this->showWarning("操作失败");
  182. }
  183. }
  184. }