// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\controller; use app\api\model\User as UserModel; use app\api\model\UserFans; use app\common\exception\BaseException; use app\api\model\UserCoupon as UserCouponModel; use app\api\service\User as UserService; use app\common\model\UserInfo; use think\response\Json; /** * 用户管理 * Class User * @package app\api */ class User extends Controller { /** * 当前用户详情 * @return Json * @throws BaseException */ public function info(): Json { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 获取用户头像 $userInfo['avatar']; // 获取会员等级 $userInfo['grade']; return $this->renderSuccess(compact('userInfo')); } /** * 当前用户详情 * @return Json * @throws BaseException */ public function detail(): Json { // 当前用户信息 $userId = $this->request->param('user_id',0); if($userId>0){ $userInfo = \app\api\model\User::detail($userId); $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : []; }else{ $userInfo = UserService::getCurrentLoginUser(true); $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : []; } if(empty($userInfo)){ return $this->renderSuccess('获取失败'); } $info = [ 'user_id'=> $userInfo['user_id'], 'nick_name'=> $userInfo['nick_name'], 'user_type'=> $userInfo['user_type'], 'avatar_url'=> $userInfo['avatar_url'], ]; unset($userInfo['avatar']); return $this->renderSuccess(compact('info')); } /** * @return Json * @throws \cores\exception\BaseException */ public function setInfo(): Json { $model = new UserModel; if (!$result = $model->saveInfo($this->request->param())) { return $this->renderSuccess($model->getError() ?: '保存失败'); } return $this->renderSuccess($result? $result : '保存成功'); } /** * 账户资产 * @return Json * @throws BaseException */ public function assets(): Json { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); // 用户优惠券模型 $model = new UserCouponModel; // 返回数据 return $this->renderSuccess([ 'assets' => [ 'balance' => $userInfo['balance'], // 账户余额 'points' => $userInfo['points'], // 会员积分 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用) ] ]); } /** * 手机号绑定 * @return Json * @throws \cores\exception\BaseException */ public function bindMobile(): Json { $model = new UserModel; if (!$model->bindMobile($this->postForm())) { return $this->renderSuccess($model->getError() ?: '操作失败'); } return $this->renderSuccess('恭喜您,手机号绑定成功'); } /** * 学长学姐列表 * @return Json * @throws \cores\exception\BaseException * @throws \think\db\exception\DbException */ public function seniorList() { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); $info = isset($userInfo['info'])? $userInfo['info'] : []; $admissionYear = isset($info['admission_year'])? $info['admission_year'] : 0; $model = new UserModel; $params = $this->request->param(); $params['admission_year'] = isset($params['admission_year'])? $params['admission_year'] : $admissionYear; $params['user_id'] = $userInfo['user_id']; $list = $model->getList($params, 15); return $this->renderSuccess(compact('list')); } /** * 获取我关注的用户列表 * @return Json * @throws \cores\exception\BaseException * @throws \think\db\exception\DbException */ public function followList() { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0; $model = new UserFans; $params = $this->request->param(); $list = $model->getFollowList($userId, $params, 15); return $this->renderSuccess(compact('list')); } /** * 获取关注我的用户列表 * @return Json * @throws \cores\exception\BaseException * @throws \think\db\exception\DbException */ public function fans() { // 当前用户信息 $userInfo = UserService::getCurrentLoginUser(true); $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0; $model = new UserFans; $params = $this->request->param(); $list = $model->getFansList($userId, $params, 15); return $this->renderSuccess(compact('list')); } }