Raprize.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 Raprize extends Backend
  15. {
  16. /**
  17. * Raprize模型对象
  18. * @var \app\admin\model\Raprize
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Raprize;
  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. $ra_id=input('ids');
  37. if(empty($ra_id)){
  38. $ra_id=session('ra_id');
  39. }else{
  40. session('ra_id',$ra_id);
  41. }
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if (false === $this->request->isAjax()) {
  45. return $this->view->fetch();
  46. }
  47. //如果发送的来源是 Selectpage,则转发到 Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  52. $dts['r_id'] =$ra_id;
  53. $list = $this->model
  54. ->where($where)
  55. ->where($dts)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $result = ['total' => $list->total(), 'rows' => $list->items()];
  59. return json($result);
  60. }
  61. /**
  62. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  63. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  64. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  65. */
  66. /**
  67. * 添加
  68. *
  69. * @return string
  70. * @throws \think\Exception
  71. */
  72. public function add()
  73. {
  74. if (false === $this->request->isPost()) {
  75. return $this->view->fetch();
  76. }
  77. $params = $this->request->post('row/a');
  78. if (empty($params)) {
  79. $this->error(__('Parameter %s can not be empty', ''));
  80. }
  81. $params = $this->preExcludeFields($params);
  82. $ra_id=session('ra_id');
  83. $params['r_id']=$ra_id;
  84. $raffle = \app\admin\model\Raffle::getById($ra_id);
  85. $params['r_name']=$raffle->name;
  86. $category=\app\common\model\Category::getById($params['t_type']);
  87. $params['t_type_name']=$category->name;
  88. $prize=\app\admin\model\Prize::getById($params['p_id']);
  89. $params['p_name']=$prize->name;
  90. $params['price']=$prize->price;
  91. $params['money']=$prize->money;
  92. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  93. $params[$this->dataLimitField] = $this->auth->id;
  94. }
  95. $result = false;
  96. Db::startTrans();
  97. try {
  98. //是否采用模型验证
  99. if ($this->modelValidate) {
  100. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  101. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  102. $this->model->validateFailException()->validate($validate);
  103. }
  104. $result = $this->model->allowField(true)->save($params);
  105. Db::commit();
  106. } catch (ValidateException|PDOException|Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if ($result === false) {
  111. $this->error(__('No rows were inserted'));
  112. }
  113. $this->success();
  114. }
  115. }