Pledges.php 4.5 KB

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