Rebate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\controller\conf;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 重置返利配置
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Rebate extends Backend
  11. {
  12. /**
  13. * Rebate模型对象
  14. * @var \app\admin\model\conf\Rebate
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\conf\Rebate;
  21. }
  22. public function import()
  23. {
  24. parent::import();
  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 add()
  35. {
  36. if ($this->request->isPost()) {
  37. $params = $this->request->post("row/a");
  38. if ($params) {
  39. $params = $this->preExcludeFields($params);
  40. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  41. $params[$this->dataLimitField] = $this->auth->id;
  42. }
  43. $result = false;
  44. Db::startTrans();
  45. try {
  46. //是否采用模型验证
  47. if ($this->modelValidate) {
  48. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  49. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  50. $this->model->validateFailException(true)->validate($validate);
  51. }
  52. $result = $this->model->allowField(true)->save($params);
  53. Db::commit();
  54. } catch (ValidateException $e) {
  55. Db::rollback();
  56. $this->error($e->getMessage());
  57. } catch (PDOException $e) {
  58. Db::rollback();
  59. $this->error($e->getMessage());
  60. } catch (Exception $e) {
  61. Db::rollback();
  62. $this->error($e->getMessage());
  63. }
  64. if ($result !== false) {
  65. $this->success();
  66. } else {
  67. $this->error(__('No rows were inserted'));
  68. }
  69. }
  70. $this->error(__('Parameter %s can not be empty', ''));
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 编辑
  76. */
  77. public function edit($ids = null)
  78. {
  79. $row = $this->model->get($ids);
  80. if (!$row) {
  81. $this->error(__('No Results were found'));
  82. }
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds)) {
  85. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  86. $this->error(__('You have no permission'));
  87. }
  88. }
  89. if ($this->request->isPost()) {
  90. $params = $this->request->post("row/a");
  91. if ($params) {
  92. $params = $this->preExcludeFields($params);
  93. $result = false;
  94. Db::startTrans();
  95. try {
  96. //是否采用模型验证
  97. if ($this->modelValidate) {
  98. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  100. $row->validateFailException(true)->validate($validate);
  101. }
  102. if($row["id"]) $result = $row->execute("update hx_rebate_config set `recharge` = ".$params["recharge"].", `preget` = ".$params["preget"].", `prepreget` = ".$params["prepreget"]." where id = ".$row["id"]);
  103. // $result = $row->allowField(true)->save($params);
  104. Db::commit();
  105. } catch (ValidateException $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. } catch (PDOException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if ($result !== false) {
  116. $this->success();
  117. } else {
  118. $this->error(__('No rows were updated'));
  119. }
  120. }
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. $this->view->assign("row", $row);
  124. return $this->view->fetch();
  125. }
  126. }