FreightItems.php 4.8 KB

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