User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\controller\shopro\user;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use addons\shopro\service\Wallet as WalletService;
  6. use app\admin\model\shopro\user\User as UserModel;
  7. use app\admin\model\shopro\user\Coupon as UserCouponModel;
  8. class User extends Common
  9. {
  10. protected $model = null;
  11. protected $noNeedRight = ['select'];
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. $this->model = new UserModel;
  16. }
  17. /**
  18. * 用户列表
  19. */
  20. public function index()
  21. {
  22. if (!$this->request->isAjax()) {
  23. return $this->view->fetch();
  24. }
  25. $data = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  26. $this->success('获取成功', null, $data);
  27. }
  28. /**
  29. * 用户详情
  30. *
  31. * @param $id
  32. */
  33. public function detail($id)
  34. {
  35. if (!$this->request->isAjax()) {
  36. return $this->view->fetch();
  37. }
  38. $user = $this->model->with(['third_oauth', 'parent_user'])->where('id', $id)->find();
  39. if (!$user) {
  40. $this->error(__('No Results were found'));
  41. }
  42. $this->success('获取成功', null, $user);
  43. }
  44. /**
  45. * 更新用户
  46. *
  47. * @param $id
  48. * @return \think\Response
  49. */
  50. public function edit($id = null)
  51. {
  52. $params = $this->request->only(['username', 'nickname', 'mobile', 'password', 'avatar', 'gender', 'email', 'status']);
  53. if (empty($params['password'])) unset($params['password']);
  54. if (empty($params['username'])) unset($params['username']);
  55. $params['id'] = $id;
  56. $this->svalidate($params, '.edit');
  57. unset($params['id']);
  58. $user = $this->model->where('id', $id)->find();
  59. $user->save($params);
  60. $this->success('更新成功', null, $user);
  61. }
  62. /**
  63. * 删除用户(支持批量)
  64. *
  65. * @param $id
  66. * @return \think\Response
  67. */
  68. public function delete($id)
  69. {
  70. if (empty($id)) {
  71. $this->error(__('Parameter %s can not be empty', 'id'));
  72. }
  73. $id = explode(',', $id);
  74. $list = $this->model->where('id', 'in', $id)->select();
  75. $result = Db::transaction(function () use ($list) {
  76. $count = 0;
  77. foreach ($list as $item) {
  78. $count += $item->delete();
  79. }
  80. return $count;
  81. });
  82. if ($result) {
  83. $this->success('删除成功', null, $result);
  84. } else {
  85. $this->error(__('No rows were deleted'));
  86. }
  87. }
  88. public function recharge()
  89. {
  90. if (!$this->request->isAjax()) {
  91. return $this->view->fetch();
  92. }
  93. $params = $this->request->only(['id', 'type', 'amount', 'memo']);
  94. if (!in_array($params['type'], ['money', 'score'])) {
  95. error_stop('参数错误');
  96. }
  97. $result = Db::transaction(function () use ($params) {
  98. return WalletService::change($params['id'], $params['type'], $params['amount'], 'admin_recharge', [], $params['memo']);
  99. });
  100. if ($result) {
  101. $this->success('充值成功');
  102. }
  103. $this->error('充值失败');
  104. }
  105. /**
  106. * 用户优惠券列表
  107. */
  108. public function coupon($id)
  109. {
  110. $userCoupons = UserCouponModel::sheepFilter()->with('coupon')->where('user_id', $id)
  111. ->order('id', 'desc')->paginate($this->request->param('list_rows', 10));
  112. $this->success('获取成功', null, $userCoupons);
  113. }
  114. public function select()
  115. {
  116. if (!$this->request->isAjax()) {
  117. return $this->view->fetch();
  118. }
  119. $data = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  120. $this->success('获取成功', null, $data);
  121. }
  122. }