Detail.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\admin\controller\shai;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 盲盒详情
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Detail extends Backend
  13. {
  14. protected $modelValidate = true;
  15. /**
  16. * Detail模型对象
  17. * @var \app\admin\model\box\Detail
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ['*'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\shai\Detail;
  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. public function index($boxid = null)
  36. {
  37. $boxid = $boxid ? $this->request->get('boxid') : '';
  38. if (empty($boxid)) {
  39. $this->error('盲盒有误');
  40. }
  41. $boxid_filter = '?box_id=' . $boxid;
  42. $this->assignconfig('boxid_filter', $boxid_filter);
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model->alias('detail')
  52. ->field('detail.*')
  53. ->field('goods.goods_name,goods.image goods_image')
  54. ->field('box.box_name')
  55. ->join('box box', 'box.id = detail.box_id', 'left')
  56. ->join('goods goods', 'goods.id = detail.goods_id', 'left')
  57. ->where($where)
  58. ->where('detail.box_id', $boxid)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. public function add()
  67. {
  68. if ($this->request->isPost()) {
  69. $params = $this->request->post("row/a");
  70. print_r($params);
  71. if ($params) {
  72. $params = $this->preExcludeFields($params);
  73. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  74. $params[$this->dataLimitField] = $this->auth->id;
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException(true)->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (\Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result !== false) {
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. $box_id = $this->request->get('box_id');
  106. if (empty($box_id)) {
  107. $this->error('盲盒有误', '');
  108. }
  109. $box_name = \app\admin\model\box\Box::where('id', $box_id)->value('box_name');
  110. if (!$box_name) {
  111. $this->error('盲盒有误', '');
  112. }
  113. $this->assign('box_info', ['box_name' => $box_name, 'box_id' => $box_id]);
  114. return $this->view->fetch();
  115. }
  116. }