UserController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = User::query()->where('id',$this->userId)->first();
  49. // 会员是否到期
  50. if($info && !empty($info['end_time'])){
  51. if(time() > strtotime($info['end_time'])){
  52. $info->member_type = 1;
  53. $info->save();
  54. }
  55. }
  56. $info->follow_count = UserFollow::query()->where('user_id',$this->userId)->count();
  57. return $this->success($info);
  58. }
  59. /**
  60. * 新增用户
  61. */
  62. public function create(Request $request)
  63. {
  64. $validator = Validator::make($request->all(), [
  65. 'nickname' => 'required',
  66. 'avatar' => 'required',
  67. 'password' => 'required',
  68. 'email' => 'required',
  69. 'mobile' => 'required',
  70. ]);
  71. if($validator->fails()){
  72. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  73. }
  74. $res = (new UserService())->create($request->all());
  75. return $this->success($res);
  76. }
  77. /**
  78. * 更新用户
  79. */
  80. public function upUserInfo(Request $request)
  81. {
  82. // $validator = Validator::make($request->all(), [
  83. // 'nickname' => 'required',
  84. // 'name' => 'required',
  85. // 'introduce' => 'required',
  86. // ]);
  87. // if($validator->fails()){
  88. // return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  89. // }
  90. $res = (new UserService())->update($this->userId,$request->all());
  91. return $this->success($res);
  92. }
  93. /**
  94. * @return void
  95. * 添加关注
  96. */
  97. public function addFollow(Request $request){
  98. $user = User::query()->where('id',$request->get('user_id'))->first();
  99. if(!$user){
  100. return $this->error("用户不存在!");
  101. }
  102. $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  103. if($collect){
  104. return $this->error("您已关注过了!");
  105. }
  106. $data = [
  107. 'user_id' => $this->userId,
  108. 'to_user_id' => $user->id,
  109. ];
  110. $res = UserFollow::query()->create($data);
  111. if(!$res){
  112. return $this->error("操作失败!");
  113. }
  114. return $this->success();
  115. }
  116. /**
  117. * 删除用户
  118. */
  119. public function destroy()
  120. {
  121. $user = User::query()->where('id',$this->userId)->first();
  122. if(!$user){
  123. return $this->error("用户不存在!");
  124. }
  125. $user->delete();
  126. return $this->success();
  127. }
  128. /**
  129. * 更新用户头像
  130. */
  131. public function avatar(Request $request)
  132. {
  133. $validator = Validator::make($request->all(), [
  134. 'avatar' => 'required',
  135. ]);
  136. if($validator->fails()){
  137. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  138. }
  139. $res = (new UserService())->update($this->userId,$request->all());
  140. return $this->success($this->userId);
  141. }
  142. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  143. public function groupByInitial(Request $request)
  144. {
  145. $keyword = $request->input('keyword', '');
  146. $list = User::query()
  147. ->select('id', 'name', 'avatar')
  148. ->when($keyword, function ($query, $keyword){
  149. $query->where('name', 'like', '%'.$keyword.'%');
  150. })
  151. ->whereNotNull('name')
  152. ->whereNull('deleted_at')
  153. ->get();
  154. if($list->isNotEmpty()){
  155. $list = $list->toArray();
  156. $list = SortService::getResultList($list);
  157. }else{
  158. $list = [];
  159. }
  160. return $this->success($list);
  161. }
  162. //绑定或解绑 手机号
  163. public function bindMobile(Request $request)
  164. {
  165. try {
  166. if(!$this->isAllowUnbound()){
  167. return $this->output([],ErrorCode::NOT_ALLOW);
  168. }
  169. $user = User::find($this->user->id);
  170. if (!empty($user->mobile)) { //要解绑
  171. $user->mobile = '';
  172. } else { //要绑定
  173. $user->mobile = $request->mobile;
  174. }
  175. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  176. if (!$user->save()) {
  177. return $this->output([],ErrorCode::DATA_SAVE_FALSE);
  178. }
  179. } catch (\Exception $exception) {
  180. return $this->error($exception->getMessage());
  181. }
  182. return $this->success();
  183. }
  184. //判断是否允许解绑
  185. public function isAllowUnbound()
  186. {
  187. $user = $this->user;
  188. $n = 0;
  189. if(!empty($user->mobile)){
  190. $n++;
  191. }
  192. if(!empty($user->email)){
  193. $n++;
  194. }
  195. if(!empty($user->open_id)){
  196. $n++;
  197. }
  198. if($n==1){
  199. return false;
  200. }
  201. return true;
  202. }
  203. }