UserController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Couchbase\RegexpSearchQuery;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Validator;
  10. use App\Services\Base\ErrorCode;
  11. use PHPUnit\Util\Exception;
  12. use App\Services\SmsServer;
  13. class UserController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->user = auth('api')->user();
  18. $this->userId = $this->user ? $this->user->id : 0;
  19. //如果用户被删除,会自动退出登录
  20. if (!empty($this->user->deleted_at)) {
  21. $this->user->online = 0;
  22. $this->user->save();
  23. auth('api')->logout();
  24. }
  25. }
  26. /**
  27. * 获取用户详情
  28. */
  29. public function show(Request $request)
  30. {
  31. $id = $request->get('id');
  32. if(!empty($id)){
  33. $id = $id;
  34. }else{
  35. $id = $this->userId;
  36. }
  37. $info = User::query()->where('id',$id)->first();
  38. if(!$info){
  39. return $this->error("用户不存在!");
  40. }
  41. // 会员是否到期
  42. if($info && !empty($info['end_time'])){
  43. if(time() > strtotime($info['end_time'])){
  44. $info->member_type = 1;
  45. $info->save();
  46. }
  47. }
  48. $info->follow_count = UserFollow::query()->where('user_id',$id)->count();
  49. $info->is_follow = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$id)->count();
  50. $info->ko_remaining_time = "0 일";
  51. $info->zh_remaining_time = "0 天";
  52. // 计算剩余时间
  53. if(strtotime($info->end_time) > time() && $info->member_type == 2){
  54. $info->ko_remaining_time = $this->koTimeCalculation(time(),strtotime($info->end_time));
  55. $info->zh_remaining_time = $this->zhTimeCalculation(time(),strtotime($info->end_time));
  56. }
  57. return $this->success($info);
  58. }
  59. /**
  60. * @param $beginDate 开始日期 2020-08-06 11:00:00
  61. * @param $endDate 结束日期 2020-08-10 12:10:01
  62. * @return string
  63. */
  64. public function koTimeCalculation($beginDate, $endDate)
  65. {
  66. $common = $endDate - $beginDate;
  67. $a = floor($common/86400/360); //整数年
  68. $b = floor($common/86400/30) - $a*12; //整数月
  69. $c = floor($common/86400) - $a*360 - $b*30; //整数日
  70. $d = floor($common/86400); //总的天数
  71. $str = '';
  72. if($a){
  73. $str = $a."년";
  74. }
  75. if($b){
  76. $str = $str.$b."월";
  77. }
  78. if($c){
  79. $str = $str.$c."일";
  80. }
  81. return $str;
  82. }
  83. /**
  84. * @param $beginDate 开始日期 2020-08-06 11:00:00
  85. * @param $endDate 结束日期 2020-08-10 12:10:01
  86. * @return string
  87. */
  88. public function zhTimeCalculation($beginDate, $endDate)
  89. {
  90. $common = $endDate - $beginDate;
  91. $a = floor($common/86400/360); //整数年
  92. $b = floor($common/86400/30) - $a*12; //整数月
  93. $c = floor($common/86400) - $a*360 - $b*30; //整数日
  94. $d = floor($common/86400); //总的天数
  95. $str = '';
  96. if($a){
  97. $str = $a."年";
  98. }
  99. if($b){
  100. $str = $str.$b."月";
  101. }
  102. if($c){
  103. $str = $str.$c."天";
  104. }
  105. return $str;
  106. }
  107. /**
  108. * 更新用户
  109. */
  110. public function upUserInfo(Request $request)
  111. {
  112. $res = (new UserService())->update($this->userId,$request->all());
  113. return $this->success($res);
  114. }
  115. /**
  116. * @return void
  117. * 添加关注
  118. */
  119. public function addFollow(Request $request){
  120. $user = User::query()->where('id',$request->get('user_id'))->first();
  121. if(!$user){
  122. return $this->error("用户不存在!");
  123. }
  124. $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  125. if($collect){
  126. return $this->error("您已关注过了!");
  127. }
  128. $data = [
  129. 'user_id' => $this->userId,
  130. 'to_user_id' => $user->id,
  131. ];
  132. $res = UserFollow::query()->create($data);
  133. if(!$res){
  134. return $this->error("操作失败!");
  135. }
  136. User::where('id',$request->get('user_id'))->increment('follow_count');
  137. return $this->success();
  138. }
  139. /**
  140. * 取消关注
  141. *
  142. * @param Request $request
  143. * @return \Illuminate\Http\JsonResponse
  144. */
  145. public function cancelFollow(Request $request)
  146. {
  147. $user = User::where('id',$request->get('user_id'))->first();
  148. if (!$user){
  149. return $this->error('用户不存在!');
  150. }
  151. $collect = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  152. if(!$collect){
  153. return $this->error("您还未关注!");
  154. }
  155. $delete = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->delete();
  156. if (!$delete){
  157. return $this->error('取消关注失败!');
  158. }
  159. User::where('id',$request->get('user_id'))->decrement('follow_count');
  160. return $this->success();
  161. }
  162. /**
  163. * 删除用户
  164. */
  165. public function destroy(Request $request)
  166. {
  167. $user = User::query()->where('id',$this->userId)->first();
  168. if(!$user){
  169. return $this->error("用户不存在!");
  170. }
  171. $user->delete();
  172. return $this->success();
  173. }
  174. /**
  175. * 更新用户头像
  176. */
  177. public function avatar(Request $request)
  178. {
  179. $validator = Validator::make($request->all(), [
  180. 'avatar' => 'required',
  181. ]);
  182. if($validator->fails()){
  183. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  184. }
  185. $res = (new UserService())->update($this->userId,$request->all());
  186. return $this->success($this->userId);
  187. }
  188. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  189. public function groupByInitial(Request $request)
  190. {
  191. $keyword = $request->input('keyword', '');
  192. $list = User::query()
  193. ->select('id', 'name', 'avatar')
  194. ->when($keyword, function ($query, $keyword){
  195. $query->where('name', 'like', '%'.$keyword.'%');
  196. })
  197. ->whereNotNull('name')
  198. ->whereNull('deleted_at')
  199. ->get();
  200. if($list->isNotEmpty()){
  201. $list = $list->toArray();
  202. $list = SortService::getResultList($list);
  203. }else{
  204. $list = [];
  205. }
  206. return $this->success($list);
  207. }
  208. //判断是否允许解绑
  209. public function isAllowUnbound()
  210. {
  211. $user = $this->user;
  212. $n = 0;
  213. if(!empty($user->mobile)){
  214. $n++;
  215. }
  216. if(!empty($user->email)){
  217. $n++;
  218. }
  219. if(!empty($user->open_id)){
  220. $n++;
  221. }
  222. if($n==1){
  223. return false;
  224. }
  225. return true;
  226. }
  227. /**
  228. * 账号停用
  229. *
  230. * @return \Illuminate\Http\JsonResponse
  231. */
  232. public function accountStop(){
  233. if ($this->user->is_stop){
  234. return $this->error('当前账号已停用!');
  235. }
  236. $this->user->is_stop = 1;
  237. $this->user->save();
  238. return $this->success();
  239. }
  240. /**
  241. * 账号启用
  242. *
  243. * @return \Illuminate\Http\JsonResponse
  244. */
  245. public function accountOpen(){
  246. if ($this->user->is_stop == 0){
  247. return $this->error('当前账号已启用!');
  248. }
  249. $this->user->is_stop = 0;
  250. $this->user->save();
  251. return $this->success();
  252. }
  253. }