User.php 10 KB

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