Gift.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. /**
  5. * 礼物管理
  6. *
  7. * @icon fa fa-gift
  8. */
  9. class Gift extends Backend
  10. {
  11. /**
  12. * Gift模型对象
  13. * @var \app\admin\model\Gift
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\Gift;
  20. $typeList = [
  21. 'statusList' => $this->model->getStatusList(),
  22. ];
  23. $this->view->assign($typeList);
  24. $this->assignconfig($typeList);
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. //->with(['gifttype'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. /* foreach ($list as $row) {
  56. $row->getRelation('gifttype')->visible(['name']);
  57. }*/
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 删除
  65. */
  66. public function del($ids = null)
  67. {
  68. if (false === $this->request->isPost()) {
  69. $this->error(__("Invalid parameters"));
  70. }
  71. $ids = $ids ?: $this->request->post("ids");
  72. if (empty($ids)) {
  73. $this->error(__('Parameter %s can not be empty', 'ids'));
  74. }
  75. $pk = $this->model->getPk();
  76. $adminIds = $this->getDataLimitAdminIds();
  77. if (is_array($adminIds)) {
  78. $this->model->where($this->dataLimitField, 'in', $adminIds);
  79. }
  80. $list = $this->model->where($pk, 'in', $ids)->select();
  81. $count = 0;
  82. Db::startTrans();
  83. try {
  84. foreach ($list as $item) {
  85. $count += $item->delete();
  86. }
  87. Db::commit();
  88. } catch (PDOException|Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($count) {
  93. $this->success();
  94. }
  95. $this->error(__('No rows were deleted'));
  96. }
  97. }