UserController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 show(Request $request)
  29. {
  30. $id = $request->get('id');
  31. if(!empty($id)){
  32. $id = $id;
  33. }else{
  34. $id = $this->userId;
  35. }
  36. $info = User::query()->where('id',$id)->first();
  37. if(!$info){
  38. return $this->error("用户不存在!");
  39. }
  40. // 会员是否到期
  41. if($info && !empty($info['end_time'])){
  42. if(time() > strtotime($info['end_time'])){
  43. $info->member_type = 1;
  44. $info->save();
  45. }
  46. }
  47. $info->follow_count = UserFollow::query()->where('user_id',$id)->count();
  48. $info->is_follow = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$id)->count();
  49. $info->ko_remaining_time = "0 일";
  50. $info->zh_remaining_time = "0 天";
  51. // 计算剩余时间
  52. if(strtotime($info->end_time) > time() && $info->member_type == 2){
  53. $info->ko_remaining_time = $this->koTimeCalculation(time(),strtotime($info->end_time));
  54. $info->zh_remaining_time = $this->zhTimeCalculation(time(),strtotime($info->end_time));
  55. }
  56. return $this->success($info);
  57. }
  58. /**
  59. * @param $beginDate 开始日期 2020-08-06 11:00:00
  60. * @param $endDate 结束日期 2020-08-10 12:10:01
  61. * @return string
  62. */
  63. public function koTimeCalculation($beginDate, $endDate)
  64. {
  65. $common = $endDate - $beginDate;
  66. $a = floor($common/86400/360); //整数年
  67. $b = floor($common/86400/30) - $a*12; //整数月
  68. $c = floor($common/86400) - $a*360 - $b*30; //整数日
  69. $d = floor($common/86400); //总的天数
  70. $str = '';
  71. if($a){
  72. $str = $a."년";
  73. }
  74. if($b){
  75. $str = $str.$b."월";
  76. }
  77. if($c){
  78. $str = $str.$c."일";
  79. }
  80. return $str;
  81. }
  82. /**
  83. * @param $beginDate 开始日期 2020-08-06 11:00:00
  84. * @param $endDate 结束日期 2020-08-10 12:10:01
  85. * @return string
  86. */
  87. public function zhTimeCalculation($beginDate, $endDate)
  88. {
  89. $common = $endDate - $beginDate;
  90. $a = floor($common/86400/360); //整数年
  91. $b = floor($common/86400/30) - $a*12; //整数月
  92. $c = floor($common/86400) - $a*360 - $b*30; //整数日
  93. $d = floor($common/86400); //总的天数
  94. $str = '';
  95. if($a){
  96. $str = $a."年";
  97. }
  98. if($b){
  99. $str = $str.$b."月";
  100. }
  101. if($c){
  102. $str = $str.$c."天";
  103. }
  104. return $str;
  105. }
  106. /**
  107. * 更新用户
  108. */
  109. public function upUserInfo(Request $request)
  110. {
  111. $res = (new UserService())->update($this->userId,$request->all());
  112. return $this->success($res);
  113. }
  114. /**
  115. * @return void
  116. * 添加关注
  117. */
  118. public function addFollow(Request $request){
  119. $user = User::query()->where('id',$request->get('user_id'))->first();
  120. if(!$user){
  121. return $this->error("用户不存在!");
  122. }
  123. $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  124. if($collect){
  125. return $this->error("您已关注过了!");
  126. }
  127. $data = [
  128. 'user_id' => $this->userId,
  129. 'to_user_id' => $user->id,
  130. ];
  131. $res = UserFollow::query()->create($data);
  132. if(!$res){
  133. return $this->error("操作失败!");
  134. }
  135. return $this->success();
  136. }
  137. /**
  138. * 删除用户
  139. */
  140. public function destroy(Request $request)
  141. {
  142. $user = User::query()->where('id',$this->userId)->first();
  143. if(!$user){
  144. return $this->error("用户不存在!");
  145. }
  146. $user->delete();
  147. return $this->success();
  148. }
  149. /**
  150. * 更新用户头像
  151. */
  152. public function avatar(Request $request)
  153. {
  154. $validator = Validator::make($request->all(), [
  155. 'avatar' => 'required',
  156. ]);
  157. if($validator->fails()){
  158. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  159. }
  160. $res = (new UserService())->update($this->userId,$request->all());
  161. return $this->success($this->userId);
  162. }
  163. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  164. public function groupByInitial(Request $request)
  165. {
  166. $keyword = $request->input('keyword', '');
  167. $list = User::query()
  168. ->select('id', 'name', 'avatar')
  169. ->when($keyword, function ($query, $keyword){
  170. $query->where('name', 'like', '%'.$keyword.'%');
  171. })
  172. ->whereNotNull('name')
  173. ->whereNull('deleted_at')
  174. ->get();
  175. if($list->isNotEmpty()){
  176. $list = $list->toArray();
  177. $list = SortService::getResultList($list);
  178. }else{
  179. $list = [];
  180. }
  181. return $this->success($list);
  182. }
  183. //判断是否允许解绑
  184. public function isAllowUnbound()
  185. {
  186. $user = $this->user;
  187. $n = 0;
  188. if(!empty($user->mobile)){
  189. $n++;
  190. }
  191. if(!empty($user->email)){
  192. $n++;
  193. }
  194. if(!empty($user->open_id)){
  195. $n++;
  196. }
  197. if($n==1){
  198. return false;
  199. }
  200. return true;
  201. }
  202. }