Goodsadd.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\admin\controller\service;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 附加项目管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Goodsadd extends Backend
  11. {
  12. /**
  13. * Goodsadd模型对象
  14. * @var \app\admin\model\service\Goodsadd
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\service\Goodsadd;
  21. $this->view->assign("stateList", $this->model->getStateList());
  22. }
  23. public function index()
  24. {
  25. //设置过滤方法
  26. $this->request->filter(['strip_tags', 'trim']);
  27. if (false === $this->request->isAjax()) {
  28. $goods_id = input('goods_id','');
  29. $this->assignconfig('goods_id',$goods_id);
  30. return $this->view->fetch();
  31. }
  32. //如果发送的来源是 Selectpage,则转发到 Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  37. $list = $this->model
  38. ->where($where)
  39. ->order($sort, $order)
  40. ->paginate($limit);
  41. $result = ['total' => $list->total(), 'rows' => $list->items()];
  42. return json($result);
  43. }
  44. public function add()
  45. {
  46. if (false === $this->request->isPost()) {
  47. $goods_id = input('goods_id','');
  48. $this->assign('goods_id',$goods_id);
  49. return $this->view->fetch();
  50. }
  51. $params = $this->request->post('row/a');
  52. if (empty($params)) {
  53. $this->error(__('Parameter %s can not be empty', ''));
  54. }
  55. $params = $this->preExcludeFields($params);
  56. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  57. $params[$this->dataLimitField] = $this->auth->id;
  58. }
  59. $result = false;
  60. Db::startTrans();
  61. try {
  62. //是否采用模型验证
  63. if ($this->modelValidate) {
  64. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  65. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  66. $this->model->validateFailException()->validate($validate);
  67. }
  68. $result = $this->model->allowField(true)->save($params);
  69. Db::commit();
  70. } catch (ValidateException|PDOException|Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result === false) {
  75. $this->error(__('No rows were inserted'));
  76. }
  77. $this->success();
  78. }
  79. }