AuthenticationController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Area;
  4. use App\Models\Authentication;
  5. use App\Models\Category;
  6. use App\Models\Tag;
  7. use App\Models\User;
  8. use Carbon\Carbon;
  9. use Illuminate\Http\Request;
  10. use App\Http\Requests;
  11. class AuthenticationController extends AdminController
  12. {
  13. protected $validateRules = [
  14. 'real_name' => 'required|max:64',
  15. 'title' => 'required|max:128',
  16. 'description' => 'sometimes|max:9999',
  17. 'id_card' => 'required|max:64|unique:authentications',
  18. 'id_card_image' => 'sometimes|image|max:2048',
  19. 'skill' => 'required|max:128',
  20. 'skill_image' => 'sometimes|image|max:2048',
  21. ];
  22. /**
  23. * Display a listing of the resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index(Request $request)
  28. {
  29. $query = Authentication::query();
  30. $filter = $request->all();
  31. $filter['category_id'] = $request->input('category_id',-1);
  32. /*认证申请状态过滤*/
  33. if(isset($filter['status']) && $filter['status'] > -1){
  34. $query->where('status','=',$filter['status']);
  35. }
  36. if( isset($filter['id_card']) && $filter['id_card']){
  37. $query->where('id_card','=',$filter['id_card']);
  38. }
  39. /*分类过滤*/
  40. if( $filter['category_id']> 0 ){
  41. $category = Category::findFromCache($filter['category_id']);
  42. if($category){
  43. $query->whereIn('category_id',$category->getSubIds());
  44. }
  45. }
  46. $authentications = $query->orderBy('updated_at','desc')->paginate(20);
  47. return view('admin.authentication.index')->with(compact('filter','authentications'));
  48. }
  49. public function create(){
  50. $provinces = Area::provinces();
  51. return view('admin.authentication.create')->with(compact('provinces'));
  52. }
  53. public function store(Request $request){
  54. $request->flash();
  55. $this->validateRules['user_id'] = 'required|integer|unique:authentications';
  56. $this->validate($request,$this->validateRules);
  57. $userId = $request->input('user_id');
  58. $authUser = User::find($userId);
  59. if(!$authUser){
  60. return $this->error(route('admin.authentication.create'),'申请认证的用户不存在,请核实user_id');
  61. }
  62. $data = $request->all();
  63. if(isset($data['is_recommend']) && $data['is_recommend'] ==1){
  64. $data['recommend_at'] = Carbon::now();
  65. }
  66. if($request->hasFile('id_card_image')){
  67. $savePath = storage_path('app/authentications');
  68. $file = $request->file('id_card_image');
  69. $fileName = uniqid(str_random(8)).'.'.$file->getClientOriginalExtension();
  70. $target = $file->move($savePath,$fileName);
  71. if($target){
  72. $data['id_card_image'] = 'authentications-'.$fileName;
  73. }
  74. }
  75. if($request->hasFile('skill_image')){
  76. $savePath = storage_path('app/authentications');
  77. $file = $request->file('skill_image');
  78. $fileName = uniqid(str_random(8)).'.'.$file->getClientOriginalExtension();
  79. $target = $file->move($savePath,$fileName);
  80. if($target){
  81. $data['skill_image'] = 'authentications-'.$fileName;
  82. }
  83. }
  84. $authentication = Authentication::create($data);
  85. if($authentication){
  86. Tag::multiSave($request->input('skill'),$request->user());
  87. }
  88. return $this->success(route('admin.authentication.index'),'行家认证信添加成功');
  89. }
  90. /**
  91. * Show the form for editing the specified resource.
  92. *
  93. * @param int $id
  94. * @return \Illuminate\Http\Response
  95. */
  96. public function edit($id)
  97. {
  98. $authentication = Authentication::find($id);
  99. $provinces = Area::provinces();
  100. $cities = Area::cities($authentication->province);
  101. $data = [
  102. 'provinces' => $provinces,
  103. 'cities' => $cities,
  104. ];
  105. return view('admin.authentication.edit')->with(compact('authentication','data'));
  106. }
  107. /**
  108. * Update the specified resource in storage.
  109. *
  110. * @param \Illuminate\Http\Request $request
  111. * @param int $id
  112. * @return \Illuminate\Http\Response
  113. */
  114. public function update(Request $request, $id)
  115. {
  116. $authentication = Authentication::find($id);
  117. if(!$authentication){
  118. return $this->error(route('admin.authentication.index'),'行家认证信息不存在,请核实');
  119. }
  120. $request->flash();
  121. $oldStatus = $authentication->status;
  122. $this->validateRules['id_card'] = 'required|max:64|unique:authentications,id_card,'.$authentication->user_id.',user_id';
  123. $this->validate($request,$this->validateRules);
  124. $data = $request->all();
  125. $data['recommend_at'] = null;
  126. if(isset($data['is_recommend']) && $data['is_recommend'] ==1){
  127. $data['recommend_at'] = Carbon::now();
  128. }
  129. if ($request->hasFile('id_card_image')) {
  130. $savePath = storage_path('app/authentications');
  131. $file = $request->file('id_card_image');
  132. $fileName = uniqid(str_random(8)) . '.' . $file->getClientOriginalExtension();
  133. $target = $file->move($savePath, $fileName);
  134. if ($target) {
  135. $data['id_card_image'] = 'authentications-' . $fileName;
  136. }
  137. }
  138. if ($request->hasFile('skill_image')) {
  139. $savePath = storage_path('app/authentications');
  140. $file = $request->file('skill_image');
  141. $fileName = uniqid(str_random(8)) . '.' . $file->getClientOriginalExtension();
  142. $target = $file->move($savePath, $fileName);
  143. if ($target) {
  144. $data['skill_image'] = 'authentications-' . $fileName;
  145. }
  146. }
  147. $result = $authentication->update($data);
  148. if($result){
  149. Tag::multiSave($request->input('skill'),$request->user());
  150. }
  151. return $this->success(route('admin.authentication.index'),'行家认证信息修改成功');
  152. }
  153. /*修改分类*/
  154. public function changeCategories(Request $request){
  155. $ids = $request->input('ids','');
  156. $categoryId = $request->input('category_id',0);
  157. if($ids){
  158. Authentication::whereIn('user_id',explode(",",$ids))->update(['category_id'=>$categoryId]);
  159. }
  160. return $this->success(route('admin.authentication.index'),'分类修改成功');
  161. }
  162. /**
  163. * Remove the specified resource from storage.
  164. *
  165. * @param int $id
  166. * @return \Illuminate\Http\Response
  167. */
  168. public function destroy(Request $request)
  169. {
  170. $ids = $request->input('id');
  171. Authentication::destroy($ids);
  172. return $this->success(route('admin.authentication.index'),'行家认证信息删除成功');
  173. }
  174. public function recommend(Request $request){
  175. $ids = $request->input('id');
  176. Authentication::whereIn('user_id', $ids)->where('status','>', 0)->update(['recommend_at'=>Carbon::now()]);
  177. return $this->success(route('admin.authentication.index'),'行家推荐显示成功!');
  178. }
  179. }