Pledges.php 4.8 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 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. if ($params['type_id'] == '1' && strpos($params['product_id'] , ',') !== false) {
  74. $this->error(__('单品只能选择一个质押产品'));
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException()->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException|PDOException|Exception $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. }
  91. if ($result === false) {
  92. $this->error(__('No rows were inserted'));
  93. }
  94. $this->success();
  95. }
  96. /**
  97. * 编辑
  98. *
  99. * @param $ids
  100. * @return string
  101. * @throws DbException
  102. * @throws \think\Exception
  103. */
  104. public function edit($ids = null)
  105. {
  106. $row = $this->model->get($ids);
  107. if (!$row) {
  108. $this->error(__('No Results were found'));
  109. }
  110. $adminIds = $this->getDataLimitAdminIds();
  111. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  112. $this->error(__('You have no permission'));
  113. }
  114. if (false === $this->request->isPost()) {
  115. $this->view->assign('row', $row);
  116. return $this->view->fetch();
  117. }
  118. $params = $this->request->post('row/a');
  119. if (empty($params)) {
  120. $this->error(__('Parameter %s can not be empty', ''));
  121. }
  122. if ($params['type_id'] == '1' && strpos($params['product_id'] , ',') !== false) {
  123. $this->error(__('单品只能选择一个质押产品'));
  124. }
  125. $params = $this->preExcludeFields($params);
  126. $result = false;
  127. Db::startTrans();
  128. try {
  129. //是否采用模型验证
  130. if ($this->modelValidate) {
  131. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  132. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  133. $row->validateFailException()->validate($validate);
  134. }
  135. $result = $row->allowField(true)->save($params);
  136. Db::commit();
  137. } catch (ValidateException|PDOException|Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if (false === $result) {
  142. $this->error(__('No rows were updated'));
  143. }
  144. $this->success();
  145. }
  146. }