Detail.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\controller\box;
  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. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\box\Detail;
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. public function index($boxid = null)
  35. {
  36. $boxid = $boxid ? $this->request->get('boxid') : '';
  37. if (empty($boxid)) {
  38. $this->error('盲盒有误');
  39. }
  40. $boxid_filter = '?box_id=' . $boxid;
  41. $this->assignconfig('boxid_filter', $boxid_filter);
  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->alias('detail')
  51. ->field('detail.*')
  52. ->field('goods.goods_name,goods.image goods_image')
  53. ->field('box.box_name')
  54. ->join('box box', 'box.id = detail.box_id', 'left')
  55. ->join('goods goods', 'goods.id = detail.goods_id', 'left')
  56. ->where($where)
  57. ->where('detail.box_id', $boxid)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. public function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params = $this->preExcludeFields($params);
  71. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  72. $params[$this->dataLimitField] = $this->auth->id;
  73. }
  74. $result = false;
  75. Db::startTrans();
  76. try {
  77. //是否采用模型验证
  78. if ($this->modelValidate) {
  79. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  80. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  81. $this->model->validateFailException(true)->validate($validate);
  82. }
  83. $result = $this->model->allowField(true)->save($params);
  84. Db::commit();
  85. } catch (ValidateException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (PDOException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (\Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. $box_id = $this->request->get('box_id');
  104. if (empty($box_id)) {
  105. $this->error('盲盒有误', '');
  106. }
  107. $box_name = \app\admin\model\box\Box::where('id', $box_id)->value('box_name');
  108. if (!$box_name) {
  109. $this->error('盲盒有误', '');
  110. }
  111. $this->assign('box_info', ['box_name' => $box_name, 'box_id' => $box_id]);
  112. return $this->view->fetch();
  113. }
  114. }