User.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\model\User as UserModel;
  14. use app\api\model\UserFans;
  15. use app\common\exception\BaseException;
  16. use app\api\model\UserCoupon as UserCouponModel;
  17. use app\api\service\User as UserService;
  18. use think\response\Json;
  19. /**
  20. * 用户管理
  21. * Class User
  22. * @package app\api
  23. */
  24. class User extends Controller
  25. {
  26. /**
  27. * 当前用户详情
  28. * @return Json
  29. * @throws BaseException
  30. */
  31. public function info(): Json
  32. {
  33. // 当前用户信息
  34. $userInfo = UserService::getCurrentLoginUser(true);
  35. // 获取用户头像
  36. $userInfo['avatar'];
  37. // 获取会员等级
  38. $userInfo['grade'];
  39. return $this->renderSuccess(compact('userInfo'));
  40. }
  41. /**
  42. * 当前用户详情
  43. * @return Json
  44. * @throws BaseException
  45. */
  46. public function detail(): Json
  47. {
  48. // 当前用户信息
  49. $userId = $this->request->param('user_id',0);
  50. if($userId>0){
  51. $userInfo = \app\api\model\User::detail($userId);
  52. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  53. }else{
  54. $userInfo = UserService::getCurrentLoginUser(true);
  55. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  56. }
  57. if(empty($userInfo)){
  58. return $this->renderSuccess('获取失败');
  59. }
  60. $info = [
  61. 'user_id'=> $userInfo['user_id'],
  62. 'nick_name'=> $userInfo['nick_name'],
  63. 'user_type'=> $userInfo['user_type'],
  64. 'avatar_url'=> $userInfo['avatar_url'],
  65. ];
  66. unset($userInfo['avatar']);
  67. return $this->renderSuccess(compact('info'));
  68. }
  69. /**
  70. * @return Json
  71. * @throws \cores\exception\BaseException
  72. */
  73. public function setInfo(): Json
  74. {
  75. $model = new UserModel;
  76. if (!$result = $model->saveInfo($this->request->param())) {
  77. return $this->renderSuccess($model->getError() ?: '保存失败');
  78. }
  79. return $this->renderSuccess($result? $result : '保存成功');
  80. }
  81. /**
  82. * 账户资产
  83. * @return Json
  84. * @throws BaseException
  85. */
  86. public function assets(): Json
  87. {
  88. // 当前用户信息
  89. $userInfo = UserService::getCurrentLoginUser(true);
  90. // 用户优惠券模型
  91. $model = new UserCouponModel;
  92. // 返回数据
  93. return $this->renderSuccess([
  94. 'assets' => [
  95. 'balance' => $userInfo['balance'], // 账户余额
  96. 'points' => $userInfo['points'], // 会员积分
  97. 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  98. ]
  99. ]);
  100. }
  101. /**
  102. * 手机号绑定
  103. * @return Json
  104. * @throws \cores\exception\BaseException
  105. */
  106. public function bindMobile(): Json
  107. {
  108. $model = new UserModel;
  109. if (!$model->bindMobile($this->postForm())) {
  110. return $this->renderSuccess($model->getError() ?: '操作失败');
  111. }
  112. return $this->renderSuccess('恭喜您,手机号绑定成功');
  113. }
  114. /**
  115. * 学长学姐列表
  116. * @return Json
  117. * @throws \cores\exception\BaseException
  118. * @throws \think\db\exception\DbException
  119. */
  120. public function seniorList()
  121. {
  122. // 当前用户信息
  123. $userInfo = UserService::getCurrentLoginUser(true);
  124. $info = isset($userInfo['info'])? $userInfo['info'] : [];
  125. $admissionYear = isset($info['admission_year'])? $info['admission_year'] : 0;
  126. $model = new UserModel;
  127. $params = $this->request->param();
  128. $params['admission_year'] = isset($params['admission_year'])? $params['admission_year'] : $admissionYear;
  129. $params['user_id'] = $userInfo['user_id'];
  130. $list = $model->getList($params, 15);
  131. return $this->renderSuccess(compact('list'));
  132. }
  133. /**
  134. * 获取关注用户列表
  135. * @return Json
  136. * @throws \cores\exception\BaseException
  137. * @throws \think\db\exception\DbException
  138. */
  139. public function followList()
  140. {
  141. // 当前用户信息
  142. $userInfo = UserService::getCurrentLoginUser(true);
  143. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  144. $model = new UserFans;
  145. $params = $this->request->param();
  146. $list = $model->getFollowList($userId, $params, 15);
  147. return $this->renderSuccess(compact('list'));
  148. }
  149. }