Profile.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Accounts;
  5. use app\common\service\AccountService;
  6. use app\common\service\CategoryService;
  7. use app\common\service\DemandBiddingService;
  8. use app\common\service\DemandService;
  9. use app\common\service\GoodsService;
  10. use app\common\model\Category;
  11. use app\common\service\OrderService;
  12. use app\common\service\UserService;
  13. use fast\Tree;
  14. use think\Exception;
  15. use think\exception\HttpResponseException;
  16. use think\Validate;
  17. class Profile extends Api
  18. {
  19. protected $noNeedLogin = [];
  20. protected $noNeedRight = ['*'];
  21. /**
  22. * 获取用户中心数据
  23. *
  24. * @ApiMethod (POST)
  25. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  26. * @ApiParams (name="newpassword", type="string", required=true, description="新密码")
  27. * @ApiParams (name="captcha", type="string", required=true, description="验证码")
  28. */
  29. public function userCenter()
  30. {
  31. $userInfo = $this->auth->getUserinfo();
  32. $userInfo = [
  33. 'id' => $userInfo['id'],
  34. 'avatarUrl' => $userInfo['avatar'] ?: $this->request->domain() . '/uploads/avatar/default.png',
  35. 'nickName' => $userInfo['nickname'],
  36. 'phoneNumber' => '',
  37. 'gender' => $userInfo['gender'],
  38. 'role' => $userInfo['role'],
  39. ];
  40. $publishCount = DemandService::where('user_id', $userInfo['id'])->count();
  41. $quoteCount = DemandBiddingService::where(['user_id' => $userInfo['id'], 'bidding_status' => DemandBiddingService::STATUS_BIDDING_WIN])->count();
  42. $balance = AccountService::where('user_id', $userInfo['id'])->field(['user_id', 'amount', 'freeze', '(amount + freeze) AS total_amount'])->find();
  43. $countsData = [
  44. ['num' => $publishCount, 'name' => '我发布的需求', 'type' => 'publish'],
  45. ['num' => $quoteCount, 'name' => '我投标的需求', 'type' => 'quote'],
  46. ['num' => $balance->total_amount ?? 0, 'name' => '余额', 'type' => 'balance'],
  47. ['num' => 0, 'name' => '优惠券', 'type' => 'coupon'],
  48. ];
  49. $orderTagInfos = OrderService::getOrderCount($userInfo['id']);
  50. $customerServiceInfo = [
  51. 'servicePhone' => '4006336868',
  52. 'serviceTimeDuration' => '每周三至周五 9:00-12:00 13:00-15:00',
  53. ];
  54. $data = [
  55. 'userInfo' => $userInfo,
  56. 'countsData' => $countsData,
  57. 'orderTagInfos' => $orderTagInfos,
  58. 'customerServiceInfo' => $customerServiceInfo,
  59. ];
  60. $this->success('success', $data);
  61. }
  62. public function personInfo()
  63. {
  64. $userInfo = $this->auth->getUserinfo();
  65. $data = [
  66. 'avatarUrl' => $userInfo['avatar'] ?: $this->request->domain() . '/uploads/avatar/default.png',
  67. 'nickName' => $userInfo['nickname'],
  68. 'phoneNumber' => '',
  69. 'gender' => $userInfo['gender'],
  70. 'role' => $userInfo['role'],
  71. 'address' => [
  72. 'provinceName' => '',
  73. 'provinceCode' => '',
  74. 'cityName' => '',
  75. 'cityCode' => '',
  76. ],
  77. ];
  78. $this->success('success', $data);
  79. }
  80. public function setPersonInfo()
  81. {
  82. $userId = $this->auth->id ?? -1;
  83. $params = $this->request->only(['avatar', 'nickname', 'gender', 'mobile']);
  84. $data = array_filter($params);
  85. $res = UserService::where('id', $userId)->update($data);
  86. if ($res) {
  87. $this->success('success', $data);
  88. }
  89. $this->error('更新失败!');
  90. }
  91. }