User.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <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\common\exception\BaseException;
  15. use app\api\model\UserCoupon as UserCouponModel;
  16. use app\api\service\User as UserService;
  17. use think\response\Json;
  18. /**
  19. * 用户管理
  20. * Class User
  21. * @package app\api
  22. */
  23. class User extends Controller
  24. {
  25. /**
  26. * 当前用户详情
  27. * @return Json
  28. * @throws BaseException
  29. */
  30. public function info(): Json
  31. {
  32. // 当前用户信息
  33. $userInfo = UserService::getCurrentLoginUser(true);
  34. // 获取用户头像
  35. $userInfo['avatar'];
  36. // 获取会员等级
  37. $userInfo['grade'];
  38. return $this->renderSuccess(compact('userInfo'));
  39. }
  40. /**
  41. * @return Json
  42. * @throws \cores\exception\BaseException
  43. */
  44. public function setInfo(): Json
  45. {
  46. $model = new UserModel;
  47. var_dump($this->postForm());
  48. if (!$result = $model->saveInfo($this->postForm()??[])) {
  49. return $this->renderSuccess($model->getError() ?: '保存失败');
  50. }
  51. return $this->renderSuccess($result? $result : '保存成功');
  52. }
  53. /**
  54. * 账户资产
  55. * @return Json
  56. * @throws BaseException
  57. */
  58. public function assets(): Json
  59. {
  60. // 当前用户信息
  61. $userInfo = UserService::getCurrentLoginUser(true);
  62. // 用户优惠券模型
  63. $model = new UserCouponModel;
  64. // 返回数据
  65. return $this->renderSuccess([
  66. 'assets' => [
  67. 'balance' => $userInfo['balance'], // 账户余额
  68. 'points' => $userInfo['points'], // 会员积分
  69. 'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
  70. ]
  71. ]);
  72. }
  73. /**
  74. * 手机号绑定
  75. * @return Json
  76. * @throws \cores\exception\BaseException
  77. */
  78. public function bindMobile(): Json
  79. {
  80. $model = new UserModel;
  81. if (!$model->bindMobile($this->postForm())) {
  82. return $this->renderSuccess($model->getError() ?: '操作失败');
  83. }
  84. return $this->renderSuccess('恭喜您,手机号绑定成功');
  85. }
  86. }