User.php 3.5 KB

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