Level.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\admin\controller\shopro\Common;
  4. use app\admin\model\shopro\commission\Level as LevelModel;
  5. use think\Db;
  6. class Level extends Common
  7. {
  8. protected $noNeedRight = ['select'];
  9. protected $model = null;
  10. public function _initialize()
  11. {
  12. parent::_initialize();
  13. $this->model = new LevelModel();
  14. }
  15. /**
  16. * 查看
  17. */
  18. public function index()
  19. {
  20. if (!$this->request->isAjax()) {
  21. return $this->view->fetch();
  22. }
  23. $defaultLevel = $this->model->find(1);
  24. if (!$defaultLevel) {
  25. $this->model->save([
  26. 'name' => '默认等级',
  27. 'level' => 1,
  28. 'commission_rules' => [
  29. 'commission_1' => '0.00',
  30. 'commission_2' => '0.00',
  31. 'commission_3' => '0.00'
  32. ]
  33. ]);
  34. }
  35. $list = $this->model->sheepFilter()->select();
  36. $this->success('全部等级', null, $list);
  37. }
  38. /**
  39. * 添加
  40. */
  41. public function add()
  42. {
  43. if (!$this->request->isAjax()) {
  44. return $this->view->fetch();
  45. }
  46. $params = $this->request->only(['name', 'level', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']);
  47. $this->model->save($params);
  48. $this->success('保存成功', null, $this->model);
  49. }
  50. /**
  51. * 编辑
  52. *
  53. * @param $id
  54. */
  55. public function edit($id = null)
  56. {
  57. if (!$this->request->isAjax()) {
  58. return $this->view->fetch('add');
  59. }
  60. $params = $this->request->only(['level', 'name', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']);
  61. $result = Db::transaction(function () use ($id, $params) {
  62. $this->svalidate($params);
  63. $data = $this->model->where('level', $id)->find();
  64. if (!$data) {
  65. $this->error(__('No Results were found'));
  66. }
  67. return $data->save($params);
  68. });
  69. if ($result) {
  70. $this->success('更新成功', null, $result);
  71. } else {
  72. $this->error('更新失败');
  73. }
  74. }
  75. /**
  76. * 详情
  77. *
  78. * @param $id
  79. * @return \think\Response
  80. */
  81. public function detail($id)
  82. {
  83. $detail = $this->model->get($id);
  84. if (!$detail) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $this->success('等级详情', null, $detail);
  88. }
  89. /**
  90. * 删除
  91. *
  92. * @param $id
  93. * @return \think\Response
  94. */
  95. public function delete($id)
  96. {
  97. if (empty($id)) {
  98. $this->error(__('Parameter %s can not be empty', 'id'));
  99. }
  100. $result = Db::transaction(function () use ($id) {
  101. return $this->model->where('level', $id)->delete();
  102. });
  103. if ($result) {
  104. $this->success('删除成功', null, $result);
  105. } else {
  106. $this->error(__('No rows were deleted'));
  107. }
  108. }
  109. // 选择分销商等级
  110. public function select()
  111. {
  112. if (!$this->request->isAjax()) {
  113. return $this->view->fetch();
  114. }
  115. $data = $this->model->sheepFilter()->field('level, name, image, commission_rules')->select();
  116. $this->success('选择等级', null, $data);
  117. }
  118. }