RechargeGift.php 3.5 KB

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