Studio.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\cmgadm\controller\user;
  3. use app\common\controller\Backend;
  4. use think\db;
  5. /**
  6. * 茶室管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Studio extends Backend
  11. {
  12. /**
  13. * Studio模型对象
  14. * @var \app\cmgadm\model\Studio
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\cmgadm\model\Studio;
  21. $list=db('studio')->where(['status'=>1])->select();
  22. $this->view->assign(['stulist'=>$list]);
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = false;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax())
  39. {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField'))
  42. {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $total = $this->model
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. foreach ($list as &$row)
  56. {
  57. $row['refereeid']=get_table_column('studio',$row['refereeid'],'name');
  58. }
  59. $list = collection($list)->toArray();
  60. $result = array("total" => $total, "rows" => $list);
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params['ctime']=time();
  71. $params = $this->preExcludeFields($params);
  72. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  73. $params[$this->dataLimitField] = $this->auth->id;
  74. }
  75. $result = false;
  76. Db::startTrans();
  77. try {
  78. //是否采用模型验证
  79. if ($this->modelValidate) {
  80. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  81. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  82. $this->model->validateFailException(true)->validate($validate);
  83. }
  84. $result = $this->model->allowField(true)->insertGetId($params);
  85. db('user')->where(['id'=>$params['userid']])->update(['ishost'=>1,'stuid'=>$result]);
  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. return parent::add();
  106. }
  107. }