User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\UserDynamic;
  15. use app\api\model\UserFans;
  16. use app\common\exception\BaseException;
  17. use app\api\model\UserCoupon as UserCouponModel;
  18. use app\api\service\User as UserService;
  19. use app\common\model\StudentEduexp;
  20. use app\common\model\UserInfo;
  21. use think\response\Json;
  22. /**
  23. * 用户管理
  24. * Class User
  25. * @package app\api
  26. */
  27. class User extends Controller
  28. {
  29. /**
  30. * 当前用户详情
  31. * @return Json
  32. * @throws BaseException
  33. */
  34. public function info(): Json
  35. {
  36. // 当前用户信息
  37. $userInfo = UserService::getCurrentLoginUser(true);
  38. // 获取用户头像
  39. $userInfo['avatar'];
  40. // 获取会员等级
  41. $userInfo['grade'];
  42. return $this->renderSuccess(compact('userInfo'));
  43. }
  44. /**
  45. * 当前用户详情
  46. * @return Json
  47. * @throws BaseException
  48. */
  49. public function detail(): Json
  50. {
  51. // 当前用户信息
  52. $userId = $this->request->param('user_id',0);
  53. if($userId>0){
  54. $userInfo = \app\api\model\User::detail($userId);
  55. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  56. }else{
  57. $userInfo = UserService::getCurrentLoginUser(true);
  58. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  59. }
  60. if(empty($userInfo)){
  61. return $this->renderSuccess('获取失败');
  62. }
  63. $info = [
  64. 'user_id'=> $userInfo['user_id'],
  65. 'nick_name'=> $userInfo['nick_name'],
  66. 'user_type'=> $userInfo['user_type'],
  67. 'answer_num'=> $userInfo['answer_num'],
  68. 'score'=> $userInfo['score']? $userInfo['score'] : round(4+($userInfo['user_id']%2/5), 1),
  69. 'avatar_url'=> $userInfo['avatar_url'],
  70. ];
  71. unset($userInfo['avatar']);
  72. return $this->renderSuccess(compact('info'));
  73. }
  74. /**
  75. * 当前用户主页详情
  76. * @return Json
  77. * @throws BaseException
  78. */
  79. public function homeInfo(): Json
  80. {
  81. // 当前用户信息
  82. $userId = $this->request->param('user_id',0);
  83. if($userId>0){
  84. $userInfo = \app\api\model\User::detail($userId,['info']);
  85. if($userInfo){
  86. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  87. }
  88. }else{
  89. $userInfo = UserService::getCurrentLoginUser(true);
  90. $userInfo['avatar'] = isset($userInfo['avatar'])? $userInfo['avatar'] : [];
  91. }
  92. if(empty($userInfo)){
  93. return $this->renderSuccess('获取失败');
  94. }
  95. $position = isset($userInfo['info']['position'])? $userInfo['info']['position'] : '';
  96. $admissionYar = isset($userInfo['info']['admission_year'])? $userInfo['info']['admission_year'] : '';
  97. $info = [
  98. 'user_id'=> $userInfo['user_id'],
  99. 'nick_name'=> $userInfo['nick_name'],
  100. 'user_type'=> $userInfo['user_type'],
  101. 'avatar_url'=> $userInfo['avatar_url'],
  102. 'school_id'=> isset($userInfo['info']['school_id'])? $userInfo['info']['school_id'] : 0,
  103. 'school_name'=> isset($userInfo['info']['school_name'])? $userInfo['info']['school_name'] : '',
  104. 'admission_year'=> isset($userInfo['info']['admission_year'])? $userInfo['info']['admission_year'] : '',
  105. 'parent_name'=> isset($userInfo['info']['parent_name'])? $userInfo['info']['parent_name'] : '',
  106. 'position'=> $position,
  107. 'user_type_text'=> $userInfo['user_type']==3? ($position==1? '科任老师':'招生老师') : ($admissionYar? $admissionYar.'级':'学生'),
  108. 'home_bg'=> $userInfo['home_bg'],
  109. 'counts'=> UserDynamic::getCountsByUser($userInfo['user_id']),
  110. ];
  111. unset($userInfo['avatar']);
  112. return $this->renderSuccess(compact('info'));
  113. }
  114. /**
  115. * @return Json
  116. * @throws \cores\exception\BaseException
  117. */
  118. public function setInfo(): Json
  119. {
  120. $model = new UserModel;
  121. if (!$result = $model->saveInfo($this->request->param())) {
  122. return $this->renderSuccess($model->getError() ?: '保存失败');
  123. }
  124. return $this->renderSuccess($result? $result : '保存成功');
  125. }
  126. /**
  127. * 账户资产
  128. * @return Json
  129. * @throws BaseException
  130. */
  131. public function assets(): Json
  132. {
  133. // 当前用户信息
  134. $userInfo = UserService::getCurrentLoginUser(true);
  135. // 用户优惠券模型
  136. $model = new UserCouponModel;
  137. // 返回数据
  138. return $this->renderSuccess([
  139. 'assets' => [
  140. 'balance' => $userInfo['balance'], // 账户余额
  141. 'points' => $userInfo['points'], // 会员积分
  142. 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  143. ]
  144. ]);
  145. }
  146. /**
  147. * 教育经历
  148. * @return Json
  149. * @throws BaseException
  150. */
  151. public function education(): Json
  152. {
  153. // 当前用户信息
  154. $userInfo = UserService::getCurrentLoginUser(true);
  155. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  156. $info = StudentEduexp::getInfoByUser($userId);
  157. $info = $info? $info : [];
  158. return $this->renderSuccess(compact('info'));
  159. }
  160. /**
  161. * 绑定教育经历
  162. * @return Json
  163. * @throws BaseException
  164. */
  165. public function bindEducation(): Json
  166. {
  167. $model = new StudentEduexp();
  168. if(!$model->setData($this->request->param())){
  169. return $this->renderSuccess($model->getError() ?: '设置失败');
  170. }
  171. return $this->renderSuccess('您的学历已经提交,请等待审核绑定');
  172. }
  173. /**
  174. * 手机号绑定
  175. * @return Json
  176. * @throws \cores\exception\BaseException
  177. */
  178. public function bindMobile(): Json
  179. {
  180. $model = new UserModel;
  181. if (!$model->bindMobile($this->postForm())) {
  182. return $this->renderSuccess($model->getError() ?: '操作失败');
  183. }
  184. return $this->renderSuccess('恭喜您,手机号绑定成功');
  185. }
  186. /**
  187. * 学长学姐列表
  188. * @return Json
  189. * @throws \cores\exception\BaseException
  190. * @throws \think\db\exception\DbException
  191. */
  192. public function seniorList()
  193. {
  194. // 当前用户信息
  195. $userInfo = UserService::getCurrentLoginUser(true);
  196. $info = isset($userInfo['info'])? $userInfo['info'] : [];
  197. $admissionYear = isset($info['admission_year'])? $info['admission_year'] : 0;
  198. $model = new UserModel;
  199. $params = $this->request->param();
  200. $params['admission_year'] = isset($params['admission_year'])? $params['admission_year'] : $admissionYear;
  201. $params['user_id'] = $userInfo['user_id'];
  202. $list = $model->getList($params, 15);
  203. return $this->renderSuccess(compact('list'));
  204. }
  205. /**
  206. * 获取我关注的用户列表
  207. * @return Json
  208. * @throws \cores\exception\BaseException
  209. * @throws \think\db\exception\DbException
  210. */
  211. public function followList()
  212. {
  213. // 当前用户信息
  214. $userInfo = UserService::getCurrentLoginUser(true);
  215. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  216. $model = new UserFans;
  217. $params = $this->request->param();
  218. $list = $model->getFollowList($userId, $params, 15);
  219. return $this->renderSuccess(compact('list'));
  220. }
  221. /**
  222. * 获取关注我的用户列表
  223. * @return Json
  224. * @throws \cores\exception\BaseException
  225. * @throws \think\db\exception\DbException
  226. */
  227. public function fans()
  228. {
  229. // 当前用户信息
  230. $userInfo = UserService::getCurrentLoginUser(true);
  231. $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
  232. $model = new UserFans;
  233. $params = $this->request->param();
  234. $list = $model->getFansList($userId, $params, 15);
  235. return $this->renderSuccess(compact('list'));
  236. }
  237. /**
  238. * 设置头像或背景图片
  239. * @return Json
  240. * @throws \cores\exception\BaseException
  241. */
  242. public function setImage()
  243. {
  244. $model = new UserModel;
  245. if (!$model->setImage($this->request->param())) {
  246. return $this->renderSuccess($model->getError() ?: '设置失败');
  247. }
  248. return $this->renderSuccess('设置成功');
  249. }
  250. }