Paygoldconfig.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 充值配置
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Paygoldconfig extends Backend
  14. {
  15. /**
  16. * Paygoldconfig模型对象
  17. * @var \app\admin\model\Paygoldconfig
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\Paygoldconfig;
  24. $typeList = [
  25. 'isShowList' => $this->model->getIsShowList(),
  26. 'isDefaultList' => $this->model->getIsDefaultList(),
  27. ];
  28. $this->view->assign($typeList);
  29. $this->assignconfig($typeList);
  30. }
  31. public function import()
  32. {
  33. parent::import();
  34. }
  35. /**
  36. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  37. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  38. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  39. */
  40. /**
  41. * 添加
  42. */
  43. public function add()
  44. {
  45. if ($this->request->isPost()) {
  46. $params = $this->request->post("row/a");
  47. $params = $this->preExcludeFields($params);
  48. if (!$params) {
  49. $this->error(__('Parameter %s can not be empty', ''));
  50. }
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException(true)->validate($validate);
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. if ($result == false) {
  60. throw new Exception(__('No rows were inserted'));
  61. }
  62. if ($params['is_default'] == 1) {
  63. $mWhere['is_default'] = 1;
  64. Db::name('paygold_config')->where($mWhere)->update(['is_default'=>0]);
  65. }
  66. $this->success();
  67. } catch (ValidateException|PDOException|Exception $e) {
  68. $this->error($e->getMessage());
  69. }
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 编辑
  75. */
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. $adminIds = $this->getDataLimitAdminIds();
  83. if (is_array($adminIds)) {
  84. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  85. $this->error(__('You have no permission'));
  86. }
  87. }
  88. if ($this->request->isPost()) {
  89. $params = $this->request->post("row/a");
  90. if (!$params) {
  91. $this->error(__('Parameter %s can not be empty', ''));
  92. }
  93. $params = $this->preExcludeFields($params);
  94. try {
  95. //是否采用模型验证
  96. if ($this->modelValidate) {
  97. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  99. $row->validateFailException(true)->validate($validate);
  100. }
  101. if ($params['is_default'] == 1) {
  102. $mWhere['is_default'] = 1;
  103. Db::name('paygold_config')->where($mWhere)->update(['is_default'=>0]);
  104. }
  105. $result = $row->allowField(true)->save($params);
  106. if ($result == false) {
  107. throw new Exception(__('No rows were inserted'));
  108. }
  109. $this->success();
  110. } catch (ValidateException|PDOException|Exception $e) {
  111. $this->error($e->getMessage());
  112. }
  113. }
  114. $this->view->assign("row", $row);
  115. return $this->view->fetch();
  116. }
  117. }