Blockresource.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\admin\controller\clientmanage\bookcity;
  3. use app\common\model\ManageBlock;
  4. use app\common\controller\Backend;
  5. use app\common\model\Attachment;
  6. use app\common\model\Book;
  7. use app\main\service\ActivityService;
  8. use think\Controller;
  9. use think\Exception;
  10. use think\exception\ErrorException;
  11. use think\exception\PDOException;
  12. use think\Request;
  13. /**
  14. * 书城管理-资源管理
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Blockresource extends Backend
  19. {
  20. /**
  21. * ManageBlockResource模型对象
  22. */
  23. protected $model = null;
  24. protected $blockData = [];
  25. // protected $searchFields = 'block_id';
  26. protected $relationSearch = true;
  27. /**
  28. * 是否开启Validate验证
  29. */
  30. protected $modelValidate = true;
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = model('ClientManageBlockResource');
  35. $this->view->assign("typeList", $this->model->getTypeList($this->request->get('page_id')));
  36. $page_id = model('ClientManageBlock')->where('id','in',$this->request->get('block_id'))->find();
  37. $blockList = collection(model('ClientManageBlock')->where('page_id', 'in', $page_id['page_id'])->select())->toArray();
  38. foreach ($blockList as $k => $v) {
  39. $this->blockData[$v['id']] = [
  40. 'name' => $v['name'],
  41. 'type' => $v['type']
  42. ];
  43. }
  44. $this->view->assign('block_id', $this->request->get('block_id'));
  45. $this->assignconfig('block_id', $this->request->get('block_id'));
  46. $this->assignconfig('page_id', $this->request->get('page_id'));
  47. $this->view->assign('blockData', $this->blockData);
  48. }
  49. /**
  50. * 查看
  51. */
  52. public function index()
  53. {
  54. if ($this->request->isAjax()) {
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
  56. $total = $this->model
  57. ->where($where)
  58. ->with('Book')
  59. ->order($sort, $order)
  60. ->count();
  61. $list = $this->model
  62. ->where($where)
  63. ->with('Book')
  64. ->order($sort, $order)
  65. ->limit($offset, $limit)
  66. ->select();
  67. $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /*
  73. * 新增块资源
  74. */
  75. public function add()
  76. {
  77. if($this->request->isPost()){
  78. $row = $this->request->post('row/a');
  79. if($row){
  80. try{
  81. //是否采用模型验证
  82. if ($this->modelValidate) {
  83. $name = basename(str_replace('\\', '/', get_class($this->model)));
  84. $this->model->validate(true);
  85. }
  86. $this->model->startTrans();
  87. //保存资源信息
  88. if($this->model->allowField(true)->save($row) == false){
  89. throw new Exception($this->model->getError());
  90. }
  91. switch ($row['page_id']){
  92. case '1': //男频
  93. case '2': //女频
  94. $row['free_stime'] = 0;
  95. $row['free_etime'] = 0;
  96. break;
  97. case '3': //限免男频
  98. case '4': //限免女频
  99. if($row['type']==1){
  100. //修改对应书籍记录限免时间
  101. $book = Book::get($row['book_id']);
  102. $book->setAttr('free_stime', strtotime($row['free_stime']));
  103. $book->setAttr('free_etime', strtotime($row['free_etime']));
  104. if($book->allowField(true)->save() == false){
  105. throw new Exception($book->getError());
  106. }
  107. }
  108. break;
  109. default:
  110. throw new Exception('页类型不存在');
  111. }
  112. $this->model->commit();
  113. $this->success();
  114. }catch (Exception $e){
  115. $this->model->rollback();
  116. $this->error($e->getMessage());
  117. }
  118. }
  119. $this->error(__('Parameter %s can not be empty', ''));
  120. }
  121. $this->view->assign('page_id', $this->request->get('page_id'));
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit($ids = NULL)
  128. {
  129. $row = $this->model->get($ids);
  130. if (!$row)
  131. $this->error(__('No Results were found'));
  132. $adminIds = $this->getDataLimitAdminIds();
  133. if (is_array($adminIds)) {
  134. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  135. $this->error(__('You have no permission'));
  136. }
  137. }
  138. if ($this->request->isPost()) {
  139. $params = $this->request->post("row/a");
  140. if ($params) {
  141. try {
  142. //是否采用模型验证
  143. if ($this->modelValidate) {
  144. $name = basename(str_replace('\\', '/', get_class($this->model)));
  145. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  146. $row->validate($validate);
  147. }
  148. $row->startTrans();
  149. switch ($params['type']) {
  150. case "1";
  151. $params['url'] = '';
  152. $params['client_activity_id'] = '';
  153. break;
  154. case "2";
  155. $params['book_id'] = '';
  156. $params['client_activity_id'] = '';
  157. break;
  158. case "3";
  159. $params['book_id'] = '';
  160. $params['url'] = '';
  161. $params['client_activity_id'] = '';
  162. break;
  163. case "4";
  164. $params['book_id'] = '';
  165. $params['url'] = '';
  166. break;
  167. }
  168. $result = $row->allowField(true)->save($params);
  169. if($result === false){
  170. throw new Exception($row->getError());
  171. }
  172. switch ($params['page_id']){
  173. case '1': //男频
  174. case '2': //女频
  175. $params['free_stime'] = 0;
  176. $params['free_etime'] = 0;
  177. break;
  178. case '3': //限免男频
  179. case '4': //限免女频
  180. if($params['type'] == 1){
  181. //修改对应书籍记录限免时间
  182. $book = Book::get($params['book_id']);
  183. $book->setAttr('free_stime', strtotime($params['free_stime']));
  184. $book->setAttr('free_etime', strtotime($params['free_etime']));
  185. if($book->allowField(true)->save() == false){
  186. throw new Exception($book->getError());
  187. }
  188. }
  189. break;
  190. default:
  191. throw new Exception('页类型不存在');
  192. }
  193. $row->commit();
  194. $this->success();
  195. }catch (Exception $e){
  196. $row->rollback();
  197. $this->error($e->getMessage());
  198. }
  199. }
  200. $this->error(__('Parameter %s can not be empty', ''));
  201. }
  202. //清除无效的配置
  203. switch ($row['type']) {
  204. case "1":
  205. $row['url'] = '';
  206. $row['client_activity_id'] = '';
  207. break;
  208. case "2":
  209. $row['book_id'] = '';
  210. $row['client_activity_id'] = '';
  211. break;
  212. case "3":
  213. $row['url'] = '';
  214. $row['book_id'] = '';
  215. $row['client_activity_id'] = '';
  216. break;
  217. case "4":
  218. $row['url'] = '';
  219. $row['book_id'] = '';
  220. break;
  221. }
  222. $this->view->assign("row", $row);
  223. $activityName = '';
  224. if (!empty($row['client_activity_id'])) {
  225. $activity = model('activity')->get($row['client_activity_id']);
  226. $activityName = $activity->name;
  227. }
  228. $this->view->assign('page_id', $this->request->get('page_id'));
  229. $this->assignconfig('row', $row);
  230. $this->view->assign("activityName", $activityName);
  231. return $this->view->fetch();
  232. }
  233. }