User.php 4.7 KB

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