User.php 10.0 KB

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