User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. use app\common\model\SysConfig;
  6. use app\common\service\DiscountService;
  7. /**
  8. * 会员管理
  9. *
  10. * @icon fa fa-user
  11. */
  12. class User extends Backend
  13. {
  14. protected $relationSearch = true;
  15. protected $searchFields = 'id,username,nickname';
  16. /**
  17. * @var \app\admin\model\User
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('User');
  24. $this->assign('levels',$this->model::getLevels());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('keyField')) {
  36. return $this->selectpage();
  37. }
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. $list = $this->model
  40. ->with(['group','admin'])
  41. ->where($where)
  42. ->order($sort, $order)
  43. ->paginate($limit);
  44. foreach ($list as $k => $v) {
  45. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  46. $v->hidden(['password', 'salt']);
  47. }
  48. $result = array("total" => $list->total(), "rows" => $list->items());
  49. return json($result);
  50. }
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 添加
  55. */
  56. public function add()
  57. {
  58. if ($this->request->isPost()) {
  59. $this->token();
  60. }
  61. return parent::add();
  62. }
  63. /**
  64. * 编辑
  65. */
  66. public function edit($ids = null)
  67. {
  68. if ($this->request->isPost()) {
  69. $this->token();
  70. }
  71. $row = $this->model->get($ids);
  72. $this->modelValidate = true;
  73. if (!$row) {
  74. $this->error(__('No Results were found'));
  75. }
  76. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  77. return parent::edit($ids);
  78. }
  79. /**
  80. * 删除
  81. */
  82. public function del($ids = "")
  83. {
  84. if (!$this->request->isPost()) {
  85. $this->error(__("Invalid parameters"));
  86. }
  87. $ids = $ids ? $ids : $this->request->post("ids");
  88. $row = $this->model->get($ids);
  89. $this->modelValidate = true;
  90. if (!$row) {
  91. $this->error(__('No Results were found'));
  92. }
  93. Auth::instance()->delete($row['id']);
  94. $this->success();
  95. }
  96. public function level_discount(){
  97. if ($this->request->isAjax()) {
  98. $levels=$this->model::getLevels();;
  99. $list=[];
  100. $config=DiscountService::getById();
  101. foreach ($levels as $id=>$level){
  102. $list[]=[
  103. 'id'=>$id,
  104. 'name'=>$level,
  105. 'discount'=>$config[$id]??'',
  106. ];
  107. }
  108. $result = array("total" => count($list), "rows" => $list);
  109. return json($result);
  110. }
  111. return $this->view->fetch();
  112. }
  113. public function level_discount_edit($ids){
  114. if($this->request->isGet()){
  115. $this->assign('row',DiscountService::getById($ids));
  116. $this->assign('id',$ids);
  117. return $this->fetch();
  118. }else{
  119. $data=input('row/a');
  120. $this->validate($data,[
  121. 'discount|折扣'=>['require','gt:0','lt:10'],
  122. ]);
  123. DiscountService::saveDiscount($ids,$data['discount']);
  124. $this->success();
  125. }
  126. }
  127. }