UserController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. return $this->success();
  137. }
  138. /**
  139. * 取消关注
  140. *
  141. * @param Request $request
  142. * @return \Illuminate\Http\JsonResponse
  143. */
  144. public function cancelFollow(Request $request)
  145. {
  146. $user = User::where('id',$request->get('user_id'))->first();
  147. if (!$user){
  148. return $this->error('用户不存在!');
  149. }
  150. $collect = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->first();
  151. if(!$collect){
  152. return $this->error("您还未关注!");
  153. }
  154. $delete = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->delete();
  155. if (!$delete){
  156. return $this->error('取消关注失败!');
  157. }
  158. return $this->success();
  159. }
  160. /**
  161. * 删除用户
  162. */
  163. public function destroy(Request $request)
  164. {
  165. $user = User::query()->where('id',$this->userId)->first();
  166. if(!$user){
  167. return $this->error("用户不存在!");
  168. }
  169. $user->delete();
  170. return $this->success();
  171. }
  172. /**
  173. * 更新用户头像
  174. */
  175. public function avatar(Request $request)
  176. {
  177. $validator = Validator::make($request->all(), [
  178. 'avatar' => 'required',
  179. ]);
  180. if($validator->fails()){
  181. return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
  182. }
  183. $res = (new UserService())->update($this->userId,$request->all());
  184. return $this->success($this->userId);
  185. }
  186. //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错)
  187. public function groupByInitial(Request $request)
  188. {
  189. $keyword = $request->input('keyword', '');
  190. $list = User::query()
  191. ->select('id', 'name', 'avatar')
  192. ->when($keyword, function ($query, $keyword){
  193. $query->where('name', 'like', '%'.$keyword.'%');
  194. })
  195. ->whereNotNull('name')
  196. ->whereNull('deleted_at')
  197. ->get();
  198. if($list->isNotEmpty()){
  199. $list = $list->toArray();
  200. $list = SortService::getResultList($list);
  201. }else{
  202. $list = [];
  203. }
  204. return $this->success($list);
  205. }
  206. //判断是否允许解绑
  207. public function isAllowUnbound()
  208. {
  209. $user = $this->user;
  210. $n = 0;
  211. if(!empty($user->mobile)){
  212. $n++;
  213. }
  214. if(!empty($user->email)){
  215. $n++;
  216. }
  217. if(!empty($user->open_id)){
  218. $n++;
  219. }
  220. if($n==1){
  221. return false;
  222. }
  223. return true;
  224. }
  225. /**
  226. * 账号停用
  227. *
  228. * @return \Illuminate\Http\JsonResponse
  229. */
  230. public function accountStop(){
  231. if ($this->user->is_stop){
  232. return $this->error('当前账号已停用!');
  233. }
  234. $this->user->is_stop = 1;
  235. $this->user->save();
  236. return $this->success();
  237. }
  238. /**
  239. * 账号启用
  240. *
  241. * @return \Illuminate\Http\JsonResponse
  242. */
  243. public function accountOpen(){
  244. if ($this->user->is_stop == 0){
  245. return $this->error('当前账号已启用!');
  246. }
  247. $this->user->is_stop = 0;
  248. $this->user->save();
  249. return $this->success();
  250. }
  251. }