User.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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\store\controller\store;
  13. use think\response\Json;
  14. use app\store\controller\Controller;
  15. use app\store\model\store\User as StoreUserModel;
  16. use app\store\service\store\User as StoreUserService;
  17. use app\store\service\store\Role as StoreRoleService;
  18. /**
  19. * 商家用户控制器
  20. * Class StoreUser
  21. * @package app\store\controller
  22. */
  23. class User extends Controller
  24. {
  25. /**
  26. * 获取当前登录的用户信息
  27. * @return Json
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public function info(): Json
  33. {
  34. // 当前登录用户的ID
  35. $storeUserId = StoreUserService::getLoginUserId();
  36. return $this->renderSuccess([
  37. // 用户信息
  38. 'userInfo' => StoreUserModel::detail($storeUserId),
  39. // 菜单权限
  40. 'roles' => StoreRoleService::getLoginPermissions(),
  41. ]);
  42. }
  43. /**
  44. * 管理员列表
  45. * @return Json
  46. * @throws \think\db\exception\DbException
  47. */
  48. public function list(): Json
  49. {
  50. $model = new StoreUserModel;
  51. $list = $model->getList($this->request->param());
  52. return $this->renderSuccess(compact('list'));
  53. }
  54. /**
  55. * 添加管理员
  56. * @return Json
  57. */
  58. public function add(): Json
  59. {
  60. // 新增记录
  61. $model = new StoreUserModel;
  62. if ($model->add($this->postForm())) {
  63. return $this->renderSuccess('添加成功');
  64. }
  65. return $this->renderError($model->getError() ?: '添加失败');
  66. }
  67. /**
  68. * 更新管理员
  69. * @param int $userId
  70. * @return Json
  71. */
  72. public function edit(int $userId): Json
  73. {
  74. // 管理员详情
  75. $model = StoreUserModel::detail($userId);
  76. // 更新记录
  77. if ($model->edit($this->postForm())) {
  78. return $this->renderSuccess('更新成功');
  79. }
  80. return $this->renderError($model->getError() ?: '更新失败');
  81. }
  82. /**
  83. * 删除管理员
  84. * @param int $userId
  85. * @return Json
  86. */
  87. public function delete(int $userId): Json
  88. {
  89. // 管理员详情
  90. $model = StoreUserModel::detail($userId);
  91. if (!$model->setDelete()) {
  92. return $this->renderError($model->getError() ?: '删除失败');
  93. }
  94. return $this->renderSuccess('删除成功');
  95. }
  96. /**
  97. * 更新当前管理员信息
  98. * @return Json
  99. */
  100. public function renew(): Json
  101. {
  102. // 管理员详情
  103. $model = StoreUserModel::detail(StoreUserService::getLoginUserId());
  104. // 更新当前管理员信息
  105. if ($model->renew($this->postForm())) {
  106. return $this->renderSuccess('更新成功');
  107. }
  108. return $this->renderError($model->getError() ?: '更新失败');
  109. }
  110. }