Level.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. use app\admin\model\shopro\commission\Level as LevelModel;
  9. /**
  10. * 分销商等级管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Level extends Backend
  15. {
  16. /**
  17. * Level模型对象
  18. * @var \app\admin\model\shopro\commission\Level
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new LevelModel();
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. $defaultLevel = $this->model->get(['level' => 1]);
  37. if(!$defaultLevel) {
  38. $this->model->save([
  39. 'name' => '默认等级',
  40. 'level' => 1,
  41. 'commission_rules' => '{"commission_1":"0.00","commission_2":"0.00","commission_3":"0.00"}'
  42. ]);
  43. }
  44. if($this->request->isAjax()) {
  45. $data = $this->model->order('level asc')->select();
  46. $this->success('分销商等级', null, $data);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 添加
  52. */
  53. public function add()
  54. {
  55. if ($this->request->isPost()) {
  56. $params = $this->request->post();
  57. if ($params) {
  58. $result = false;
  59. Db::startTrans();
  60. try {
  61. $result = $this->model->allowField(true)->save($params);
  62. Db::commit();
  63. } catch (ValidateException $e) {
  64. Db::rollback();
  65. $this->error($e->getMessage());
  66. } catch (PDOException $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. } catch (Exception $e) {
  70. Db::rollback();
  71. $this->error($e->getMessage());
  72. }
  73. if ($result !== false) {
  74. $this->success();
  75. } else {
  76. $this->error(__('No rows were inserted'));
  77. }
  78. }
  79. $this->error(__('Parameter %s can not be empty', ''));
  80. }
  81. $this->getExistLevel();
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 编辑
  86. */
  87. public function edit($level = null)
  88. {
  89. if(!$level) {
  90. $level = $this->request->get('level');
  91. }
  92. $row = $this->model->get($level);
  93. if (!$row) {
  94. $this->error(__('No Results were found'));
  95. }
  96. if ($this->request->isPost()) {
  97. $params = $this->request->post();
  98. if ($params) {
  99. $result = false;
  100. Db::startTrans();
  101. try {
  102. if($level != $params['new_level']) {
  103. $params['level'] = $params['new_level'];
  104. $result = LevelModel::strict(false)->insert($params);
  105. $result = $row->delete();
  106. }else {
  107. $result = $row->allowField(true)->save($params);
  108. }
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($result !== false) {
  121. $this->success();
  122. } else {
  123. $this->error(__('No rows were updated'));
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $this->getExistLevel();
  129. $this->assignconfig('row', $row);
  130. return $this->view->fetch();
  131. }
  132. private function getExistLevel()
  133. {
  134. $existLevel = $this->model->column('level');
  135. $this->assignconfig('existLevel', $existLevel);
  136. return $existLevel;
  137. }
  138. }