Selfetch.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\admin\controller\shopro\dispatch;
  3. use app\common\controller\Backend;
  4. use app\admin\model\shopro\dispatch\Selfetch as SelfetchModel;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use Exception;
  9. /**
  10. * 快递物流
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Selfetch extends Backend
  15. {
  16. /**
  17. * Express模型对象
  18. * @var \app\admin\model\shopro\dispatch\Express
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\shopro\dispatch\Dispatch;
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = false;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax())
  41. {
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $total = $this->model
  44. ->where($where)
  45. ->where('type', 'selfetch')
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->where($where)
  50. ->where('type', 'selfetch')
  51. ->order($sort, $order)
  52. ->limit($offset, $limit)
  53. ->select();
  54. foreach ($list as $row) {
  55. $row->visible(['id','name','type','type_ids', 'selfetch', 'createtime','updatetime']);
  56. $row->selfetch = $this->getSelfetch($row);
  57. }
  58. $list = collection($list)->toArray();
  59. $result = array("total" => $total, "rows" => $list);
  60. return $this->success('到点/自提', null, $result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 添加
  66. */
  67. public function add()
  68. {
  69. if ($this->request->isPost()) {
  70. $params = $this->request->post();
  71. if ($params) {
  72. $params = json_decode($params['data'], true);
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. $selfetchModel = new SelfetchModel();
  77. $selfetchModel->allowField(true)->save($params);
  78. $params['type'] = 'selfetch';
  79. $params['type_ids'] = $selfetchModel->id;
  80. $result = $this->model->allowField(true)->save($params);
  81. Db::commit();
  82. } catch (ValidateException $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (PDOException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result !== false) {
  93. $this->success();
  94. } else {
  95. $this->error(__('No rows were inserted'));
  96. }
  97. }
  98. $this->error(__('Parameter %s can not be empty', ''));
  99. }
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 编辑
  104. */
  105. public function edit($ids = null)
  106. {
  107. $row = $this->model->get($ids);
  108. $row->selfetch = $this->getSelfetch($row);
  109. if (!$row) {
  110. $this->error(__('No Results were found'));
  111. }
  112. $adminIds = $this->getDataLimitAdminIds();
  113. if (is_array($adminIds)) {
  114. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  115. $this->error(__('You have no permission'));
  116. }
  117. }
  118. if ($this->request->isPost()) {
  119. $params = $this->request->post();
  120. if ($params) {
  121. $params = json_decode($params['data'], true);
  122. $result = false;
  123. Db::startTrans();
  124. try {
  125. $selfetchModel = new SelfetchModel();
  126. $selfetch = $selfetchModel->get($row->type_ids);
  127. $selfetch->allowField(true)->save($params);
  128. $params['type'] = 'selfetch';
  129. $params['type_ids'] = $selfetch->id;
  130. $result = $row->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. } catch (PDOException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($result !== false) {
  143. $this->success();
  144. } else {
  145. $this->error(__('No rows were updated'));
  146. }
  147. }
  148. $this->error(__('Parameter %s can not be empty', ''));
  149. }
  150. $this->assignconfig("row", $row);
  151. return $this->view->fetch();
  152. }
  153. /**
  154. * 回收站
  155. */
  156. public function recyclebin()
  157. {
  158. //设置过滤方法
  159. $this->request->filter(['strip_tags']);
  160. if ($this->request->isAjax()) {
  161. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  162. $total = $this->model
  163. ->onlyTrashed()
  164. ->where($where)
  165. ->where('type', 'selfetch')
  166. ->order($sort, $order)
  167. ->count();
  168. $list = $this->model
  169. ->onlyTrashed()
  170. ->where($where)
  171. ->where('type', 'selfetch')
  172. ->order($sort, $order)
  173. ->limit($offset, $limit)
  174. ->select();
  175. $result = array("total" => $total, "rows" => $list);
  176. return json($result);
  177. }
  178. return $this->view->fetch();
  179. }
  180. private function getSelfetch($data)
  181. {
  182. if($data['type'] == 'selfetch' ) {
  183. return \app\admin\model\shopro\dispatch\Selfetch::where('id', $data['type_ids'])->find();
  184. }
  185. return null;
  186. }
  187. }