Defaultrecommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller\clientmanage\recommand;
  3. use app\common\controller\Backend;
  4. use app\main\constants\ClientApiConstants;
  5. use think\Controller;
  6. use think\Request;
  7. use app\common\library\Redis;
  8. use app\common\model\ClientDefaultRecommand;
  9. /**
  10. * 阳光app客户端内置书
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Defaultrecommand extends Backend
  15. {
  16. /**
  17. * @var ClientDefaultRecommand
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('ClientDefaultRecommand');
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 添加
  33. */
  34. public function add()
  35. {
  36. if ($this->request->isPost())
  37. {
  38. $params = $this->request->post("row/a");
  39. if ($params)
  40. {
  41. if ($this->dataLimit)
  42. {
  43. $params[$this->dataLimitField] = $this->auth->id;
  44. }
  45. try
  46. {
  47. //是否采用模型验证
  48. if ($this->modelValidate)
  49. {
  50. $name = basename(str_replace('\\', '/', get_class($this->model)));
  51. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  52. $this->model->validate($validate);
  53. }
  54. $existBook = $this->model->where('book_id', $params['book_id'])->find();
  55. if ($existBook) {
  56. $this->error('书籍在内置书库中已经存在');
  57. }
  58. $result = $this->model->allowField(true)->save($params);
  59. if ($result !== false)
  60. {
  61. $this->success();
  62. }
  63. else
  64. {
  65. $this->error($this->model->getError());
  66. }
  67. }
  68. catch (\think\exception\PDOException $e)
  69. {
  70. $this->error($e->getMessage());
  71. }
  72. }
  73. $this->error(__('Parameter %s can not be empty', ''));
  74. }
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 编辑
  79. */
  80. public function edit($ids = NULL)
  81. {
  82. $row = $this->model->get($ids);
  83. if (!$row)
  84. $this->error(__('No Results were found'));
  85. $adminIds = $this->getDataLimitAdminIds();
  86. if (is_array($adminIds))
  87. {
  88. if (!in_array($row[$this->dataLimitField], $adminIds))
  89. {
  90. $this->error(__('You have no permission'));
  91. }
  92. }
  93. if ($this->request->isPost())
  94. {
  95. $params = $this->request->post("row/a");
  96. if ($params)
  97. {
  98. try
  99. {
  100. //是否采用模型验证
  101. if ($this->modelValidate)
  102. {
  103. $name = basename(str_replace('\\', '/', get_class($this->model)));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  105. $row->validate($validate);
  106. }
  107. $existBook = $this->model
  108. ->where('book_id', $params['book_id'])
  109. ->where('id', '<>', $ids)
  110. ->find();
  111. if ($existBook) {
  112. $this->error('书籍在内置书库中已经存在');
  113. }
  114. $result = $row->allowField(true)->save($params);
  115. if ($result !== false)
  116. {
  117. $this->success();
  118. }
  119. else
  120. {
  121. $this->error($row->getError());
  122. }
  123. }
  124. catch (\think\exception\PDOException $e)
  125. {
  126. $this->error($e->getMessage());
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. $this->view->assign("row", $row);
  132. return $this->view->fetch();
  133. }
  134. /**
  135. * 删除
  136. */
  137. public function del($ids = "")
  138. {
  139. if ($ids)
  140. {
  141. $this->model->where('id', $ids)->delete();
  142. }
  143. $this->error(__('Parameter %s can not be empty', 'ids'));
  144. }
  145. public function clearcache()
  146. {
  147. try {
  148. $redis = Redis::instance();
  149. $redis->del(ClientApiConstants::DEFAULT_RECOMMAND_KEY);
  150. $this->success();
  151. } catch (\think\exception\PDOException $e) {
  152. $this->error($e->getMessage());
  153. }
  154. }
  155. }