User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 app\common\model\UserInfo;
  19. use think\response\Json;
  20. /**
  21. * 用户管理
  22. * Class User
  23. * @package app\api
  24. */
  25. class User extends Controller
  26. {
  27. /**
  28. * 当前用户详情
  29. * @return Json
  30. * @throws BaseException
  31. */
  32. public function info(): Json
  33. {
  34. // 当前用户信息
  35. $userInfo = UserService::getCurrentLoginUser(true);
  36. // 获取用户头像
  37. $userInfo['avatar'];
  38. // 获取会员等级
  39. $userInfo['grade'];
  40. return $this->renderSuccess(compact('userInfo'));
  41. }
  42. /**
  43. * 当前用户详情
  44. * @return Json
  45. * @throws BaseException
  46. */
  47. public function detail(): Json
  48. {
  49. // 当前用户信息
  50. $userId = $this->request->param('user_id',0);
  51. if($userId>0){
  52. $userInfo = \app\api\model\User::detail($userId);
  53. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  54. }else{
  55. $userInfo = UserService::getCurrentLoginUser(true);
  56. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  57. }
  58. if(empty($userInfo)){
  59. return $this->renderSuccess('获取失败');
  60. }
  61. $info = [
  62. 'user_id'=> $userInfo['user_id'],
  63. 'nick_name'=> $userInfo['nick_name'],
  64. 'user_type'=> $userInfo['user_type'],
  65. 'avatar_url'=> $userInfo['avatar_url'],
  66. ];
  67. unset($userInfo['avatar']);
  68. return $this->renderSuccess(compact('info'));
  69. }
  70. /**
  71. * @return Json
  72. * @throws \cores\exception\BaseException
  73. */
  74. public function setInfo(): Json
  75. {
  76. $model = new UserModel;
  77. if (!$result = $model->saveInfo($this->request->param())) {
  78. return $this->renderSuccess($model->getError() ?: '保存失败');
  79. }
  80. return $this->renderSuccess($result? $result : '保存成功');
  81. }
  82. /**
  83. * 账户资产
  84. * @return Json
  85. * @throws BaseException
  86. */
  87. public function assets(): Json
  88. {
  89. // 当前用户信息
  90. $userInfo = UserService::getCurrentLoginUser(true);
  91. // 用户优惠券模型
  92. $model = new UserCouponModel;
  93. // 返回数据
  94. return $this->renderSuccess([
  95. 'assets' => [
  96. 'balance' => $userInfo['balance'], // 账户余额
  97. 'points' => $userInfo['points'], // 会员积分
  98. 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  99. ]
  100. ]);
  101. }
  102. /**
  103. * 手机号绑定
  104. * @return Json
  105. * @throws \cores\exception\BaseException
  106. */
  107. public function bindMobile(): Json
  108. {
  109. $model = new UserModel;
  110. if (!$model->bindMobile($this->postForm())) {
  111. return $this->renderSuccess($model->getError() ?: '操作失败');
  112. }
  113. return $this->renderSuccess('恭喜您,手机号绑定成功');
  114. }
  115. /**
  116. * 学长学姐列表
  117. * @return Json
  118. * @throws \cores\exception\BaseException
  119. * @throws \think\db\exception\DbException
  120. */
  121. public function seniorList()
  122. {
  123. // 当前用户信息
  124. $userInfo = UserService::getCurrentLoginUser(true);
  125. $info = isset($userInfo['info'])? $userInfo['info'] : [];
  126. $admissionYear = isset($info['admission_year'])? $info['admission_year'] : 0;
  127. $model = new UserModel;
  128. $params = $this->request->param();
  129. $params['admission_year'] = isset($params['admission_year'])? $params['admission_year'] : $admissionYear;
  130. $params['user_id'] = $userInfo['user_id'];
  131. $list = $model->getList($params, 15);
  132. return $this->renderSuccess(compact('list'));
  133. }
  134. /**
  135. * 获取我关注的用户列表
  136. * @return Json
  137. * @throws \cores\exception\BaseException
  138. * @throws \think\db\exception\DbException
  139. */
  140. public function followList()
  141. {
  142. // 当前用户信息
  143. $userInfo = UserService::getCurrentLoginUser(true);
  144. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  145. $model = new UserFans;
  146. $params = $this->request->param();
  147. $list = $model->getFollowList($userId, $params, 15);
  148. return $this->renderSuccess(compact('list'));
  149. }
  150. /**
  151. * 获取关注我的用户列表
  152. * @return Json
  153. * @throws \cores\exception\BaseException
  154. * @throws \think\db\exception\DbException
  155. */
  156. public function fans()
  157. {
  158. // 当前用户信息
  159. $userInfo = UserService::getCurrentLoginUser(true);
  160. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  161. $model = new UserFans;
  162. $params = $this->request->param();
  163. $list = $model->getFansList($userId, $params, 15);
  164. return $this->renderSuccess(compact('list'));
  165. }
  166. }