User.php 10 KB

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