Raffle.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\response\Json;
  9. /**
  10. * 活动管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Raffle extends Backend
  15. {
  16. /**
  17. * Raffle模型对象
  18. * @var \app\admin\model\Raffle
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Raffle;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 查看
  29. *
  30. * @return string|Json
  31. * @throws \think\Exception
  32. * @throws DbException
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if (false === $this->request->isAjax()) {
  39. return $this->view->fetch();
  40. }
  41. //如果发送的来源是 Selectpage,则转发到 Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  46. if($this->auth->id==1){
  47. $list = $this->model->where($where)->order($sort, $order)
  48. ->paginate($limit);
  49. }else{
  50. $website=$this->auth->username;
  51. $list = $this->model->where($where)->where("website='{$website}'")->order($sort, $order)
  52. ->paginate($limit);
  53. }
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. /**
  58. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  59. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  60. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  61. */
  62. /**
  63. * 添加
  64. *
  65. * @return string
  66. * @throws \think\Exception
  67. */
  68. public function add()
  69. {
  70. if (false === $this->request->isPost()) {
  71. return $this->view->fetch();
  72. }
  73. $params = $this->request->post('row/a');
  74. if (empty($params)) {
  75. $this->error(__('Parameter %s can not be empty', ''));
  76. }
  77. $params = $this->preExcludeFields($params);
  78. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  79. $params[$this->dataLimitField] = $this->auth->id;
  80. }
  81. $params['website']=$this->auth->username;
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. //是否采用模型验证
  86. if ($this->modelValidate) {
  87. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  88. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  89. $this->model->validateFailException()->validate($validate);
  90. }
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException|PDOException|Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result === false) {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. $this->success();
  101. }
  102. }