Synthesis.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller\product;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 产品合成管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Synthesis extends Backend
  15. {
  16. /**
  17. * Synthesis模型对象
  18. * @var \app\admin\model\product\Synthesis
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\common\model\Synthesis;
  25. }
  26. /**
  27. * 查看
  28. *
  29. * @return string|Json
  30. * @throws \think\Exception
  31. * @throws DbException
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if (false === $this->request->isAjax()) {
  38. return $this->view->fetch();
  39. }
  40. //如果发送的来源是 Selectpage,则转发到 Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  45. $list = $this->model
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. $result = ['total' => $list->total(), 'rows' => $list->items()];
  50. return json($result);
  51. }
  52. /**
  53. * 添加
  54. *
  55. * @return string
  56. * @throws \think\Exception
  57. */
  58. public function add()
  59. {
  60. if (false === $this->request->isPost()) {
  61. return $this->view->fetch();
  62. }
  63. $params = $this->request->post('row/a');
  64. if (empty($params)) {
  65. $this->error(__('Parameter %s can not be empty', ''));
  66. }
  67. $params = $this->preExcludeFields($params);
  68. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  69. $params[$this->dataLimitField] = $this->auth->id;
  70. }
  71. $result = false;
  72. Db::startTrans();
  73. try {
  74. //是否采用模型验证
  75. if ($this->modelValidate) {
  76. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  77. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  78. $this->model->validateFailException()->validate($validate);
  79. }
  80. $result = $this->model->allowField(true)->save($params);
  81. Db::commit();
  82. } catch (ValidateException|PDOException|Exception $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. }
  86. if ($result === false) {
  87. $this->error(__('No rows were inserted'));
  88. }
  89. $this->success();
  90. }
  91. /**
  92. * 编辑
  93. *
  94. * @param $ids
  95. * @return string
  96. * @throws DbException
  97. * @throws \think\Exception
  98. */
  99. public function edit($ids = null)
  100. {
  101. $row = $this->model->get($ids);
  102. if (!$row) {
  103. $this->error(__('No Results were found'));
  104. }
  105. $adminIds = $this->getDataLimitAdminIds();
  106. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  107. $this->error(__('You have no permission'));
  108. }
  109. if (false === $this->request->isPost()) {
  110. $this->view->assign('row', $row);
  111. return $this->view->fetch();
  112. }
  113. $params = $this->request->post('row/a');
  114. if (empty($params)) {
  115. $this->error(__('Parameter %s can not be empty', ''));
  116. }
  117. $params = $this->preExcludeFields($params);
  118. $result = false;
  119. Db::startTrans();
  120. try {
  121. //是否采用模型验证
  122. if ($this->modelValidate) {
  123. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  125. $row->validateFailException()->validate($validate);
  126. }
  127. $result = $row->allowField(true)->save($params);
  128. Db::commit();
  129. } catch (ValidateException|PDOException|Exception $e) {
  130. Db::rollback();
  131. $this->error($e->getMessage());
  132. }
  133. if (false === $result) {
  134. $this->error(__('No rows were updated'));
  135. }
  136. $this->success();
  137. }
  138. }