Express.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\shopro\data;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 快递公司
  7. */
  8. class Express extends Common
  9. {
  10. protected $noNeedRight = ['select'];
  11. /**
  12. * Faq模型对象
  13. * @var \app\admin\model\shopro\data\Express
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\shopro\data\Express;
  20. }
  21. /**
  22. * 查看
  23. *
  24. * @return string|Json
  25. * @throws \think\Exception
  26. * @throws DbException
  27. */
  28. public function index()
  29. {
  30. if (!$this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. $list = $this->model->sheepFilter()->paginate($this->request->request('list_rows', 10));
  34. $this->success('', null, $list);
  35. }
  36. /**
  37. * 选择快递公司
  38. *
  39. * @return void
  40. */
  41. public function select()
  42. {
  43. if (!$this->request->isAjax()) {
  44. return $this->view->fetch();
  45. }
  46. $list = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  47. $this->success('', null, $list);
  48. }
  49. /**
  50. * 添加
  51. */
  52. public function add()
  53. {
  54. if (!$this->request->isAjax()) {
  55. return $this->view->fetch();
  56. }
  57. $params = $this->request->only(['name', 'code', 'weigh']);
  58. $this->svalidate($params, '.add');
  59. $this->model->save($params);
  60. $this->success('保存成功', null, $this->model);
  61. }
  62. /**
  63. * 详情
  64. *
  65. * @param $id
  66. * @return \think\Response
  67. */
  68. public function detail($id)
  69. {
  70. $detail = $this->model->where('id', $id)->find();
  71. if (!$detail) {
  72. $this->error(__('No Results were found'));
  73. }
  74. $this->success('获取成功', null, $detail);
  75. }
  76. /**
  77. * 编辑(支持批量)
  78. */
  79. public function edit($id = null)
  80. {
  81. if (!$this->request->isAjax()) {
  82. return $this->view->fetch('add');
  83. }
  84. $params = $this->request->only(['name', 'code', 'weigh']);
  85. $list = $this->model->where('id', 'in', $id)->select();
  86. $result = Db::transaction(function () use ($list, $params) {
  87. $count = 0;
  88. foreach ($list as $item) {
  89. $params['id'] = $item->id;
  90. $this->svalidate($params);
  91. $count += $item->save($params);
  92. }
  93. return $count;
  94. });
  95. if ($result) {
  96. $this->success('更新成功', null, $result);
  97. } else {
  98. $this->error('更新失败');
  99. }
  100. }
  101. /**
  102. * 删除(支持批量)
  103. *
  104. * @param $id
  105. * @return \think\Response
  106. */
  107. public function delete($id)
  108. {
  109. if (empty($id)) {
  110. $this->error(__('Parameter %s can not be empty', 'id'));
  111. }
  112. $list = $this->model->where('id', 'in', $id)->select();
  113. $result = Db::transaction(function () use ($list) {
  114. $count = 0;
  115. foreach ($list as $item) {
  116. $count += $item->delete();
  117. }
  118. return $count;
  119. });
  120. if ($result) {
  121. $this->success('删除成功', null, $result);
  122. } else {
  123. $this->error(__('No rows were deleted'));
  124. }
  125. }
  126. }