Pledges.php 4.9 KB

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