UserController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\User;
  4. use App\Models\UserFollow;
  5. use App\Services\Api\SortService;
  6. use App\Services\Api\UserService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Validator;
  9. use App\Services\Base\ErrorCode;
  10. use PHPUnit\Util\Exception;
  11. use App\Services\SmsServer;
  12. class UserController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. $this->user = auth('api')->user();
  17. $this->userId = $this->user ? $this->user->id : 0;
  18. //如果用户被删除,会自动退出登录
  19. if (!empty($this->user->deleted_at)) {
  20. $this->user->online = 0;
  21. $this->user->save();
  22. auth('api')->logout();
  23. }
  24. }
  25. /**
  26. * 获取用户列表
  27. */
  28. public function index(Request $request)
  29. {
  30. $validator = Validator::make($request->all(), [
  31. 'nickname' => 'required',
  32. ]);
  33. if($validator->fails()){
  34. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  35. }
  36. $search = ['keyword'=>'1'];
  37. $where = ['nickname' => $request->nickname];
  38. $orderby = ['id'=>'desc','created_at'=>'desc'];
  39. $pagesize = 10;
  40. $list = (new UserService())->search($search,$where,$orderby,$pagesize);
  41. return $this->success($list);
  42. }
  43. /**
  44. * 获取用户详情
  45. */
  46. public function show()
  47. {
  48. $info = (new UserService())->show($this->userId);
  49. $info->follow_count = UserFollow::query()->where('user_id',$this->userId)->count();
  50. return $this->success($info);
  51. }
  52. /**
  53. * 新增用户
  54. */
  55. public function create(Request $request)
  56. {
  57. $validator = Validator::make($request->all(), [
  58. 'nickname' => 'required',
  59. 'avatar' => 'required',
  60. 'password' => 'required',
  61. 'email' => 'required',
  62. 'mobile' => 'required',
  63. ]);
  64. if($validator->fails()){
  65. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  66. }
  67. $res = (new UserService())->create($request->all());
  68. return $this->success($res);
  69. }
  70. /**
  71. * 更新用户
  72. */
  73. public function upUserInfo(Request $request)
  74. {
  75. $validator = Validator::make($request->all(), [
  76. 'nickname' => 'required',
  77. 'name' => 'required',
  78. 'introduce' => 'required',
  79. ]);
  80. if($validator->fails()){
  81. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  82. }
  83. $res = (new UserService())->update($this->userId,$request->all());
  84. return $this->success($res);
  85. }
  86. /**
  87. * @return void
  88. * 添加关注
  89. */
  90. public function addFollow(Request $request){
  91. $user = User::query()->where('id',$request->get('user_id'))->first();
  92. if(!$user){
  93. return $this->error("用户不存在!");
  94. }
  95. $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  96. if($collect){
  97. return $this->error("您已关注过了!");
  98. }
  99. $data =[
  100. 'user_id' => $this->userId,
  101. 'to_user_id' => $user->id,
  102. ];
  103. $res = UserFollow::query()->create($data);
  104. if(!$res){
  105. return $this->error("操作失败!");
  106. }
  107. return $this->success();
  108. }
  109. /**
  110. * 删除用户
  111. */
  112. public function destroy(Request $request,$id)
  113. {
  114. $res = (new UserService())->destroy($id);
  115. return $this->success($res);
  116. }
  117. /**
  118. * 更新用户头像
  119. */
  120. public function avatar(Request $request)
  121. {
  122. $validator = Validator::make($request->all(), [
  123. 'avatar' => 'required',
  124. ]);
  125. if($validator->fails()){
  126. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  127. }
  128. $res = (new UserService())->update($this->userId,$request->all());
  129. return $this->success($this->userId);
  130. }
  131. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  132. public function groupByInitial(Request $request)
  133. {
  134. $keyword = $request->input('keyword', '');
  135. $list = User::query()
  136. ->select('id', 'name', 'avatar')
  137. ->when($keyword, function ($query, $keyword){
  138. $query->where('name', 'like', '%'.$keyword.'%');
  139. })
  140. ->whereNotNull('name')
  141. ->whereNull('deleted_at')
  142. ->get();
  143. if($list->isNotEmpty()){
  144. $list = $list->toArray();
  145. $list = SortService::getResultList($list);
  146. }else{
  147. $list = [];
  148. }
  149. return $this->success($list);
  150. }
  151. //绑定或解绑 手机号
  152. public function bindMobile(Request $request)
  153. {
  154. try {
  155. if(!$this->isAllowUnbound()){
  156. return $this->output([],ErrorCode::NOT_ALLOW);
  157. }
  158. $user = User::find($this->user->id);
  159. if (!empty($user->mobile)) { //要解绑
  160. $user->mobile = '';
  161. } else { //要绑定
  162. $user->mobile = $request->mobile;
  163. }
  164. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  165. if (!$user->save()) {
  166. return $this->output([],ErrorCode::DATA_SAVE_FALSE);
  167. }
  168. } catch (\Exception $exception) {
  169. return $this->error($exception->getMessage());
  170. }
  171. return $this->success();
  172. }
  173. //判断是否允许解绑
  174. public function isAllowUnbound()
  175. {
  176. $user = $this->user;
  177. $n = 0;
  178. if(!empty($user->mobile)){
  179. $n++;
  180. }
  181. if(!empty($user->email)){
  182. $n++;
  183. }
  184. if(!empty($user->open_id)){
  185. $n++;
  186. }
  187. if($n==1){
  188. return false;
  189. }
  190. return true;
  191. }
  192. }