Vipprivilege.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use Think\Db;
  6. /**
  7. * 会员特权
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Vipprivilege extends Backend
  12. {
  13. /**
  14. * Vipprivilege模型对象
  15. * @var \app\admin\model\Vipprivilege
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Vipprivilege;
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. $vip_id = input('vip_id', 0, 'intval'); //会员id
  34. if (!$vip_id) {
  35. $vip_id = input('ids', 0, 'intval'); //从会员列表第一次跳转过来
  36. }
  37. $this->assignconfig('vip_id', $vip_id);
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->with(['vip'])
  50. ->where($where)
  51. ->where(['vip_id' => $vip_id])
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('vip')->visible(['title','level']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add() {
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $params['vip_id'] = input('vip_id', 0, 'intval');
  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. return $this->view->fetch();
  104. }
  105. }