UserController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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(Request $request)
  47. {
  48. $id = $request->get('id');
  49. if(!empty($id)){
  50. $id = $id;
  51. }else{
  52. $id = $this->userId;
  53. }
  54. $info = User::query()->where('id',$id)->first();
  55. if(!$info){
  56. return $this->error("用户不存在!");
  57. }
  58. // 会员是否到期
  59. if($info && !empty($info['end_time'])){
  60. if(time() > strtotime($info['end_time'])){
  61. $info->member_type = 1;
  62. $info->save();
  63. }
  64. }
  65. $info->follow_count = UserFollow::query()->where('user_id',$id)->count();
  66. $info->is_follow = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$id)->count();
  67. $info->remaining_time = "0 天";
  68. // 计算剩余时间
  69. if(strtotime($info->end_time) > time() && $info->member_type == 2){
  70. $info->remaining_time = $this->timeCalculation(time(),strtotime($info->end_time));
  71. }
  72. return $this->success($info);
  73. }
  74. /**
  75. * @param $beginDate 开始日期 2020-08-06 11:00:00
  76. * @param $endDate 结束日期 2020-08-10 12:10:01
  77. * @return string
  78. */
  79. public function timeCalculation($beginDate, $endDate)
  80. {
  81. $common = $endDate - $beginDate;
  82. $a = floor($common/86400/360); //整数年
  83. $b = floor($common/86400/30) - $a*12; //整数月
  84. $c = floor($common/86400) - $a*360 - $b*30; //整数日
  85. $d = floor($common/86400); //总的天数
  86. $str = '';
  87. if($a){
  88. $str = $a."年";
  89. }
  90. if($b){
  91. $str = $str.$b."月";
  92. }
  93. if($c){
  94. $str = $str.$c."天";
  95. }
  96. return $str;
  97. }
  98. /**
  99. * 新增用户
  100. */
  101. public function create(Request $request)
  102. {
  103. $validator = Validator::make($request->all(), [
  104. 'nickname' => 'required',
  105. 'avatar' => 'required',
  106. 'password' => 'required',
  107. 'email' => 'required',
  108. 'mobile' => 'required',
  109. ]);
  110. if($validator->fails()){
  111. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  112. }
  113. $res = (new UserService())->create($request->all());
  114. return $this->success($res);
  115. }
  116. /**
  117. * 更新用户
  118. */
  119. public function upUserInfo(Request $request)
  120. {
  121. // $validator = Validator::make($request->all(), [
  122. // 'nickname' => 'required',
  123. // 'name' => 'required',
  124. // 'introduce' => 'required',
  125. // ]);
  126. // if($validator->fails()){
  127. // return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  128. // }
  129. $res = (new UserService())->update($this->userId,$request->all());
  130. return $this->success($res);
  131. }
  132. /**
  133. * @return void
  134. * 添加关注
  135. */
  136. public function addFollow(Request $request){
  137. $user = User::query()->where('id',$request->get('user_id'))->first();
  138. if(!$user){
  139. return $this->error("用户不存在!");
  140. }
  141. $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  142. if($collect){
  143. return $this->error("您已关注过了!");
  144. }
  145. $data = [
  146. 'user_id' => $this->userId,
  147. 'to_user_id' => $user->id,
  148. ];
  149. $res = UserFollow::query()->create($data);
  150. if(!$res){
  151. return $this->error("操作失败!");
  152. }
  153. return $this->success();
  154. }
  155. /**
  156. * 删除用户
  157. */
  158. public function destroy(Request $request)
  159. {
  160. $user = User::query()->where('id',$this->userId)->first();
  161. if(!$user){
  162. return $this->error("用户不存在!");
  163. }
  164. if(empty($request->email)){
  165. return $this->error("邮箱不能为空!"); // 邮箱不能为空
  166. }
  167. if(empty($request->code)){
  168. return $this->error("验证码不能为空!"); // 邮箱不能为空
  169. }
  170. if(!EmailController::isEmailCodeRight($request->email,$request->code)){
  171. return $this->error("验证码验证失败!");
  172. }
  173. $user->delete();
  174. return $this->success();
  175. }
  176. /**
  177. * 更新用户头像
  178. */
  179. public function avatar(Request $request)
  180. {
  181. $validator = Validator::make($request->all(), [
  182. 'avatar' => 'required',
  183. ]);
  184. if($validator->fails()){
  185. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  186. }
  187. $res = (new UserService())->update($this->userId,$request->all());
  188. return $this->success($this->userId);
  189. }
  190. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  191. public function groupByInitial(Request $request)
  192. {
  193. $keyword = $request->input('keyword', '');
  194. $list = User::query()
  195. ->select('id', 'name', 'avatar')
  196. ->when($keyword, function ($query, $keyword){
  197. $query->where('name', 'like', '%'.$keyword.'%');
  198. })
  199. ->whereNotNull('name')
  200. ->whereNull('deleted_at')
  201. ->get();
  202. if($list->isNotEmpty()){
  203. $list = $list->toArray();
  204. $list = SortService::getResultList($list);
  205. }else{
  206. $list = [];
  207. }
  208. return $this->success($list);
  209. }
  210. //绑定或解绑 手机号
  211. public function bindMobile(Request $request)
  212. {
  213. try {
  214. if(!$this->isAllowUnbound()){
  215. return $this->output([],ErrorCode::NOT_ALLOW);
  216. }
  217. $user = User::find($this->user->id);
  218. if (!empty($user->mobile)) { //要解绑
  219. $user->mobile = '';
  220. } else { //要绑定
  221. $user->mobile = $request->mobile;
  222. }
  223. SmsServer::checkSmsCodeByVerifyKey($request->mobile, $request->smsCode);
  224. if (!$user->save()) {
  225. return $this->output([],ErrorCode::DATA_SAVE_FALSE);
  226. }
  227. } catch (\Exception $exception) {
  228. return $this->error($exception->getMessage());
  229. }
  230. return $this->success();
  231. }
  232. //判断是否允许解绑
  233. public function isAllowUnbound()
  234. {
  235. $user = $this->user;
  236. $n = 0;
  237. if(!empty($user->mobile)){
  238. $n++;
  239. }
  240. if(!empty($user->email)){
  241. $n++;
  242. }
  243. if(!empty($user->open_id)){
  244. $n++;
  245. }
  246. if($n==1){
  247. return false;
  248. }
  249. return true;
  250. }
  251. }